python - 使用 matplotlib 绘制虚线二维向量?

标签 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()

这给了我漂亮的箭头,但我怎样才能将它们的线条样式更改为点线、虚线等?

最佳答案

啊!实际上,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

关于python - 使用 matplotlib 绘制虚线二维向量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15352129/

相关文章:

python - Pandas:如何将系列的多重索引折叠为日期时间索引?

python - 在 python 中绘制二元 3D 矩阵

python - 在循环的每次传递处显示子图

python - 使用 BeautifulSoup 中的 nextSibling 什么都不输出

python - 从当前文件相对导入

python - 使用正则表达式从字符串中提取子字符串

python - matplotlib add_subplot 奇数个地 block

python - 如何强制 PyPlot 在条形图中显示空箱

python - Matplotlib 子图图形大小

python - matplotlib 直方图上的第一个和最后一个条出现移动