Matplotlib 三角メッシュと極座標関数


Matplotlib リファレンスドキュメント

三角メッシュ関数は非構造格子 (unstructured grid) 上でデータを描画・可視化するために使われ、極座標関数は極座標系でプロットします。

関数一覧

関数機能
triplot()非構造の三角メッシュのワイヤーフレームを描画
tripcolor()三角メッシュ上に疑似カラー図を描画
tricontour()三角メッシュ上に等高線を描画
tricontourf()三角メッシュ上に塗りつぶし等高線を描画
polar()極座標系でプロット(subplot_kw projection='polar' と同等)

三角メッシュ関数

triplot() - メッシュのワイヤーフレーム

matplotlib.pyplot.triplot(*args, **kwargs)
Axes.triplot(triangulation, **kwargs)
# triangulation: Triangulation 对象(由 x, y 坐标创建)

tripcolor() - 三角疑似カラー

matplotlib.pyplot.tripcolor(*args, **kwargs)
Axes.tripcolor(triangulation, C=None, **kwargs)

tricontour() / tricontourf() - 三角等高線

matplotlib.pyplot.tricontour(*args, **kwargs)
matplotlib.pyplot.tricontourf(*args, **kwargs)
Axes.tricontour(triangulation, C=None, levels=None, **kwargs)
Axes.tricontourf(triangulation, C=None, levels=None, **kwargs)

例

import matplotlib.pyplot as plt
import matplotlib.tri as tri
import numpy as np

np.random.seed(42)

# ランダムな散布点を生成
n = 200
x = np.random.rand(n) * 4 - 2
y = np.random.rand(n) * 4 - 2
# 各点の関数値
z = x * np.exp(-x**2 - y**2)

# Delaunay 三角分割を作成
triang = tri.Triangulation(x, y)

fig, axes = plt.subplots(2, 2, figsize=(10, 8),
                          layout='constrained')

# 三角メッシュのワイヤーフレーム
axes[0, 0].triplot(triang, 'k-', linewidth=0.5, alpha=0.5)
axes[0, 0].scatter(x, y, c=z, s=8, cmap='viridis')
axes[0, 0].set_title('triplot() - Mesh + Scatter')

# 三角疑似カラー
tc1 = axes[0, 1].tripcolor(triang, z, cmap='viridis',
                            shading='gouraud')  # スムーズなシェーディング
fig.colorbar(tc1, ax=axes[0, 1], label='z value')
axes[0, 1].set_title('tripcolor()')

# 三角等高線
tc2 = axes[1, 0].tricontourf(triang, z, levels=12,
                              cmap='RdYlBu')
axes[1, 0].tricontour(triang, z, levels=12,
                       colors='black', linewidths=0.3)
fig.colorbar(tc2, ax=axes[1, 0], label='z value')
axes[1, 0].set_title('tricontourf()')

# 等高線のみ
axes[1, 1].tricontour(triang, z, levels=15,
                       cmap='viridis', linewidths=1.5)
axes[1, 1].set_title('tricontour()')

plt.show()

polar() - 極座標プロット

matplotlib.pyplot.polar(*args, **kwargs)
# 等同于:
# plt.subplot(projection='polar')
# ax.plot(theta, r)
パラメータ説明
theta角度配列(ラジアン)
r動径距離配列

例

import matplotlib.pyplot as plt
import numpy as np

theta = np.linspace(0, 2*np.pi, 100)

fig, axes = plt.subplots(2, 2, figsize=(10, 10),
    subplot_kw={'projection': 'polar'},
    layout='constrained')

# バラ曲線 r = sin(3θ)
r1 = np.abs(np.sin(3 * theta))
axes[0, 0].plot(theta, r1, 'blue', linewidth=2)
axes[0, 0].set_title('Rose Curve: r=|sin(3θ)|')

# アルキメデスの螺旋 r = θ
r2 = theta
axes[0, 1].plot(theta, r2, 'red', linewidth=2)
axes[0, 1].set_title('Archimedean Spiral: r=θ')

# カージオイド r = 1 + cos(θ)
r3 = 1 + np.cos(theta)
axes[1, 0].fill(theta, r3, alpha=0.5, color='coral')
axes[1, 0].plot(theta, r3, 'red', linewidth=2)
axes[1, 0].set_title('Cardioid: r=1+cos(θ)')

# 複数の花びら
r4 = np.sin(4 * theta)
axes[1, 1].fill(theta, np.abs(r4), alpha=0.5, color='purple')
axes[1, 1].plot(theta, np.abs(r4), 'purple', linewidth=2)
axes[1, 1].set_rticks([0.5, 1.0])
axes[1, 1].set_title('4-petal Rose: r=|sin(4θ)|')

plt.show()

rgrids() / thetagrids() - 極座標グリッド

matplotlib.pyplot.rgrids(radii=None, labels=None, angle=None,
    fmt=None, **kwargs)
matplotlib.pyplot.thetagrids(angles=None, labels=None, fmt=None,
    **kwargs)

例

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots(subplot_kw={'projection': 'polar'},
                        figsize=(6, 6), layout='constrained')

theta = np.linspace(0, 2*np.pi, 100)
ax.plot(theta, theta/3, linewidth=2)

# カスタム動径グリッド
ax.set_rticks([1, 3, 5, 7, 9])      # 動径目盛位置
ax.set_rlabel_position(-22.5)         # 動径ラベル位置

# カスタム角度グリッド
ax.set_thetagrids(np.arange(0, 360, 45),  # 45 度ごと
                  labels=['0°','45°','90°','135°','180°',
                          '225°','270°','315°'])

ax.set_title('Custom Polar Grid')
plt.show()

Matplotlib リファレンスドキュメント

その他の拡張