2016-04-26 158 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