2017-04-27 55 views
1

我試圖在matplotlib中創建一組Axes.annotate()。我希望這些註釋偏離點(類似於textcoords='offset pixels'),但在‘axes fraction’而不是絕對像素數。註釋點偏移軸的大小的一個固定分數

使用數據座標的問題是,當使用對數(或其他)尺度時,它會變得雜亂。

offset pixels的問題在於,如果更改圖形的大小或dpi,外觀會發生變化。

下面是概括的問題的嘗試:

fig, axs = plt.subplots(1,2) 
axs[0].plot([1,2],[10,100]) 
axs[1].semilogy([1,2],[10,100]) 
for ax in axs: 
    ax.annotate('', xy=(1,10), xytext=(1,50), textcoords=('data', 'offset pixels'),arrowprops={'arrowstyle':'-', 'lw':3}) 
plt.show() 

fig, axs = plt.subplots(1,2, dpi=200) 
axs[0].plot([1,2],[10,100]) 
axs[1].semilogy([1,2],[10,100]) 
for ax in axs: 
    ax.annotate('', xy=(1,10), xytext=(1,50), textcoords=('data', 'offset pixels'),arrowprops={'arrowstyle':'-', 'lw':3}) 
plt.show() 

enter image description here enter image description here

我想有註解行具有相同的長度(相對於圖的大小),不管圖形的大小,還是軸的縮放比例。

這可能嗎?

回答

2

我設法通過Matplotlib's Transformation Tutoria升,其中已經包含了我所需要的一切尋找解決我的問題。

要繪製相對於座標軸長度恆定的東西,我使用了Axes.transLimits對象。但之前,我必須警惕我處理對數座標軸的可能性,因此我還會使用對象來考慮座標軸的(可能的)非線性縮放比例。

我在最後使用的代碼是獲取我的初始點的「軸」座標,然後從該點開始簡單繪圖到該點的某個偏移量,指定我的座標用軸表示座標系

xax,yax = ax.transLimits.transform(ax.transScale.transform([x,y])) 
ax.plot([xax,xax], [y,y+offset], transform=ax.transAxes) 
0

如果要以軸單位爲單位指定註釋偏移量,請執行此操作。以下產生lentgh 20%軸分數線。

ax.annotate('', xy=(1,10), xytext=(1,0.2), textcoords=('data', 'axes fraction') 

enter image description here enter image description here

+0

使用「軸分數」作爲你沒有指定的行的端部的,所述y座標應爲座標軸的高度,這是不一樣的20%之說的長度該線應該是20%高,還是我誤解了? –