简体   繁体   English

在 y 轴上做一个小刻度线并用 matplotlib 中的文本对其进行注释

[英]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.我正在尝试 plot 一个在 y 轴上的特定点上有一个小刻度线的图形和 label 那个点。 Here is a sample plot:这是一个示例 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:使用此代码,我可以生成以下 plot: 在此处输入图像描述

However, I want the tick marks across the y-axis as follows:但是,我希望 y 轴上的刻度线如下所示: 在此处输入图像描述

Any help would be highly appreciated.任何帮助将不胜感激。 Thanks!谢谢!

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:由于Axes之外的artist被clip掉了,可以设置clip_on为False, xmin设置为负值,如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM