Python 使用 matplotlib quiver 更改箭头的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15701952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change size of arrows using matplotlib quiver
提问by SirC
I am using quiver from matplotlib to plot a vectorial field. I would like to change the size of the thickness of each arrow depending on the number of data which produced a specific arrow of the vector field. Therefore what I am looking for is not a general scale transformation of the arrow size, but the way to customize the thickness of the arrow in quiver one-by-one. Is it possible? Can you help me?
我正在使用 matplotlib 中的 quiver 来绘制矢量场。我想根据产生矢量场特定箭头的数据数量来更改每个箭头的粗细大小。所以我要找的不是箭头大小的一般尺度变换,而是在quiver中逐个自定义箭头粗细的方法。是否可以?你能帮助我吗?
采纳答案by unutbu
The linewidthsparameter to plt.quivercontrols the thickness of the arrows. If you pass it a 1-dimensional array of values, each arrow gets a different thickness.
的linewidths参数来plt.quiver控制的箭头的厚度。如果传递给它一个一维数组,每个箭头的粗细都不同。
For example,
例如,
widths = np.linspace(0, 2, X.size)
plt.quiver(X, Y, cos(deg), sin(deg), linewidths=widths)
creates linewidths growing from 0 to 2.
创建从 0 增长到 2 的线宽。
import matplotlib.pyplot as plt
import numpy as np
sin = np.sin
cos = np.cos
# http://stackoverflow.com/questions/6370742/#6372413
xmax = 4.0
xmin = -xmax
D = 20
ymax = 4.0
ymin = -ymax
x = np.linspace(xmin, xmax, D)
y = np.linspace(ymin, ymax, D)
X, Y = np.meshgrid(x, y)
# plots the vector field for Y'=Y**3-3*Y-X
deg = np.arctan(Y ** 3 - 3 * Y - X)
widths = np.linspace(0, 2, X.size)
plt.quiver(X, Y, cos(deg), sin(deg), linewidths=widths)
plt.show()
yields
产量


回答by nekketsuuu
@unutbu's solution is not useful after matplotlib 2.0.0 (see this issueand this pull request). As of matplotlib 2.1.2, there seems to be no parameter of plt.quiverwhich officially supports one-by-one configuration of arrow widths. But some workarounds are remained.
@unutbu 的解决方案在 matplotlib 2.0.0 之后没有用(请参阅此问题和此拉取请求)。从 matplotlib 2.1.2 开始,似乎没有plt.quiver正式支持箭头宽度的一对一配置的参数。但仍有一些解决方法。
Method 1
方法一
Just use Python's loop and the widthparameter. This will be slow for large data.
只需使用 Python 的循环和width参数。这对于大数据来说会很慢。
import matplotlib.pyplot as plt
import numpy as np
# original code by user423805
# https://stackoverflow.com/a/6372413/5989200
xmax = 4.0
xmin = -xmax
D = 20
ymax = 4.0
ymin = -ymax
for y in np.linspace(ymin, ymax, D):
for x in np.linspace(xmin, xmax, D):
deg = np.arctan(y ** 3 - 3 * y - x)
w = 0.005 * (y - ymin) / (ymax - ymin) # just example...
plt.quiver(x, y, np.cos(deg), np.sin(deg), width=w)
plt.show()
Method 2
方法二
This is only a workaround, but linewidthscan be used if we set edgecolors.
这只是一种解决方法,但linewidths如果我们设置edgecolors.
import matplotlib.pyplot as plt
import numpy as np
# original code by user423805
# https://stackoverflow.com/a/6372413/5989200
xmax = 4.0
xmin = -xmax
D = 20
ymax = 4.0
ymin = -ymax
x = np.linspace(xmin, xmax, D)
y = np.linspace(ymin, ymax, D)
X, Y = np.meshgrid(x, y)
deg = np.arctan(Y ** 3 - 3 * Y - X)
widths = np.linspace(0, 2, X.size)
plt.quiver(X, Y, np.cos(deg), np.sin(deg), linewidths=widths, edgecolors='k')
plt.show()
Note that efiring, one of maintainers of matplotlib, said:
请注意,matplotlib 的维护者之一 efiring说:
So please use the
widthkwarg together withunits;linewidthsis only for controlling the outline thickness, when an outline of a different color is explicitly requested.
所以请将
widthkwarg 与units;linewidths当明确要求不同颜色的轮廓时,仅用于控制轮廓粗细。

