Skip to content

Add contour and tricontour plots to plot types #21114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
24 changes: 24 additions & 0 deletions plot_types/arrays/contour.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""
================
contour(X, Y, Z)
================

See `~matplotlib.axes.Axes.contour`.
"""
import matplotlib.pyplot as plt
import numpy as np

plt.style.use('mpl_plot_gallery')

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

# plot
fig, ax = plt.subplots()
ax.grid(False)

ax.contour(X, Y, Z, levels=levels)

plt.show()
11 changes: 5 additions & 6 deletions plot_types/arrays/contourf.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""
====================================
contour(X, Y, Z) / contourf(X, Y, Z)
====================================
=================
contourf(X, Y, Z)
=================

See `~matplotlib.axes.Axes.contour` / `~matplotlib.axes.Axes.contourf`.
See `~matplotlib.axes.Axes.contourf`.
"""
import matplotlib.pyplot as plt
import numpy as np
Expand All @@ -12,13 +12,12 @@

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

# plot
fig, ax = plt.subplots()

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

plt.show()
17 changes: 9 additions & 8 deletions plot_types/unstructured/tricontour.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""
==========================================
tricontour(x, y, z) / tricontourf(x, y, z)
==========================================
===================
tricontour(x, y, z)
===================

See `~matplotlib.axes.Axes.tricontour` / `~matplotlib.axes.Axes.tricontourf`.
See `~matplotlib.axes.Axes.tricontour`.
"""
import matplotlib.pyplot as plt
import numpy as np
Expand All @@ -12,7 +12,8 @@

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

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

# plot:
fig, ax = plt.subplots()
ax.grid(False)

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

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

Expand Down
35 changes: 35 additions & 0 deletions plot_types/unstructured/tricontourf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""
====================
tricontourf(x, y, z)
====================

See `~matplotlib.axes.Axes.tricontourf`.
"""
import matplotlib.pyplot as plt
import numpy as np

plt.style.use('mpl_plot_gallery')

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

# sample it to make unstructured x, y, z
np.random.seed(1)
ysamp = np.random.randint(0, 256, size=250)
xsamp = np.random.randint(0, 256, size=250)
y = Y[:, 0][ysamp]
x = X[0, :][xsamp]
z = Z[ysamp, xsamp]

# plot:
fig, ax = plt.subplots()
ax.grid(False)

ax.plot(x, y, 'o', markersize=2, color='grey')
ax.tricontourf(x, y, z, levels=levels)

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

plt.show()
4 changes: 2 additions & 2 deletions plot_types/unstructured/tripcolor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

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

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

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

ax.set(xlim=(-3, 3), ylim=(-3, 3))
Expand Down
1 change: 1 addition & 0 deletions plot_types/unstructured/triplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

# plot:
fig, ax = plt.subplots()
ax.grid(False)

ax.triplot(x, y)

Expand Down