2016-04-26 207 views
0

我在网格上创建了​​一个包含多个子图的图。这些图在两个参数上有所不同,所以我希望它看起来像是在一个坐标系中下的。直接在matplotlib图上绘制箭头

我设法在图上直接使用subplot旁边的matplotlib.lines.Line2D()来绘制线条。 但我宁愿有一个箭头,而不是一条线,使其更清晰。 (我可以用fig.text添加特定的参数值()。)

I'd like the blue lines to be arrows

import matplotlib as mpl 
import matplotlib.pyplot as plt 
import numpy as np 
from itertools import product 

fig = plt.figure() 
plotGrid = mpl.gridspec.GridSpec(2, 2) 

x = np.linspace(0,10,10000) 
y = [j* np.sin(x + i) for i,j in product(range(2), range(1,3))] 

for i in range(4): 
    ax = plt.Subplot(fig, plotGrid[i]) 

    for sp in ax.spines.values(): 
     sp.set_visible(False) 

    ax.plot(x,y[i], color = 'r') 
    ax.set_xticks([]) 
    ax.set_yticks([]) 
    fig.add_subplot(ax) 

all_axes = fig.get_axes() 


#I would like these lines to be arrows 
blcorPosn = 0.08 #bottom corner position 
l1 = mpl.lines.Line2D([blcorPosn,blcorPosn], [1, blcorPosn], 
         transform=fig.transFigure, fig) 

l2 = mpl.lines.Line2D([blcorPosn, 1], [blcorPosn, blcorPosn], 
         transform=fig.transFigure, fig) 

fig.lines.extend([l1, l2]) 

我不知道这是否是要走的路。但是现在我花了一天的时间在这上面,我目前看到的绘制箭的唯一方法是直接在轴上绘制它们,但对我而言,这并不是我所能想象的。

这也是我在这里的第一篇文章,所以如何提问的建议是高度赞赏。 谢谢

+0

http://matplotlib.org/examples/pylab_examples/arrow_simple_demo.html –

+0

另一件事,也许,只是也许会帮助你: http://matplotlib.org/users/pyplot_tutorial.html #标注文本 – Drachenfels

回答

0

你可以用一个稍微修改的FancyArrow补丁修改沿着每个轴的Line2D。主要区别在于起源和目的地x,y的坐标被替换为原点x,y和要绘制的距离x,y。的值被作为参数还直接传递,而不是作为列表:

l1 = mpl.patches.FancyArrow(blcorPosn, blcorPosn, 1, 0, 
          transform=fig.transFigure, figure=fig) 

l2 = mpl.patches.FancyArrow(blcorPosn, blcorPosn, 0, 1, 
          transform=fig.transFigure, figure=fig) 

FancyArrow补丁接受几个其它参数,以允许自定义的箭头的外观,包括width(对于线宽度)head_widthhead_length

Figure with axis arrows