2014-11-20 140 views
0

我在python 2.7中使用matplotlib。我正在试图在轴外的图形区域中创建一个箭头。在图形区域绘制带锐利边缘的箭头

from matplotlib.pyplot import * 

fig = figure() 
ax1 = fig.add_axes([.1,.1,.6,.8]) 

ax1.annotate('',xy=(.8,.92),xycoords='figure fraction',xytext=(.8,.1) 
      arrowprops=dict(arrowstyle='->',fc='k',lw=10)) 

ax2 = fig.add_axes([.85,.1,.1,.8]) 
ax2.spines['top'].set_visible(False) 
ax2.spines['bottom'].set_visible(False) 
ax2.spines['left'].set_visible(False) 
ax2.spines['right'].set_visible(False) 
ax2.tick_params(axis='both',which='both', 
       top='off',right='off',left='off',bottom='off', 
       labeltop='off',labelright='off',labelleft='off',labelbottom='off') 

ax2.patch.set_facecolor('None') 

ax2.set_xlim(0,1) 
ax2.set_ylim(0,1) 
ax2.arrow(.5,0,0,1,fc='k',ec='k',head_width=.25, 
      head_length=.05,width=.15,length_includes_head=True)  
show() 

使用

ax1.annotate(...) 

给了我一个 '模糊' 看箭头。我能弄清楚如何得到一个更好的看箭头的唯一方法是通过创建另一个轴只是为了添加箭头,使用

ax2.arrow(...) 

(网站不会让我张贴图片,但复制和粘贴代码你会看到我在说什么) 还有一个更好的方式来做到这一点,虽然...

回答

0

我认为改变箭头风格将在这里帮助。例如,将其从'->'更改为'simple'可以提供更好看的箭头。您可以通过玩mutation_scale来改变宽度。例如,

ax1.annotate('',xy=(.8,.92),xycoords='figure fraction',xytext=(.8,.1), 
    arrowprops=dict(arrowstyle="simple",fc="k", ec="k",mutation_scale=30)) 

这是你的脚本,上述simple箭头蓝色绘制。请注意与黑色箭头的区别->箭头与annotate

from matplotlib.pyplot import * 

fig = figure() 
ax1 = fig.add_axes([.1,.1,.5,.8]) 

# Your original arrow (black) 
ax1.annotate('',xy=(.7,.92),xycoords='figure fraction',xytext=(.7,.1), 
      arrowprops=dict(arrowstyle='->',fc='k',lw=10)) 

# "Simple" arrow (blue) 
ax1.annotate('',xy=(.8,.92),xycoords='figure fraction',xytext=(.8,.1), 
      arrowprops=dict(arrowstyle="simple",fc="b", ec="k",mutation_scale=30)) 

ax2 = fig.add_axes([.85,.1,.1,.8]) 
ax2.spines['top'].set_visible(False) 
ax2.spines['bottom'].set_visible(False) 
ax2.spines['left'].set_visible(False) 
ax2.spines['right'].set_visible(False) 
ax2.tick_params(axis='both',which='both', 
       top='off',right='off',left='off',bottom='off', 
       labeltop='off',labelright='off',labelleft='off',labelbottom='off') 

ax2.patch.set_facecolor('None') 

ax2.set_xlim(0,1) 
ax2.set_ylim(0,1) 
ax2.arrow(.5,0,0,1,fc='r',ec='k',head_width=.25, 
      head_length=.05,width=.15,length_includes_head=True)  
show() 

enter image description here