现在位置: 首页 > Matplotlib 教程 > 正文

Matplotlib 三角网格与极坐标函数


Matplotlib 参考文档 Matplotlib 参考文档

三角网格函数用于在非结构化网格上绘制和可视化数据,极坐标函数在极坐标系中绘图。

函数一览

函数功能
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 参考文档 Matplotlib 参考文档