Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Any help would be highly appreciated.
Thanks!

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading