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 Plot in Python With Examples A matplotlib quiver plot is basically something that helps in displaying the velocity vectors as arrows with the components (u, v) at the points (x, y). To plot the coordinates specified above, we can use the following command in each corresponding pair of the elements present in x and y.
To plot the 2D field of arrows, we use the Quiver plot in the matplotlib library. This plot mainly helps in displaying the velocity vectors as arrows having components (u,v) at the points (x,y). The Quiver plots are useful for Electrical engineers to visualize electrical potential and for Mechanical engineers to show stress gradients.
Plot a 2D field of arrows using quiver () method. To animate the quiver, we can change the u and v values, in animate () method. Update the u and v values and the color of the vectors.
Shaft width in arrow units; default depends on choice of units, above, and number of vectors; a typical starting value is about 0.005 times the width of the plot. Head width as multiple of shaft width. Head length as multiple of shaft width.
@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.
Just use Python's loop and the width
parameter. This will be slow for large data.
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()
This is only a workaround, but linewidths
can be used if we set 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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With