forked from matplotlib/matplotlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request matplotlib#28225 from scottshambaugh/3d_fill_between
[ENH]: fill_between extended to 3D
- Loading branch information
Showing
13 changed files
with
391 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ Plotting | |
plot_surface | ||
plot_wireframe | ||
plot_trisurf | ||
fill_between | ||
|
||
clabel | ||
contour | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
Fill between 3D lines | ||
--------------------- | ||
|
||
The new method `.Axes3D.fill_between` allows to fill the surface between two | ||
3D lines with polygons. | ||
|
||
.. plot:: | ||
:include-source: | ||
:alt: Example of 3D fill_between | ||
|
||
N = 50 | ||
theta = np.linspace(0, 2*np.pi, N) | ||
|
||
x1 = np.cos(theta) | ||
y1 = np.sin(theta) | ||
z1 = 0.1 * np.sin(6 * theta) | ||
|
||
x2 = 0.6 * np.cos(theta) | ||
y2 = 0.6 * np.sin(theta) | ||
z2 = 2 # Note that scalar values work in addition to length N arrays | ||
|
||
fig = plt.figure() | ||
ax = fig.add_subplot(projection='3d') | ||
ax.fill_between(x1, y1, z1, x2, y2, z2, | ||
alpha=0.5, edgecolor='k') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
""" | ||
===================== | ||
Fill between 3D lines | ||
===================== | ||
Demonstrate how to fill the space between 3D lines with surfaces. Here we | ||
create a sort of "lampshade" shape. | ||
""" | ||
|
||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
|
||
N = 50 | ||
theta = np.linspace(0, 2*np.pi, N) | ||
|
||
x1 = np.cos(theta) | ||
y1 = np.sin(theta) | ||
z1 = 0.1 * np.sin(6 * theta) | ||
|
||
x2 = 0.6 * np.cos(theta) | ||
y2 = 0.6 * np.sin(theta) | ||
z2 = 2 # Note that scalar values work in addition to length N arrays | ||
|
||
fig = plt.figure() | ||
ax = fig.add_subplot(projection='3d') | ||
ax.fill_between(x1, y1, z1, x2, y2, z2, alpha=0.5, edgecolor='k') | ||
|
||
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
""" | ||
========================= | ||
Fill under 3D line graphs | ||
========================= | ||
Demonstrate how to create polygons which fill the space under a line | ||
graph. In this example polygons are semi-transparent, creating a sort | ||
of 'jagged stained glass' effect. | ||
""" | ||
|
||
import math | ||
|
||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
|
||
gamma = np.vectorize(math.gamma) | ||
N = 31 | ||
x = np.linspace(0., 10., N) | ||
lambdas = range(1, 9) | ||
|
||
ax = plt.figure().add_subplot(projection='3d') | ||
|
||
facecolors = plt.colormaps['viridis_r'](np.linspace(0, 1, len(lambdas))) | ||
|
||
for i, l in enumerate(lambdas): | ||
# Note fill_between can take coordinates as length N vectors, or scalars | ||
ax.fill_between(x, l, l**x * np.exp(-l) / gamma(x + 1), | ||
x, l, 0, | ||
facecolors=facecolors[i], alpha=.7) | ||
|
||
ax.set(xlim=(0, 10), ylim=(1, 9), zlim=(0, 0.35), | ||
xlabel='x', ylabel=r'$\lambda$', zlabel='probability') | ||
|
||
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,36 @@ | ||
""" | ||
============================================= | ||
Generate polygons to fill under 3D line graph | ||
============================================= | ||
==================== | ||
Generate 3D polygons | ||
==================== | ||
Demonstrate how to create polygons which fill the space under a line | ||
graph. In this example polygons are semi-transparent, creating a sort | ||
of 'jagged stained glass' effect. | ||
Demonstrate how to create polygons in 3D. Here we stack 3 hexagons. | ||
""" | ||
|
||
import math | ||
|
||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
|
||
from matplotlib.collections import PolyCollection | ||
|
||
# Fixing random state for reproducibility | ||
np.random.seed(19680801) | ||
from mpl_toolkits.mplot3d.art3d import Poly3DCollection | ||
|
||
# Coordinates of a hexagon | ||
angles = np.linspace(0, 2 * np.pi, 6, endpoint=False) | ||
x = np.cos(angles) | ||
y = np.sin(angles) | ||
zs = [-3, -2, -1] | ||
|
||
def polygon_under_graph(x, y): | ||
""" | ||
Construct the vertex list which defines the polygon filling the space under | ||
the (x, y) line graph. This assumes x is in ascending order. | ||
""" | ||
return [(x[0], 0.), *zip(x, y), (x[-1], 0.)] | ||
# Close the hexagon by repeating the first vertex | ||
x = np.append(x, x[0]) | ||
y = np.append(y, y[0]) | ||
|
||
verts = [] | ||
for z in zs: | ||
verts.append(list(zip(x*z, y*z, np.full_like(x, z)))) | ||
verts = np.array(verts) | ||
|
||
ax = plt.figure().add_subplot(projection='3d') | ||
|
||
x = np.linspace(0., 10., 31) | ||
lambdas = range(1, 9) | ||
|
||
# verts[i] is a list of (x, y) pairs defining polygon i. | ||
gamma = np.vectorize(math.gamma) | ||
verts = [polygon_under_graph(x, l**x * np.exp(-l) / gamma(x + 1)) | ||
for l in lambdas] | ||
facecolors = plt.colormaps['viridis_r'](np.linspace(0, 1, len(verts))) | ||
|
||
poly = PolyCollection(verts, facecolors=facecolors, alpha=.7) | ||
ax.add_collection3d(poly, zs=lambdas, zdir='y') | ||
|
||
ax.set(xlim=(0, 10), ylim=(1, 9), zlim=(0, 0.35), | ||
xlabel='x', ylabel=r'$\lambda$', zlabel='probability') | ||
poly = Poly3DCollection(verts, alpha=.7) | ||
ax.add_collection3d(poly) | ||
ax.auto_scale_xyz(verts[:, :, 0], verts[:, :, 1], verts[:, :, 2]) | ||
ax.set_aspect('equalxy') | ||
|
||
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
""" | ||
==================================== | ||
fill_between(x1, y1, z1, x2, y2, z2) | ||
==================================== | ||
See `~mpl_toolkits.mplot3d.axes3d.Axes3D.fill_between`. | ||
""" | ||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
|
||
plt.style.use('_mpl-gallery') | ||
|
||
# Make data for a double helix | ||
n = 50 | ||
theta = np.linspace(0, 2*np.pi, n) | ||
x1 = np.cos(theta) | ||
y1 = np.sin(theta) | ||
z1 = np.linspace(0, 1, n) | ||
x2 = np.cos(theta + np.pi) | ||
y2 = np.sin(theta + np.pi) | ||
z2 = z1 | ||
|
||
# Plot | ||
fig, ax = plt.subplots(subplot_kw={"projection": "3d"}) | ||
ax.fill_between(x1, y1, z1, x2, y2, z2, alpha=0.5) | ||
ax.plot(x1, y1, z1, linewidth=2, color='C0') | ||
ax.plot(x2, y2, z2, linewidth=2, color='C0') | ||
|
||
ax.set(xticklabels=[], | ||
yticklabels=[], | ||
zticklabels=[]) | ||
|
||
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+50.1 KB
...mpl_toolkits/mplot3d/tests/baseline_images/test_axes3d/fill_between_polygon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+70.4 KB
lib/mpl_toolkits/mplot3d/tests/baseline_images/test_axes3d/fill_between_quad.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.