Charles Brunet
Charles Brunet

Reputation: 23120

Add tick labels on right yaxis in matplotlib

I have the following graph: Matplotlib graph

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?

Upvotes: 2

Views: 1650

Answers (2)

tacaswell
tacaswell

Reputation: 87356

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.

Upvotes: 5

Robbert
Robbert

Reputation: 2724

If you want a new scale, use 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.

Upvotes: 0

Related Questions