2013-03-12 195 views
6

我使用quiver繪製在matplotlib載體:用matplotlib繪製虛線2D矢量?

from itertools import chain 
import matplotlib.pyplot as pyplot 
pyplot.figure() 
pyplot.axis('equal') 
axis = pyplot.gca() 
axis.quiver(*zip(*map(lambda l: chain(*l), [ 
    ((0, 0), (3, 1)), 
    ((0, 0), (1, 0)), 
])), angles='xy', scale_units='xy', scale=1) 

axis.set_xlim([-4, 4]) 
axis.set_ylim([-4, 4]) 
pyplot.draw() 
pyplot.show() 

這給了我很好的箭,但如何改變自己的線條樣式,以點線,虛線,等等?

+1

'線型='dashed''應該工作,[根據文檔](http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.quiver)。但顯然這不起作用。這可能是一個錯誤。 – 2013-03-12 02:46:38

+0

@JoeKington::(對於解決方法有什麼建議嗎? – Mehrdad 2013-03-12 02:53:03

+0

不是我的頭頂,不幸的是... – 2013-03-12 03:07:38

回答

10

啊!實際上,linestyle='dashed'確實有效,只是箭頭只在默認情況下被填充,沒有線寬設置。他們是補丁而不是路徑。

如果你做這樣的事情:

import matplotlib.pyplot as plt 

fig, ax = plt.subplots() 
ax.axis('equal') 

ax.quiver((0,0), (0,0), (3,1), (1,0), angles='xy', scale_units='xy', scale=1, 
      linestyle='dashed', facecolor='none', linewidth=1) 

ax.axis([-4, 4, -4, 4]) 
plt.show() 

enter image description here

你得到虛線箭頭,但可能並不完全符合你腦子裏。

可以玩弄一些參數變得有點接近,但它仍然不是完全好看:

import matplotlib.pyplot as plt 

fig, ax = plt.subplots() 
ax.axis('equal') 

ax.quiver((0,0), (0,0), (3,1), (1,0), angles='xy', scale_units='xy', scale=1, 
      linestyle='dashed', facecolor='none', linewidth=2, 
      width=0.0001, headwidth=300, headlength=500) 

ax.axis([-4, 4, -4, 4]) 
plt.show() 

enter image description here

因此,另一種解決方法是使用艙口:

import matplotlib.pyplot as plt 

fig, ax = plt.subplots() 
ax.axis('equal') 

ax.quiver((0,0), (0,0), (3,1), (1,0), angles='xy', scale_units='xy', scale=1, 
     hatch='ooo', facecolor='none') 

ax.axis([-4, 4, -4, 4]) 
plt.show() 

enter image description here

+0

+1謝謝,總比沒有好,雖然還不夠理想哈哈 – Mehrdad 2013-03-12 03:51:52

+0

另一個問題:如果我們放大到不同的方面比例,箭頭變得扭曲。 – 2017-12-21 00:30:32