2015-07-10 95 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

正是我在找的東西。謝謝。 –