我正在使用 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()
这给了我漂亮的箭头,但我怎样才能将它们的线条样式更改为点线、虚线等?
最佳答案
啊!实际上,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()
您得到虚线箭头,但可能与您的想法不尽相同。
您可以尝试使用一些参数来更接近一点,但它看起来仍然不太好:
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()
因此,另一种解决方法是使用影线:
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()
关于python - 使用 matplotlib 绘制虚线二维向量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15352129/