Skip to content

Commit ae5f936

Browse files
committed
Add contour and tricontour plots to plot types
1 parent c53216b commit ae5f936

File tree

4 files changed

+70
-9
lines changed

4 files changed

+70
-9
lines changed

plot_types/arrays/contour.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
================
3+
contour(X, Y, Z)
4+
================
5+
6+
See `~matplotlib.axes.Axes.contour`.
7+
"""
8+
import matplotlib.pyplot as plt
9+
import numpy as np
10+
11+
plt.style.use('mpl_plot_gallery')
12+
13+
# make data
14+
X, Y = np.meshgrid(np.linspace(-3, 3, 256), np.linspace(-3, 3, 256))
15+
Z = (1 - X/2 + X**5 + Y**3) * np.exp(-X**2 - Y**2)
16+
Z = Z - Z.min()
17+
levels = np.linspace(np.min(Z), np.max(Z), 7)
18+
19+
# plot
20+
fig, ax = plt.subplots()
21+
ax.grid(False)
22+
23+
ax.contour(X, Y, Z, levels=levels)
24+
25+
plt.show()

plot_types/arrays/contourf.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""
2-
====================================
3-
contour(X, Y, Z) / contourf(X, Y, Z)
4-
====================================
2+
=================
3+
contourf(X, Y, Z)
4+
=================
55
6-
See `~matplotlib.axes.Axes.contour` / `~matplotlib.axes.Axes.contourf`.
6+
See `~matplotlib.axes.Axes.contourf`.
77
"""
88
import matplotlib.pyplot as plt
99
import numpy as np
@@ -12,14 +12,13 @@
1212

1313
# make data
1414
X, Y = np.meshgrid(np.linspace(-3, 3, 256), np.linspace(-3, 3, 256))
15-
Z = (1 - X/2. + X**5 + Y**3) * np.exp(-X**2 - Y**2)
15+
Z = (1 - X/2 + X**5 + Y**3) * np.exp(-X**2 - Y**2)
1616
Z = Z - Z.min()
1717
levels = np.linspace(np.min(Z), np.max(Z), 7)
1818

1919
# plot
2020
fig, ax = plt.subplots()
2121

2222
ax.contourf(X, Y, Z, levels=levels)
23-
ax.contour(X, Y, Z, levels=levels, colors="white", linewidths=0.5)
2423

2524
plt.show()

plot_types/unstructured/tricontour.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
# make structured data
1414
X, Y = np.meshgrid(np.linspace(-3, 3, 256), np.linspace(-3, 3, 256))
15-
Z = (1 - X/2. + X**5 + Y**3) * np.exp(-X**2 - Y**2)
15+
Z = (1 - X/2 + X**5 + Y**3) * np.exp(-X**2 - Y**2)
1616
Z = Z - Z.min()
1717

1818
# sample it to make unstructured x, y, z
@@ -25,10 +25,11 @@
2525

2626
# plot:
2727
fig, ax = plt.subplots()
28+
ax.grid(False)
2829

29-
ax.plot(x, y, '.k', alpha=0.5)
30+
ax.plot(x, y, 'o', markersize=2, color='lightgrey')
3031
levels = np.linspace(np.min(Z), np.max(Z), 7)
31-
ax.tricontourf(x, y, z, levels=levels)
32+
ax.tricontour(x, y, z, levels=levels)
3233

3334
ax.set(xlim=(-3, 3), ylim=(-3, 3))
3435

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
==========================================
3+
tricontour(x, y, z) / tricontourf(x, y, z)
4+
==========================================
5+
6+
See `~matplotlib.axes.Axes.tricontour` / `~matplotlib.axes.Axes.tricontourf`.
7+
"""
8+
import matplotlib.pyplot as plt
9+
import numpy as np
10+
11+
plt.style.use('mpl_plot_gallery')
12+
13+
# make structured data
14+
X, Y = np.meshgrid(np.linspace(-3, 3, 256), np.linspace(-3, 3, 256))
15+
Z = (1 - X/2. + X**5 + Y**3) * np.exp(-X**2 - Y**2)
16+
Z = Z - Z.min()
17+
18+
# sample it to make unstructured x, y, z
19+
np.random.seed(1)
20+
ysamp = np.random.randint(0, 256, size=250)
21+
xsamp = np.random.randint(0, 256, size=250)
22+
y = Y[:, 0][ysamp]
23+
x = X[0, :][xsamp]
24+
z = Z[ysamp, xsamp]
25+
26+
# plot:
27+
fig, ax = plt.subplots()
28+
ax.grid(False)
29+
30+
ax.plot(x, y, 'o', markersize=2, color='grey')
31+
levels = np.linspace(np.min(Z), np.max(Z), 7)
32+
ax.tricontourf(x, y, z, levels=levels)
33+
34+
ax.set(xlim=(-3, 3), ylim=(-3, 3))
35+
36+
plt.show()

0 commit comments

Comments
 (0)