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中的颤动绘制矢量场。我想根据产生向量场的特定箭头的数据数量来改变每个箭头的厚度。因此,我所寻找的不是箭头尺寸的一般缩放变换,而是从一乘一的角度定制箭头的厚度的方法。是可能的吗?你能帮我吗?
2 个解决方案
#1
8
The linewidths
parameter to plt.quiver
controls the thickness of the arrows. If you pass it a 1-dimensional array of values, each arrow gets a different thickness.
对plt的线宽参数。箭筒控制箭筒的厚度。如果你给它一个一维的值数组,每个箭头都有不同的厚度。
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://*.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
收益率
#2
0
@unutbu's solution is not useful after matplotlib 2.0.0 (see this issue and this pull request). As of matplotlib 2.1.2, there seems to be no parameter of plt.quiver
which 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 width
parameter. This will be slow for large data.
只需使用Python的循环和宽度参数。对于大数据来说,这将是一个缓慢的过程。
import matplotlib.pyplot as plt
import numpy as np
# original code by user423805
# https://*.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 linewidths
can be used if we set edgecolors
.
这只是一个变通方法,但是如果我们设置edgecolors,则可以使用linewidths。
import matplotlib.pyplot as plt
import numpy as np
# original code by user423805
# https://*.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的维护人员efire说:
So please use the
width
kwarg together withunits
;linewidths
is only for controlling the outline thickness, when an outline of a different color is explicitly requested.所以请使用宽度kwarg和单位;线宽仅用于控制轮廓厚度,当显式要求不同颜色的轮廓时。
#1
8
The linewidths
parameter to plt.quiver
controls the thickness of the arrows. If you pass it a 1-dimensional array of values, each arrow gets a different thickness.
对plt的线宽参数。箭筒控制箭筒的厚度。如果你给它一个一维的值数组,每个箭头都有不同的厚度。
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://*.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
收益率
#2
0
@unutbu's solution is not useful after matplotlib 2.0.0 (see this issue and this pull request). As of matplotlib 2.1.2, there seems to be no parameter of plt.quiver
which 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 width
parameter. This will be slow for large data.
只需使用Python的循环和宽度参数。对于大数据来说,这将是一个缓慢的过程。
import matplotlib.pyplot as plt
import numpy as np
# original code by user423805
# https://*.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 linewidths
can be used if we set edgecolors
.
这只是一个变通方法,但是如果我们设置edgecolors,则可以使用linewidths。
import matplotlib.pyplot as plt
import numpy as np
# original code by user423805
# https://*.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的维护人员efire说:
So please use the
width
kwarg together withunits
;linewidths
is only for controlling the outline thickness, when an outline of a different color is explicitly requested.所以请使用宽度kwarg和单位;线宽仅用于控制轮廓厚度,当显式要求不同颜色的轮廓时。