用matplotlib绘制虚线二维向量? [英] Plotting dashed 2D vectors with matplotlib?
问题描述
我正在使用 quiver
在matplotlib中绘制矢量:
I'm using quiver
to draw vectors in 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()
这给了我漂亮的箭头,但是如何将它们的线条样式更改为点线,虚线等?
which gives me nice arrows, but how can I change their line style to dotted, dashed, etc.?
推荐答案
啊!实际上,linestyle='dashed'
确实有效,只是箭袋箭头仅在默认情况下填充并且没有设置线宽.它们是补丁而不是路径.
Ah! Actually, linestyle='dashed'
does work, it's just that quiver arrows are only filled by default and don't have a linewidth set. They're patches instead of paths.
如果你这样做:
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()
您会看到虚线箭头,但可能与您的想法不尽相同.
You get dashed arrows, but probably not quite what you had in mind.
您可以调整一些参数以接近一点,但它看起来仍然不太好:
You can play around with some of the parameters to get a bit closer, but it still doesn't exactly look nice:
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()
因此,另一种解决方法是使用阴影线:
Therefore, another workaround would be to use hatches:
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()
这篇关于用matplotlib绘制虚线二维向量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!