Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set axes label in coordinate system of figure rather than axes

I would like to use the coordinate system of the figure rather than the axis to set the coordinates of the axes label (or if this is not possible at least some absolute coordinate system).

In other words I would like to the label at the same position for this two examples:

import matplotlib.pyplot as plt
from pylab import axes

plt.figure().show()
ax = axes([.2, .1, .7, .8])
ax.plot([1, 2], [1, 2])
ax.set_ylabel('BlaBla')
ax.yaxis.set_label_coords(-.1, .5)
plt.draw()

plt.figure().show()
ax = axes([.2, .1, .4, .8])
ax.plot([1, 2], [1, 2])
ax.set_ylabel('BlaBla')
ax.yaxis.set_label_coords(-.1, .5)

plt.draw()
plt.show()

Is this possible in matplotlib?

Illustrate difference

like image 390
magu_ Avatar asked Oct 20 '22 07:10

magu_


1 Answers

Yes. You can use transforms to translate from one coordinate system to another. There's an in-depth explanation here: http://matplotlib.org/users/transforms_tutorial.html

If you want to use figure coordinates, first you'll need to transform from figure coordinates to display coordinates. You can do this with fig.transFigure. Later, when you're ready to plot the axis, you can transform from display to axes using ax.transAxes.inverted().

import matplotlib.pyplot as plt
from pylab import axes

fig = plt.figure()
coords = fig.transFigure.transform((.1, .5))
ax = axes([.2, .1, .7, .8])
ax.plot([1, 2], [1, 2])
axcoords = ax.transAxes.inverted().transform(coords)
ax.set_ylabel('BlaBla')
ax.yaxis.set_label_coords(*axcoords)
plt.draw()

plt.figure().show()
coords = fig.transFigure.transform((.1, .5))
ax = axes([.2, .1, .4, .8])
ax.plot([1, 2], [1, 2])
ax.set_ylabel('BlaBla')
axcoords = ax.transAxes.inverted().transform(coords)
ax.yaxis.set_label_coords(*axcoords)

plt.draw()
plt.show()
like image 156
Amy Teegarden Avatar answered Oct 22 '22 00:10

Amy Teegarden