我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()
Run Code Online (Sandbox Code Playgroud)
这给了我很好的箭头,但我怎样才能将它们的线条样式改为点线,虚线等?
Joe*_*ton 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()
Run Code Online (Sandbox Code Playgroud)

你得到了虚线箭头,但可能不是你的想法.
您可以使用一些参数来更接近,但它仍然看起来不太好看:
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()
Run Code Online (Sandbox Code Playgroud)

因此,另一种解决方法是使用阴影线:
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()
Run Code Online (Sandbox Code Playgroud)

| 归档时间: |
|
| 查看次数: |
4418 次 |
| 最近记录: |