Skip to content

Commit ec5d889

Browse files
committed
Add contour and tricontour plots to plot types
1 parent 70f2145 commit ec5d889

File tree

7 files changed

+78
-16
lines changed

7 files changed

+78
-16
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,13 +12,12 @@
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
levels = np.linspace(Z.min(), Z.max(), 7)
1717

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

2121
ax.contourf(X, Y, Z, levels=levels)
22-
ax.contour(X, Y, Z, levels=levels, colors="white", linewidths=0.5)
2322

2423
plt.show()

plot_types/unstructured/tricontour.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""
2-
==========================================
3-
tricontour(x, y, z) / tricontourf(x, y, z)
4-
==========================================
2+
===================
3+
tricontour(x, y, z)
4+
===================
55
6-
See `~matplotlib.axes.Axes.tricontour` / `~matplotlib.axes.Axes.tricontourf`.
6+
See `~matplotlib.axes.Axes.tricontour`.
77
"""
88
import matplotlib.pyplot as plt
99
import numpy as np
@@ -12,7 +12,8 @@
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)
16+
levels = np.linspace(Z.min(), Z.max(), 7)
1617

1718
# sample it to make unstructured x, y, z
1819
np.random.seed(1)
@@ -24,10 +25,10 @@
2425

2526
# plot:
2627
fig, ax = plt.subplots()
28+
ax.grid(False)
2729

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

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

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
====================
3+
tricontourf(x, y, z)
4+
====================
5+
6+
See `~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+
levels = np.linspace(np.min(Z), np.max(Z), 7)
18+
19+
# sample it to make unstructured x, y, z
20+
np.random.seed(1)
21+
ysamp = np.random.randint(0, 256, size=250)
22+
xsamp = np.random.randint(0, 256, size=250)
23+
y = Y[:, 0][ysamp]
24+
x = X[0, :][xsamp]
25+
z = Z[ysamp, xsamp]
26+
27+
# plot:
28+
fig, ax = plt.subplots()
29+
ax.grid(False)
30+
31+
ax.plot(x, y, 'o', markersize=2, color='grey')
32+
ax.tricontourf(x, y, z, levels=levels)
33+
34+
ax.set(xlim=(-3, 3), ylim=(-3, 3))
35+
36+
plt.show()

plot_types/unstructured/tripcolor.py

+2-2
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

1717
# sample it to make unstructured x, y, z
1818
np.random.seed(1)
@@ -25,7 +25,7 @@
2525
# plot:
2626
fig, ax = plt.subplots()
2727

28-
ax.plot(x, y, '.k', alpha=0.5)
28+
ax.plot(x, y, 'o', markersize=2, color='grey')
2929
ax.tripcolor(x, y, z)
3030

3131
ax.set(xlim=(-3, 3), ylim=(-3, 3))

plot_types/unstructured/triplot.py

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
# plot:
2424
fig, ax = plt.subplots()
25+
ax.grid(False)
2526

2627
ax.triplot(x, y)
2728

0 commit comments

Comments
 (0)