Label the axes in the coordinate system of the shape, not the axis

I would like to use the coordinate system of the figure, not the axis, to set the coordinates of the axis labels (or if this is not possible, at least in some kind of absolute coordinate system).

In other words, I would like the label to be in the same position for these 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

+5
source share
1 answer

Yes. You can use conversions to translate from one coordinate system to another. There is a detailed explanation here: http://matplotlib.org/users/transforms_tutorial.html

If you want to use the coordinates of the figures, first you will need to convert from the coordinates of the figure to the coordinates. You can do this with fig.transFigure. Later, when you are ready to plot an axis, you can convert it from a display to an axis 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() 
+2
source

All Articles