Skip to content
Advertisement

matplotlib: draw a box on axes

import matplotlib.patches as patches

df2 = pd.melt(
    data2.query('worktype_id!=38')[['period','worktype', 'utilization_billable', 'utilization' ]], 
    id_vars=['period', 'worktype']).sort_values(['period', 'variable'], ascending=[True, True])
g = sns.relplot(x='period', y='value', 
            hue='variable', data=df2, col='worktype', kind='line', col_wrap=4, 
            )
for ax in g.axes:
    ax.axhline(y=75, color='g', linestyle='--')
    ax.axhline(y=90, color='r', linestyle='--')
    rect = plt.Rectangle((10,10),10,10,linewidth=3,edgecolor='r',facecolor='red', alpha=1)
    ax.add_patch(rect)

I want to draw the box between y=75 and 90 (green and red lines on the chart), but nothing is shown up when I do ax.add_patch

this is the image I get:

enter image description here

So How do I fill out the area between red and green horizontal lines?

Advertisement

Answer

If you have a fixed area you want to fill and you want to do it across the entire chart, you are probably better off using axvspan. E.g.

ax.axvspan(75, 90, facecolor='red')
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement