Matplotlib スパン線とベクトル場関数
スパン線関数は、グラフ内に Axes 全体にわたる参照線と色付き帯を追加するために使用されます。ベクトル場関数は、矢印図と流線図を描画するために使用されます。
関数一覧
| 関数 | 機能 |
|---|---|
| axhline() | Axes 全体にわたる水平線を追加 |
| axhspan() | Axes 全体にわたる水平の色付き帯を追加 |
| axvline() | Axes 全体にわたる垂直線を追加 |
| axvspan() | Axes 全体にわたる垂直の色付き帯を追加 |
| axline() | 2点を通る無限長の直線を追加 |
| 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 で2点を通る無限直線を定義
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 で2点を通る無限直線を定義
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()
その他の拡張