Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Backport PR #24108 on branch v3.6.x (Add 3D plots to plot_types doc page) #24258

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
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
1 change: 1 addition & 0 deletions doc/sphinxext/gallery_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
'../plot_types/arrays',
'../plot_types/stats',
'../plot_types/unstructured',
'../plot_types/3D',
]


Expand Down
6 changes: 6 additions & 0 deletions plot_types/3D/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.. _3D_plots:

3D
--

3D plots using the `mpl_toolkits.mplot3d` library.
29 changes: 29 additions & 0 deletions plot_types/3D/scatter3d_simple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""
==============
3D scatterplot
==============

See `~mpl_toolkits.mplot3d.axes3d.Axes3D.scatter`.
"""
import matplotlib.pyplot as plt
import numpy as np

plt.style.use('_mpl-gallery')

# Make data
np.random.seed(19680801)
n = 100
rng = np.random.default_rng()
xs = rng.uniform(23, 32, n)
ys = rng.uniform(0, 100, n)
zs = rng.uniform(-50, -25, n)

# Plot
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
ax.scatter(xs, ys, zs)

ax.set(xticklabels=[],
yticklabels=[],
zticklabels=[])

plt.show()
29 changes: 29 additions & 0 deletions plot_types/3D/surface3d_simple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""
=====================
3D surface
=====================

See `~mpl_toolkits.mplot3d.axes3d.Axes3D.plot_surface`.
"""
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np

plt.style.use('_mpl-gallery')

# Make data
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)

# Plot the surface
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
ax.plot_surface(X, Y, Z, vmin=Z.min() * 2, cmap=cm.Blues)

ax.set(xticklabels=[],
yticklabels=[],
zticklabels=[])

plt.show()
34 changes: 34 additions & 0 deletions plot_types/3D/trisurf3d_simple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
======================
Triangular 3D surfaces
======================

See `~mpl_toolkits.mplot3d.axes3d.Axes3D.plot_trisurf`.
"""
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np

plt.style.use('_mpl-gallery')

n_radii = 8
n_angles = 36

# Make radii and angles spaces
radii = np.linspace(0.125, 1.0, n_radii)
angles = np.linspace(0, 2*np.pi, n_angles, endpoint=False)[..., np.newaxis]

# Convert polar (radii, angles) coords to cartesian (x, y) coords.
x = np.append(0, (radii*np.cos(angles)).flatten())
y = np.append(0, (radii*np.sin(angles)).flatten())
z = np.sin(-x*y)

# Plot
fig, ax = plt.subplots(subplot_kw={'projection': '3d'})
ax.plot_trisurf(x, y, z, vmin=z.min() * 2, cmap=cm.Blues)

ax.set(xticklabels=[],
yticklabels=[],
zticklabels=[])

plt.show()
31 changes: 31 additions & 0 deletions plot_types/3D/voxels_simple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""
==========================
3D voxel / volumetric plot
==========================

See `~mpl_toolkits.mplot3d.axes3d.Axes3D.voxels`.
"""
import matplotlib.pyplot as plt
import numpy as np

plt.style.use('_mpl-gallery')

# Prepare some coordinates
x, y, z = np.indices((8, 8, 8))

# Draw cuboids in the top left and bottom right corners
cube1 = (x < 3) & (y < 3) & (z < 3)
cube2 = (x >= 5) & (y >= 5) & (z >= 5)

# Combine the objects into a single boolean array
voxelarray = cube1 | cube2

# Plot
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
ax.voxels(voxelarray, edgecolor='k')

ax.set(xticklabels=[],
yticklabels=[],
zticklabels=[])

plt.show()
24 changes: 24 additions & 0 deletions plot_types/3D/wire3d_simple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""
=================
3D wireframe plot
=================

See `~mpl_toolkits.mplot3d.axes3d.Axes3D.plot_wireframe`.
"""
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt

plt.style.use('_mpl-gallery')

# Make data
X, Y, Z = axes3d.get_test_data(0.05)

# Plot
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)

ax.set(xticklabels=[],
yticklabels=[],
zticklabels=[])

plt.show()