2013-03-12 108 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