简体   繁体   English

在matplotlib的右yaxis上添加刻度标签

[英]Add tick labels on right yaxis in matplotlib

I have the following graph: 我有以下图形: Matplotlib图

I'd like to add custom ticks with labels on the right hand side of the graph, to identify the dashed horizontal lines. 我想在图表的右侧添加带有标签的自定义刻度线,以标识水平虚线。 How can I do that? 我怎样才能做到这一点?

ax = gca()
ax.axhline(.5, linestyle='--')
trans = matplotlib.transforms.blended_transform_factory(
    ax.transAxes,
    ax.transData)

ax.annotate('label', xy=(1.01, .5), xycoords=trans, clip_on=False, va='center')
ax.set_xlim([0,2])
plt.draw()

See here for details on blended transforms. 有关混合变换的详细信息,请参见此处 The x coordinate in is axis units (so it will always be just a tad off to the right, and the y-coordinate is is data units so you can put it exactly where you want. There isn't much point in putting in ticks on the right because you dashed lines will cover them up. 输入的x坐标是轴单位(因此,它总是向右偏移一点,而y坐标是数据单位,因此您可以将其精确地放置在所需的位置。在右边,因为虚线会掩盖它们。

If you want a new scale, use twinx() . 如果要使用新的比例尺,请使用twinx()

fig = plt.figure()
ax = []
ax.append(fig.add_subplot(111))
ax.append(ax[0].twinx())
ax[0].plot(...)
ax[1].set_yticks([...])
ax[1].set_yticklabels([...])
plt.show()

If you want just a label, use a text thingy, as @tcaswell wrote. 如果你想只是一个标签,使用text啄,作为@tcaswell写道。

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

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