1

I have a problem and struggle to find a solution, or phrase the question for a search engine. I have many python plots where i use hatches to show exclusions from constraints (so some hatches are not in the plot). However in the legend these hatches look weird, because they are not centered but aligned to the global grid. For example "HS excl." is the hatch "++", but the horizontal line is not centered. Similar for "STU obs." or "Vacuum stab.". Is there a way to adjust the refence point for the legend, or any other way to fix that issue? My Legend code looks like this:

legend_elements=[Patch(facecolor='none',hatch="\\\\",label='not unitary',edgecolor="#8D8896",lw=1),
                Patch(facecolor='none',hatch="//",label='not bfb',edgecolor="#752692",lw=1),
                Patch(facecolor='none',hatch="---",label='STU obs.',edgecolor="#124C8A",lw=1),
                Patch(facecolor='none',hatch="xx",label='Vacuum stab.',edgecolor="#B9314F",lw=1),
                Patch(facecolor='none',hatch="||",label='HB excl.',edgecolor="#F57200",lw=1),
                Patch(facecolor='none',hatch="++",label='HS excl.',edgecolor="tab:green",lw=1),
                Patch(facecolor='none',hatch="//",label='$h_{95}$ excl.',edgecolor="#4EA5D9",lw=1),
                Patch(facecolor="darkgreen",label='stable')
                ]


ax.legend(handles=legend_elements,ncols=2,loc="lower center",bbox_to_anchor=(0.5, 1.05))

Thank you very much.

Plot with weird hatches in the Legend

1
  • 1
    An idea that could work for you, is to double the density of the hatches in the legend. So use hatch="++++" in the legend, while using hatch="++" in the plot.
    – JohanC
    Commented Mar 24 at 19:01

1 Answer 1

0

You could try to enlarge the plot legend, e.g. by setting the legend properties to {'size': 24}. In this way the legend labels get bigger, which makes their hatches look better:

import matplotlib.pyplot as plt
import numpy as np

# based on the example from https://matplotlib.org/stable/gallery/lines_bars_and_markers/stackplot_demo.html

# data from United Nations World Population Prospects (Revision 2019)
# https://population.un.org/wpp/, license: CC BY 3.0 IGO
year = [1950, 1960, 1970, 1980, 1990, 2000, 2010, 2018]
population = {
    'Asia': [1394, 1686, 2120, 2625, 3202, 3714, 4169, 4560],
    'Europe': [220, 253, 276, 295, 310, 303, 294, 293]
}
hatches = ['+', '.']
colors = ['tab:red', 'tab:blue']

fig, ax = plt.subplots(figsize=(12, 6))
areas = ax.stackplot(year, population.values(), labels=population.keys(), alpha=0.8, colors=colors)
# hatch each area separately
for a, h in zip(areas, hatches):
    a.set_hatch(h)
# show a big legend
ax.legend(loc='upper left', reverse=True, prop={'size': 24})
ax.set_title('Population', fontsize=20)
ax.set_xlabel('Year', fontsize=20)
ax.set_ylabel('Number of people (millions)', fontsize=20)
fig.tight_layout()
plt.show()

The outcome:

enter image description here

For comparison, this is how the same plot would look like with the default legend size:

enter image description here

Not the answer you're looking for? Browse other questions tagged or ask your own question.