Matplotlib Triangular Mesh and Polar Coordinate Functions
Matplotlib Reference Documentation
Triangular mesh functions are used to plot and visualize data on unstructured grids, while polar coordinate functions plot in the polar coordinate system.
Function Overview
| Function | Description |
|---|---|
| triplot() | Draw a wireframe of an unstructured triangular mesh |
| tripcolor() | Draw a pseudocolor plot on a triangular mesh |
| tricontour() | Draw contour lines on a triangular mesh |
| tricontourf() | Draw filled contour lines on a triangular mesh |
| polar() | Plot in the polar coordinate system (equivalent to subplot_kw projection='polar') |
Triangular Mesh Functions
triplot() - Mesh Wireframe
matplotlib.pyplot.triplot(*args, **kwargs) Axes.triplot(triangulation, **kwargs) # triangulation: Triangulation 对象(由 x, y 坐标创建)
tripcolor() - Triangular Pseudocolor
matplotlib.pyplot.tripcolor(*args, **kwargs) Axes.tripcolor(triangulation, C=None, **kwargs)
tricontour() / tricontourf() - Triangular Contour
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)
Example
import matplotlib.pyplot as plt
import matplotlib.tri as tri
import numpy as np
np.random.seed(42)
# Generate random scatter points
n = 200
x = np.random.rand(n) * 4 - 2
y = np.random.rand(n) * 4 - 2
# Function value at each point
z = x * np.exp(-x**2 - y**2)
# Create Delaunay triangulation
triang = tri.Triangulation(x, y)
fig, axes = plt.subplots(2, 2, figsize=(10, 8),
layout='constrained')
# Triangular mesh wireframe
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')
# Triangular pseudocolor
tc1 = axes[0, 1].tripcolor(triang, z, cmap='viridis',
shading='gouraud') # Smooth shading
fig.colorbar(tc1, ax=axes[0, 1], label='z value')
axes[0, 1].set_title('tripcolor()')
# Triangular contour
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()')
# Contour lines only
axes[1, 1].tricontour(triang, z, levels=15,
cmap='viridis', linewidths=1.5)
axes[1, 1].set_title('tricontour()')
plt.show()
import matplotlib.tri as tri
import numpy as np
np.random.seed(42)
# Generate random scatter points
n = 200
x = np.random.rand(n) * 4 - 2
y = np.random.rand(n) * 4 - 2
# Function value at each point
z = x * np.exp(-x**2 - y**2)
# Create Delaunay triangulation
triang = tri.Triangulation(x, y)
fig, axes = plt.subplots(2, 2, figsize=(10, 8),
layout='constrained')
# Triangular mesh wireframe
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')
# Triangular pseudocolor
tc1 = axes[0, 1].tripcolor(triang, z, cmap='viridis',
shading='gouraud') # Smooth shading
fig.colorbar(tc1, ax=axes[0, 1], label='z value')
axes[0, 1].set_title('tripcolor()')
# Triangular contour
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()')
# Contour lines only
axes[1, 1].tricontour(triang, z, levels=15,
cmap='viridis', linewidths=1.5)
axes[1, 1].set_title('tricontour()')
plt.show()
polar() - Polar Plot
matplotlib.pyplot.polar(*args, **kwargs) # 等同于: # plt.subplot(projection='polar') # ax.plot(theta, r)
| Parameter | Description |
|---|---|
| theta | Angle array (radians) |
| r | Radial distance array |
Example
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')
# Rose curve 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θ)|')
# Archimedean spiral r = θ
r2 = theta
axes[0, 1].plot(theta, r2, 'red', linewidth=2)
axes[0, 1].set_title('Archimedean Spiral: r=θ')
# Cardioid 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(θ)')
# Multiple petals
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()
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')
# Rose curve 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θ)|')
# Archimedean spiral r = θ
r2 = theta
axes[0, 1].plot(theta, r2, 'red', linewidth=2)
axes[0, 1].set_title('Archimedean Spiral: r=θ')
# Cardioid 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(θ)')
# Multiple petals
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() - Polar Grid
matplotlib.pyplot.rgrids(radii=None, labels=None, angle=None,
fmt=None, **kwargs)
matplotlib.pyplot.thetagrids(angles=None, labels=None, fmt=None,
**kwargs)
Example
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)
# Custom radial grid
ax.set_rticks([1, 3, 5, 7, 9]) # Radial tick positions
ax.set_rlabel_position(-22.5) # Radial label positions
# Custom angle grid
ax.set_thetagrids(np.arange(0, 360, 45), # Every 45 degrees
labels=['0°','45°','90°','135°','180°',
'225°','270°','315°'])
ax.set_title('Custom Polar Grid')
plt.show()
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)
# Custom radial grid
ax.set_rticks([1, 3, 5, 7, 9]) # Radial tick positions
ax.set_rlabel_position(-22.5) # Radial label positions
# Custom angle grid
ax.set_thetagrids(np.arange(0, 360, 45), # Every 45 degrees
labels=['0°','45°','90°','135°','180°',
'225°','270°','315°'])
ax.set_title('Custom Polar Grid')
plt.show()
Other Extensions