2015-07-10 81 views
5

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

换句话说,我想在标签在此两个例子相同的位置:

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?

Illustrate difference

回答

2

是的。您可以使用变换将坐标系转换为另一个坐标系。这里有一个深入的解释: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() 
+0

正是我在找的东西。谢谢。 –