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

mag*_*gu_ 5 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_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()
Run Code Online (Sandbox Code Playgroud)

这在matplotlib中是否可行?

说明差异

Amy*_*den 3

是的。您可以使用变换从一种坐标系转换到另一种坐标系。这里有深入的解释:http ://matplotlib.org/users/transforms_tutorial.html

如果要使用图形坐标,首先需要从图形坐标转换为显示坐标。您可以使用Fig.transFigure 来完成此操作。稍后,当您准备好绘制轴时,可以使用 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()
Run Code Online (Sandbox Code Playgroud)