Matplotlib Triangular Mesh and Polar Coordinate Functions
Matplotlib Reference Documentation
Triangulation functions are used to plot and visualize data on unstructured grids, while polar functions plot in polar coordinates.
Function overview
| function | Function |
|---|---|
| triplot() | Draw the wireframe of an unstructured triangular mesh |
| tripcolor() | Draw a pseudocolor plot on a triangular mesh |
| tricontour() | Plot contours on a triangular mesh |
| tricontourf() | Plot filled contours on a triangular mesh |
| polar() | Plot in polar coordinates (equivalent to subplot_kw projection='polar') |
Triangulation functions
triplot() - mesh wireframe
matplotlib.pyplot.triplot(*args, **kwargs)
Axes.triplot(triangulation, **kwargs)
# triangulation: Triangulation object (created from x, y coordinates)
tripcolor() - triangular pseudocolor
matplotlib.pyplot.tripcolor(*args, **kwargs)
Axes.tripcolor(triangulation, C=None, **kwargs)
tricontour() / tricontourf() - triangular contours
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 a 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 contours
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()'
)
Contours only
axes
[
1
,
1
]
.
tricontour
(
triang
,
z
,
levels
=
15
,
cmap
=
'viridis'
,
linewidths
=
1.5
)
axes
[
1
,
1
]
.
set_title
(
'tricontour()'
)
plt.
show
(
)
polar() - polar plotting
matplotlib.pyplot.polar(*args, **kwargs)
# 等同于:
# plt.subplot(projection='polar')
# ax.plot(theta, r)
| Parameters | Description |
|---|---|
| theta | Array of angles (in radians) |
| r | Array of radial distances |
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
(
)
rgrids() / thetagrids() - polar grids
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 angular 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
(
)