Legend symbol mismatch for hachured pattern in PyGMT

I’m currently working on a map using PyGMT, and I’ve encountered an issue when trying to insert a legend for a hachured symbol. The problem is that the legend symbol does not match the hachure pattern I’ve defined in the map. I’m looking for some guidance on how to resolve this discrepancy. Here’s an example of the problem:

import pygmt

fig = pygmt.Figure()
fig.basemap(
region=[0, 5, 0, 5],
projection="X10c",
frame="rlbt+glightgray",)

fig.plot(
x=2.5,
y=2.5,
style="c1c", 
pen="1p,red", 
fill="p8+fred+b-", label='hachure')

fig.legend(box=False, position='+o0.5c')

fig.show()

Same in GMT CLI so we have ourselves a bug! Looks like it ignores the b- indication for the legend.

1 Like

Hello @Robson,

when using the label parameter of Figure.plot and adding an auto-legend, the modifiers +f and +b to adjust the color of the pattern seem to be ignored, as mentioned by @pwessel. But when using an external GMT specific legend file and passing it the spec parameter of Figure.legend the legend entry is correctly added:

I just modified and extended your code example to test this:

Legend file (please place it in your working directory): legend_pattern.txt (195 Bytes)

import pygmt

fig = pygmt.Figure()

# -----------------------------------------------------------------------------
# Left: auto-legend via label parameter
fig.basemap(
    region=[0, 5, 0, 5],
    projection="X10c",
    frame="rlbt+tauto-legend via label +glightgray",
)
fig.plot(
    x=2.5,
    y=2.5,
    style="c1c", 
    pen="1p,red", 
    fill="p8+fred+b",
    label="fill hachure colored",
)
fig.legend(box=False)

fig.shift_origin(xshift="+w0.5c")

# -----------------------------------------------------------------------------
# Right: manual legend via external file passed to spec parameter
fig.basemap(
    region=[0, 5, 0, 5],
    projection="X10c",
    frame="rlbt+tmanual legend via spec+glightgray",
)
fig.plot(
    x=2.5,
    y=2.5,
    style="c1c", 
    pen="1p,red", 
    fill="p8+fred+b",
)

fig.legend(spec="legend_pattern.txt", box=False)

fig.show()
# fig.savefig(fname="legend_pattern.png")

Output figure:

1 Like

@yvonnefroehlich Thank you very much, I will mark your answer with the workaround using the specifications file as a solution.

Bug found and fixed in this PR. Expect approval and merging into master soon. Thanks for pointing this problem out!