Home > database >  making a small tick mark across y-axis and annotating it with a text in matplotlib
making a small tick mark across y-axis and annotating it with a text in matplotlib

Time:04-14

I am trying to plot a figure with a small tick mark across a particular point in y - axis and label that point. Here is a sample plot:

df = pd.DataFrame({'x': np.arange(10),
                   'y': np.arange(100, 200, 10)})

fig,ax = plt.subplots(figsize =(4.5,4.5))
ax.plot(df.x,df.y)

plt.xlabel('x')
plt.ylabel('y')
plt.xlim(0,10)
plt.ylim(100,200)

plt.annotate(r'$y_{i}$',\
             xy = (0.2,150),
             xytext = (-0.75,150))          

ax.axhline(y=150, xmin=0, xmax=0.02)
plt.savefig("plot/sample plot.png", dpi = 1000)
plt.show()
plt.clf()

with this code I could produce following plot: enter image description here

However, I want the tick marks across the y-axis as follows: enter image description here

Any help would be highly appreciated. Thanks!

CodePudding user response:

Since the artist outside of the Axes is clipped, you can set clip_on to False, and set xmin to a negative value, such as:

ax.axhline(y=150, xmin=-0.02, xmax=0.02, clip_on=False)
  • Related