2012-05-07 24 views
3

使用matplotlib,我製作一個包含兩個Axes對象(即兩組xy軸)的圖形。我想連接兩個點---一個是從一個軸上拾取的,另一個是從另一個軸上拾取---用箭頭或線條連接。通過matplotlib中的箭頭連接軸中的點和另一個軸中的另一個點

我試圖通過使用annotate()函數和ConnectionPatch對象來做到這一點,但在兩種方式中,箭頭的一部分被軸的「框架」隱藏。請參閱附圖,其中我嘗試通過ConnectionPatch對象連接兩個軸的原點。

我也附上用於生成圖的腳本。

有沒有辦法'提出'箭頭(或將軸框向後推)?

Trying to connect the origins of the two axes.

#!/usr/bin/python 
# 
# This script was written by Norio TAKEMOTO 2012-5-7 


import matplotlib.pyplot as plt 
from matplotlib.patches import ConnectionPatch 

# creating a figure and axes. 
fig=plt.figure(figsize=(10,5)) 

ax1=plt.axes([0.05,0.15,0.40,0.80]) 

plt.xticks([0]) 
plt.yticks([0]) 
plt.xlim((-1.23, 1.23)) 
plt.ylim((-2.34, 2.34)) 


ax2=plt.axes([0.60,0.15, 0.30, 0.30]) 

plt.xticks([0]) 
plt.yticks([0]) 
plt.xlim((-3.45, 3.45)) 
plt.ylim((-4.56, 4.56)) 


# trying to connect the point (0,0) in ax1 and the point (0,0) in ax2 
# by an arrow, but some part is hidden. I can't find a solution. Let's 
# ask stackoverflow. 

#xy_A_ax1=(0,0) 
#xy_B_ax2=(0,0) 
# 
#inv1 = ax1.transData.inverted() 
#xy_B_display = ax2.transData.transform(xy_B_ax2) 
#xy_B_ax1  = inv1.transform(xy_B_display) 
#ax1.annotate('Wundaba', xy=(0, 0), xytext=xy_B_ax1, 
#    xycoords='data',textcoords='data', 
#    arrowprops=dict(arrowstyle='->')) 


con = ConnectionPatch(xyA=(0, 0), xyB=(0, 0), 
         coordsA='data', coordsB='data', 
         axesA=ax1, axesB=ax2, 
         arrowstyle='->', clip_on=False) 
ax1.add_artist(con) 

plt.savefig('fig1.eps') 
plt.savefig('fig1.png') 

回答

3

一個簡單的方法是透明的參數設置爲savefig(),即plt.savefig('fig1.png', transparent=1)

enter image description here

,或者就在第二個圖形使用透明度:

ax2.patch.set_facecolor('None')

as line 21.

+0

謝謝你的回答。兩者都爲我工作。但是,我仍然想知道一種方法來控制箭頭和軸框架的「zorder」。如果我找到這樣的方法,我會稍後在這裏報告。 – norio

相關問題