使用matplotlib绘制虚线2D矢量?

时间:2013-03-12 02:14:24

标签: python matplotlib

我正在使用quiver在matplotlib中绘制矢量:

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 个答案:

答案 0 :(得分: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