Skip to content

Commit 76eff4a

Browse files
fill_between in plot types
1 parent 26cbf88 commit 76eff4a

File tree

5 files changed

+77
-45
lines changed

5 files changed

+77
-45
lines changed

doc/users/next_whats_new/fill_between_3d.rst

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ function.
1212
N = 50
1313
theta = np.linspace(0, 2*np.pi, N)
1414

15-
xs1 = np.cos(theta)
16-
ys1 = np.sin(theta)
17-
zs1 = 0.1 * np.sin(6 * theta)
15+
x1 = np.cos(theta)
16+
y1 = np.sin(theta)
17+
z1 = 0.1 * np.sin(6 * theta)
1818

19-
xs2 = 0.6 * np.cos(theta)
20-
ys2 = 0.6 * np.sin(theta)
21-
zs2 = 2 # Note that scalar values work in addition to length N arrays
19+
x2 = 0.6 * np.cos(theta)
20+
y2 = 0.6 * np.sin(theta)
21+
z2 = 2 # Note that scalar values work in addition to length N arrays
2222

2323
fig = plt.figure()
2424
ax = fig.add_subplot(projection='3d')
25-
ax.fill_between(xs1, ys1, zs1, xs2, ys2, zs2,
25+
ax.fill_between(x1, y1, z1, x2, y2, z2,
2626
alpha=0.5, edgecolor='k')

galleries/examples/mplot3d/fillbetween3d.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@
1313
N = 50
1414
theta = np.linspace(0, 2*np.pi, N)
1515

16-
xs1 = np.cos(theta)
17-
ys1 = np.sin(theta)
18-
zs1 = 0.1 * np.sin(6 * theta)
16+
x1 = np.cos(theta)
17+
y1 = np.sin(theta)
18+
z1 = 0.1 * np.sin(6 * theta)
1919

20-
xs2 = 0.6 * np.cos(theta)
21-
ys2 = 0.6 * np.sin(theta)
22-
zs2 = 2 # Note that scalar values work in addition to length N arrays
20+
x2 = 0.6 * np.cos(theta)
21+
y2 = 0.6 * np.sin(theta)
22+
z2 = 2 # Note that scalar values work in addition to length N arrays
2323

2424
fig = plt.figure()
2525
ax = fig.add_subplot(projection='3d')
26-
ax.fill_between(xs1, ys1, zs1, xs2, ys2, zs2, alpha=0.5, edgecolor='k')
26+
ax.fill_between(x1, y1, z1, x2, y2, z2, alpha=0.5, edgecolor='k')
2727

2828
plt.show()
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
====================================
3+
fill_between(x1, y1, z1, x2, y2, z2)
4+
====================================
5+
6+
See `~mpl_toolkits.mplot3d.axes3d.Axes3D.fill_between`.
7+
"""
8+
import matplotlib.pyplot as plt
9+
import numpy as np
10+
11+
plt.style.use('_mpl-gallery')
12+
13+
# Make data
14+
n = 50
15+
theta = np.linspace(0, 2*np.pi, n)
16+
x1 = np.cos(theta)
17+
y1 = np.sin(theta)
18+
z1 = np.linspace(0, 1, n)
19+
x2 = np.cos(theta + np.pi)
20+
y2 = np.sin(theta + np.pi)
21+
z2 = z1
22+
23+
# Plot
24+
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
25+
ax.fill_between(x1, y1, z1, x2, y2, z2, alpha=0.5)
26+
ax.plot(x1, y1, z1, linewidth=2, color='C0')
27+
ax.plot(x2, y2, z2, linewidth=2, color='C0')
28+
29+
ax.set(xticklabels=[],
30+
yticklabels=[],
31+
zticklabels=[])
32+
33+
plt.show()

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1904,21 +1904,21 @@ def plot(self, xs, ys, *args, zdir='z', **kwargs):
19041904

19051905
plot3D = plot
19061906

1907-
def fill_between(self, xs1, ys1, zs1, xs2, ys2, zs2, *, where=None, **kwargs):
1907+
def fill_between(self, x1, y1, z1, x2, y2, z2, *, where=None, **kwargs):
19081908
"""
19091909
Fill the area between two 2D or 3D curves.
19101910
1911-
The curves are defined by the points (*xs1*, *ys1*, *zs1*) and
1912-
(*xs2*, *ys2*, *zs2*). This creates one or multiple quadrangle
1911+
The curves are defined by the points (*x1*, *y1*, *z1*) and
1912+
(*x2*, *y2*, *z2*). This creates one or multiple quadrangle
19131913
polygons that are filled. All points must be the same length N, or a
19141914
single value to be used for all points.
19151915
19161916
Parameters
19171917
----------
1918-
xs1, ys1, zs1 : float or 1D array-like
1918+
x1, y1, z1 : float or 1D array-like
19191919
x, y, and z coordinates of vertices for 1st line.
19201920
1921-
xs2, ys2, zs2 : float or 1D array-like
1921+
x2, y2, z2 : float or 1D array-like
19221922
x, y, and z coordinates of vertices for 2nd line.
19231923
19241924
where : array of bool (length N), optional
@@ -1941,41 +1941,40 @@ def fill_between(self, xs1, ys1, zs1, xs2, ys2, zs2, *, where=None, **kwargs):
19411941
19421942
"""
19431943
had_data = self.has_data()
1944-
xs1, ys1, zs1, xs2, ys2, zs2 = cbook._broadcast_with_masks(xs1, ys1, zs1,
1945-
xs2, ys2, zs2)
1944+
x1, y1, z1, x2, y2, z2 = cbook._broadcast_with_masks(x1, y1, z1, x2, y2, z2)
19461945

19471946
if where is None:
19481947
where = True
19491948
else:
19501949
where = np.asarray(where, dtype=bool)
1951-
if where.size != xs1.size:
1950+
if where.size != x1.size:
19521951
raise ValueError(f"where size ({where.size}) does not match "
1953-
f"size ({xs1.size})")
1954-
where = where & ~np.isnan(xs1) # NaNs were broadcast in _broadcast_with_masks
1952+
f"size ({x1.size})")
1953+
where = where & ~np.isnan(x1) # NaNs were broadcast in _broadcast_with_masks
19551954

19561955
polys = []
19571956
for idx0, idx1 in cbook.contiguous_regions(where):
1958-
xs1slice = xs1[idx0:idx1]
1959-
ys1slice = ys1[idx0:idx1]
1960-
zs1slice = zs1[idx0:idx1]
1961-
xs2slice = xs2[idx0:idx1]
1962-
ys2slice = ys2[idx0:idx1]
1963-
zs2slice = zs2[idx0:idx1]
1964-
1965-
if not len(xs1slice):
1957+
x1slice = x1[idx0:idx1]
1958+
y1slice = y1[idx0:idx1]
1959+
z1slice = z1[idx0:idx1]
1960+
x2slice = x2[idx0:idx1]
1961+
y2slice = y2[idx0:idx1]
1962+
z2slice = z2[idx0:idx1]
1963+
1964+
if not len(x1slice):
19661965
continue
19671966

1968-
for i in range(len(xs1slice) - 1):
1969-
poly = [(xs1slice[i], ys1slice[i], zs1slice[i]),
1970-
(xs1slice[i+1], ys1slice[i+1], zs1slice[i+1]),
1971-
(xs2slice[i+1], ys2slice[i+1], zs2slice[i+1]),
1972-
(xs2slice[i], ys2slice[i], zs2slice[i])]
1967+
for i in range(len(x1slice) - 1):
1968+
poly = [(x1slice[i], y1slice[i], z1slice[i]),
1969+
(x1slice[i+1], y1slice[i+1], z1slice[i+1]),
1970+
(x2slice[i+1], y2slice[i+1], z2slice[i+1]),
1971+
(x2slice[i], y2slice[i], z2slice[i])]
19731972
polys.append(poly)
19741973

19751974
polyc = art3d.Poly3DCollection(polys, **kwargs)
19761975
self.add_collection(polyc)
19771976

1978-
self.auto_scale_xyz([xs1, xs2], [ys1, ys2], [zs1, zs2], had_data)
1977+
self.auto_scale_xyz([x1, x2], [y1, y2], [z1, z2], had_data)
19791978
return polyc
19801979

19811980
def plot_surface(self, X, Y, Z, *, norm=None, vmin=None,

lib/mpl_toolkits/mplot3d/tests/test_axes3d.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -599,17 +599,17 @@ def test_fill_between():
599599

600600
theta = np.linspace(0, 2*np.pi, 50)
601601

602-
xs1 = np.cos(theta)
603-
ys1 = np.sin(theta)
604-
zs1 = 0.1 * np.sin(6 * theta)
602+
x1 = np.cos(theta)
603+
y1 = np.sin(theta)
604+
z1 = 0.1 * np.sin(6 * theta)
605605

606-
xs2 = 0.6 * np.cos(theta)
607-
ys2 = 0.6 * np.sin(theta)
608-
zs2 = 2
606+
x2 = 0.6 * np.cos(theta)
607+
y2 = 0.6 * np.sin(theta)
608+
z2 = 2
609609

610610
where = (theta < np.pi/2) | (theta > 3*np.pi/2)
611611

612-
ax.fill_between(xs1, ys1, zs1, xs2, ys2, zs2,
612+
ax.fill_between(x1, y1, z1, x2, y2, z2,
613613
where=where, alpha=0.5, edgecolor='k')
614614

615615

0 commit comments

Comments
 (0)