Home > Enterprise >  How to add a vertical line to a seaborn pointplot
How to add a vertical line to a seaborn pointplot

Time:10-16

I want to add a vertical line in sns.pointplot(), which has datetime xticks, I used ax.axvline, but the position of the line was wrong.

my dataframe like this:

city year PM
c1 2011 2
c1 2012 2
c1 2013 3
c2 2011 3
c2 2012 2
c2 2013 3

my code like this:

df['year']=pd.to_datetime(df['year'])

fig,ax=plt.subplots()

g=sns.pointplot(x='year',
          y='PM',
          data=df,
          ax=ax,
          color='black',
          scale=0.5,
          capsize=0.2,
          errwidth=0.8
          )


ax.axvline(x=pd.Timestamp('2012-09-01 00:00:00'),
        ymin=0,ymax=1,
        #c="red",
        ls='--',
        linewidth=0.8,zorder=0, clip_on=False)

I got this:

CodePudding user response:

  • enter image description here

  • Related