Python 在图形的坐标系中设置轴标签,而不是轴

Python 在图形的坐标系中设置轴标签,而不是轴,python,matplotlib,Python,Matplotlib,我想使用图形的坐标系而不是轴来设置轴标签的坐标(或者,如果这不可能,至少可以使用一些绝对坐标系) 换言之,对于这两个示例,我希望标签位于相同的位置: 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_coor

我想使用图形的坐标系而不是轴来设置轴标签的坐标(或者,如果这不可能,至少可以使用一些绝对坐标系)

换言之,对于这两个示例,我希望标签位于相同的位置:

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()
这在matplotlib中可能吗


是的。可以使用变换从一个坐标系转换到另一个坐标系。这里有一个深入的解释:

如果要使用地物坐标,首先需要将地物坐标转换为显示坐标。你可以用fig.transFigure来做这个。稍后,当您准备绘制轴时,可以使用ax.transAxes.inversed()从显示转换为轴


正是我想要的。非常感谢。
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()