Matplotlib 跨度线与矢量场函数
跨度线函数用于在图表中添加可横跨整个 Axes 的参考线和着色带;矢量场函数用于绘制箭头图和流线图。
函数一览
| 函数 | 功能 |
|---|---|
| axhline() | 添加横跨 Axes 的水平线 |
| axhspan() | 添加横跨 Axes 的水平着色带 |
| axvline() | 添加纵跨 Axes 的垂直线 |
| axvspan() | 添加纵跨 Axes 的垂直着色带 |
| axline() | 添加通过两点的无限长直线 |
| quiver() | 绘制矢量场(箭头图) |
| quiverkey() | 给矢量场添加标尺图例 |
| barbs() | 绘制风羽图(气象用) |
| streamplot() | 绘制流线图 |
Span 函数
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)
实例
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')
# 左图:span 曲线
ax1.plot(x, y, 'k-', alpha=0.7)
ax1.axhline(y=0, color='gray', linewidth=0.8) # y=0 水平线
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, # 着色带
color='red', label='Phase 2')
ax1.axhspan(0.5, 1.0, xmin=0.6, xmax=1.0, # 局部着色带
alpha=0.15, color='blue')
ax1.set_title('Span Functions')
ax1.legend()
# 右图:axline 通过两点定义无限直线
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()
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')
# 左图:span 曲线
ax1.plot(x, y, 'k-', alpha=0.7)
ax1.axhline(y=0, color='gray', linewidth=0.8) # y=0 水平线
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, # 着色带
color='red', label='Phase 2')
ax1.axhspan(0.5, 1.0, xmin=0.6, xmax=1.0, # 局部着色带
alpha=0.15, color='blue')
ax1.set_title('Span Functions')
ax1.legend()
# 右图:axline 通过两点定义无限直线
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()
矢量场函数
quiver() - 矢量场
matplotlib.pyplot.quiver(*args, **kwargs) # X, Y, U, V: 网格坐标和矢量分量 # C: 可选的颜色数据
streamplot() - 流线图
matplotlib.pyplot.streamplot(x, y, u, v, density=1,
linewidth=None, color=None, cmap=None, **kwargs)
barbs() - 风羽图
matplotlib.pyplot.barbs(*args, **kwargs) # X, Y, U, V: 网格坐标和风分量
实例
import matplotlib.pyplot as plt
import numpy as np
# 创建矢量场数据
x = np.linspace(-3, 3, 15)
y = np.linspace(-3, 3, 15)
X, Y = np.meshgrid(x, y)
# 旋涡场
U = -Y
V = X
# 速度大小
speed = np.sqrt(U**2 + V**2)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5),
layout='constrained')
# 左图:quiver 箭头图
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')
# 右图: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()
import numpy as np
# 创建矢量场数据
x = np.linspace(-3, 3, 15)
y = np.linspace(-3, 3, 15)
X, Y = np.meshgrid(x, y)
# 旋涡场
U = -Y
V = X
# 速度大小
speed = np.sqrt(U**2 + V**2)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5),
layout='constrained')
# 左图:quiver 箭头图
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')
# 右图: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() 示例
实例
import matplotlib.pyplot as plt
import numpy as np
# 模拟风场数据
x = np.linspace(0, 10, 10)
y = np.linspace(0, 8, 8)
X, Y = np.meshgrid(x, y)
# 风的分量(向东的均匀风 + 随机扰动)
U = 10 + np.random.randn(*X.shape) * 3
V = np.random.randn(*X.shape) * 5
fig, ax = plt.subplots(figsize=(8, 5), layout='constrained')
# 风羽图:风羽表示风速,方向表示风向
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()
import numpy as np
# 模拟风场数据
x = np.linspace(0, 10, 10)
y = np.linspace(0, 8, 8)
X, Y = np.meshgrid(x, y)
# 风的分量(向东的均匀风 + 随机扰动)
U = 10 + np.random.randn(*X.shape) * 3
V = np.random.randn(*X.shape) * 5
fig, ax = plt.subplots(figsize=(8, 5), layout='constrained')
# 风羽图:风羽表示风速,方向表示风向
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()
Matplotlib 参考文档
点我分享笔记