3

I have made the following plot with the code below. I would like to create a horizontal red dashed line running across all the facetplots to highlight all points above 200, but when I run this

plt.axhline(200, ls='--', linewidth=3, color='red')

I only get the line in the last plot. I am guessing I need to loop through all the plots but i am not sure how to do that. Thanks for the help.

enter image description here

g = sns.relplot(x='hour', y="n",
                 col="w_day", hue="Zone",
                 kind="scatter", ci=95, data=df_1, col_order=col_order)

axes = g.axes.flatten()
axes[0].set_title("Monday")
axes[1].set_title("Tuesday")
axes[2].set_title("Wednesday")
axes[3].set_title("Thursday")
axes[4].set_title("Friday")
axes[5].set_title("Saturday")
axes[6].set_title("Sunday")

axes[0].set_ylabel("Hourly N")
for ax in axes:
    ax.set_xlabel("Hour")

g.fig.suptitle('', 
               weight='semibold', 
               y= 1.06, 
               size='x-large')

plt.axhline(200, ls='--', linewidth=3, color='red')


plt.margins(x=0)
plt.subplots_adjust(hspace=0, wspace=0)
ojp
  • 973
  • 1
  • 11
  • 26

1 Answers1

5
for ax in axes:
    ax.axhline(200, ls='--', linewidth=3, color='red')
Diziet Asahi
  • 38,379
  • 7
  • 60
  • 75
  • Is there a way of adding this horizontal line to the legend? – Hawklaz Dec 09 '20 at 13:01
  • 1
    @Hawklaz not easily, no. Because `relplot` uses a `FacetGrid`, the legend is created differently than the classical matplotlib stuff. If you post a question with a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) for your own usecase, someone might find a solution – Diziet Asahi Dec 09 '20 at 13:29