Matplotlib Span Line and Vector Field Functions
Matplotlib Reference Documentation
Span functions are used to add reference lines and shaded bands that span the entire Axes; vector field functions are used to draw quiver plots and streamplots.
Function overview
| function | Function |
|---|---|
| axhline() | Add a horizontal line spanning the Axes |
| axhspan() | Add a horizontal shaded band spanning the Axes |
| axvline() | Add a vertical line spanning the Axes |
| axvspan() | Add a vertical shaded band spanning the Axes |
| axline() | Add an infinitely long straight line passing through two points |
| quiver() | Plot a vector field (quiver chart) |
| quiverkey() | Add a scale legend to the vector field |
| barbs() | Draw a barbs plot (for meteorology) |
| streamplot() | Plot a streamplot |
Span functions
axhline() / axvline()
matplotlib.pyplot.axhline(y=0, xmin=0, xmax=1, **kwargs)
matplotlib.pyplot.axvline(x=0, ymin=0, ymax=1, **kwargs)
axhspan() / axvspan()
matplotlib.pyplot.axhspan(ymin, ymax, xmin=0, xmax=1, **kwargs)
matplotlib.pyplot.axvspan(xmin, xmax, ymin=0, ymax=1, **kwargs)
axline()
matplotlib.pyplot.axline(xy1, xy2=None, *, slope=None, **kwargs)
Example
import
matplotlib.
pyplot
as
plt
import
numpy
as
np
x
=
np.
linspace
(
0
,
10
,
100
)
y
=
np.
sin
(
x
)
fig
,
(
ax1
,
ax2
)
=
plt.
subplots
(
1
,
2
,
figsize
=
(
12
,
5
)
,
layout
=
'constrained'
)
Left plot: span curve
ax1.
plot
(
x
,
y
,
'k-'
,
alpha
=
0.7
)
ax1.
axhline
(
y
=
0
,
color
=
'gray'
,
linewidth
=
0.8
)
y=0 horizontal line
ax1.
axhline
(
y
=
0.5
,
color
=
'green'
,
linestyle
=
'--'
)
y=0.5
ax1.
axvline
(
x
=
np.
pi
,
color
=
'red'
,
linestyle
=
'--'
)
x=π
ax1.
axvspan
(
np.
pi
,
2
*np.
pi
,
alpha
=
0.15
,
Shaded band
color
=
'red'
,
label
=
'Phase 2'
)
ax1.
axhspan
(
0.5
,
1.0
,
xmin
=
0.6
,
xmax
=
1.0
,
Local shaded band
alpha
=
0.15
,
color
=
'blue'
)
ax1.
set_title
(
'Span Functions'
)
ax1.
legend
(
)
Right plot: axline defines an infinite straight line through two points
ax2.
plot
(
[
0
,
2
,
4
]
,
[
0
,
2
,
3
]
,
'ro'
,
label
=
'Data points'
)
ax2.
axline
(
(
0
,
0
)
,
(
2
,
2
)
,
color
=
'blue'
,
linestyle
=
'--'
,
label
=
'Through (0,0) and (2,2)'
)
ax2.
axline
(
(
0
,
0
)
,
slope
=
1.5
,
color
=
'green'
,
linestyle
=
':'
,
label
=
'From (0,0) with slope=1.5'
)
ax2.
set_xlim
(
1
,
5
)
ax2.
set_ylim
(
1
,
5
)
ax2.
set_title
(
'axline()'
)
ax2.
legend
(
)
ax2.
grid
(
True
,
alpha
=
0.3
)
plt.
show
(
)
Vector field functions
quiver() - vector field
matplotlib.pyplot.quiver(*args, **kwargs)
# X, Y, U, V: grid coordinates and vector components
# C: Optional color data
streamplot() - streamplot
matplotlib.pyplot.streamplot(x, y, u, v, density=1,
linewidth=None, color=None, cmap=None, **kwargs)
barbs() - barbs plot
matplotlib.pyplot.barbs(*args, **kwargs)
# X, Y, U, V: grid coordinates and wind components
Example
import
matplotlib.
pyplot
as
plt
import
numpy
as
np
Create vector field data
x
=
np.
linspace
(
3
,
3
,
15
)
y
=
np.
linspace
(
3
,
3
,
15
)
X
,
Y
=
np.
meshgrid
(
x
,
y
)
Vortex field
U
=
-Y
V
=
X
Speed magnitude
speed
=
np.
sqrt
(
U**
2
- V**
2
)
fig
,
(
ax1
,
ax2
)
=
plt.
subplots
(
1
,
2
,
figsize
=
(
12
,
5
)
,
layout
=
'constrained'
)
Left plot: quiver arrow plot
q
=
ax1.
quiver
(
X
,
Y
,
U
,
V
,
speed
,
cmap
=
'viridis'
,
scale
=
50
,
width
=
0.005
)
fig.
colorbar
(
q
,
ax
=
ax1
,
label
=
'Speed'
)
ax1.
set_title
(
'quiver() - Arrow Field'
)
ax1.
set_aspect
(
'equal'
)
Right plot: streamplot
strm
=
ax2.
streamplot
(
x
,
y
,
U
,
V
,
color
=
speed
,
cmap
=
'plasma'
,
density
=
1.5
,
linewidth
=
1.5
)
fig.
colorbar
(
strm.
lines
,
ax
=
ax2
,
label
=
'Speed'
)
ax2.
set_title
(
'streamplot() - Streamlines'
)
ax2.
set_aspect
(
'equal'
)
plt.
show
(
)
barbs() example
Example
import
matplotlib.
pyplot
as
plt
import
numpy
as
np
Simulated wind field data
x
=
np.
linspace
(
0
,
10
,
10
)
y
=
np.
linspace
(
0
,
8
,
8
)
X
,
Y
=
np.
meshgrid
(
x
,
y
)
Wind components (uniform eastward wind + random perturbation)
U
=
10
- np.
random
.
randn
(
*X.
shape
)
3
V
=
np.
random
.
randn
(
*X.
shape
)
5
fig
,
ax
=
plt.
subplots
(
figsize
=
(
8
,
5
)
,
layout
=
'constrained'
)
Barbs plot: the barbs indicate wind speed, the direction indicates wind direction
ax.
barbs
(
X
,
Y
,
U
,
V
,
length
=
6
)
ax.
set_title
(
'barbs() - Wind Barbs (Meteorological)'
)
ax.
set_xlabel
(
'Longitude'
)
ax.
set_ylabel
(
'Latitude'
)
ax.
set_xlim
(
1
,
11
)
ax.
set_ylim
(
1
,
9
)
plt.
show
(
)