Pygmt.legend

hello everyone
I want to use shift_origin to draw multiple graphics on a canvas, and then draw the labels from each graphic into the same legend. Here is my code, but the result of the legend doesn’t seem to be very good. How can I separate the three stacked labels

import pygmt

fig = pygmt.Figure()



font ="15p,Helvetica-Bold"
fig.basemap(region=[0,30,0,12],
            projection="X30c/20c",
            frame=["af"],)
fig.plot(x=1, y=2, style="t0.7c",pen="0.5p black",fill="red", label="1")
fig.text(position="TL", text="(a)",offset="j+1c/0.5c", font=font)

fig.shift_origin(xshift="31c", yshift="10c")
fig.basemap(region=[0,15,0,8],
            projection="X15c/8c",
            frame=["af"])
fig.plot(x=1, y=2, style="t0.7c",pen="0.5p black",fill="black", label="2")
fig.text(position="TL",text="(b)", offset="j+1c/0.5c", font=font)

fig.shift_origin(yshift="-9c")
fig.basemap(region=[0,12,0,8],
            projection="X15c/8c",
            frame=["af"])
fig.plot(x=1, y=2, style="t0.7c",pen="0.5p black",fill="blue", label="3")
fig.text(position="TL", text="(c)", offset="j+1c/0.5c", font=font)
fig.legend(position="JBR+jBR+o5.5c/-1c", box=True)
fig.show()

Hello @kjz1997,

You mean, you want to add some vertical space between the different entries in the legend? This can be accomplished by appending +G and the desired value to the label parameter. This adds vertical space above the legend entry. An overview of all modifiers for the label parameter can be found at gmt — GMT 6.5.0 documentation.

import pygmt

font ="15p,Helvetica-Bold"

fig = pygmt.Figure()

fig.basemap(region=[0, 30, 0, 12], projection="X30c/20c", frame=["af"])
fig.plot(x=1, y=2, style="t0.7c", pen="0.5p,black", fill="red", label="1+G0.3c")
fig.text(position="TL", text="(a)",offset="j+1c/0.5c", font=font)

fig.shift_origin(xshift="31c", yshift="10c")
fig.basemap(region=[0, 15, 0, 8], projection="X15c/8c", frame=["af"])
fig.plot(x=1, y=2, style="t0.7c",pen="0.5p,black", fill="black", label="2+G0.5c")
fig.text(position="TL",text="(b)", offset="j+1c/0.5c", font=font)

fig.shift_origin(yshift="-9c")
fig.basemap(region=[0, 12, 0, 8], projection="X15c/8c", frame=["af"])
fig.plot(x=1, y=2, style="t0.7c", pen="0.5p,black", fill="blue", label="3+G0.5c")
fig.text(position="TL", text="(c)", offset="j+1c/0.5c", font=font)

fig.legend(position="JBR+jBR+o5.5c/-1c", box=True)

fig.show()

OK, thanks. Now, I have another problem, how can I put the label in the center? From the current results, it can be seen that the label is positioned higher in the box, which is very unsightly. I added the ‘o’ command after the label, but it didn’t work

import pygmt

import pygmt

font ="15p,Helvetica-Bold"

fig = pygmt.Figure()

fig.basemap(region=[0, 30, 0, 12], projection="X30c/20c", frame=["af"])
fig.plot(x=[3,5], y=[4,8], pen="0.5p,black", fill="red", label="SAR footprint+N3")
fig.text(position="TL", text="(a)",offset="j+1c/0.5c", font=font)

fig.shift_origin(xshift="31c", yshift="10c")
fig.basemap(region=[0, 15, 0, 8], projection="X15c/8c", frame=["af"])
fig.plot(x=[1,3], y=[2,5],pen="0.5p,black", fill="black", label="Study area")
fig.text(position="TL",text="(b)", offset="j+1c/0.5c", font=font)

fig.shift_origin(yshift="-9c")
fig.basemap(region=[0, 12, 0, 8], projection="X15c/8c", frame=["af"])
fig.plot(x=1, y=2, style="t0.7c", pen="0.5p,black", fill="blue", label="SM sensor")
fig.text(position="TL", text="(c)", offset="j+1c/0.5c", font=font)

fig.legend(position="JBR+jBR+o0.5c/0.5c+w9c/3c", box=True)

fig.show()

Changing box=True to something like box="+c0.3c+p".

In addition to this, you maybe want to reduce the height of the legend. If you only give the width of the legend, the height is determined automatically, i.e., change "+w9c/3c" to "+w9c".