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

Skip to content

Commit 82a4b24

Browse files
authored
Merge pull request #9990 from eric-wieser/lightsource-shading
Fix and document lightsource argument in mplot3d
2 parents 074cee4 + b056107 commit 82a4b24

File tree

1 file changed

+36
-29
lines changed

1 file changed

+36
-29
lines changed

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,7 +1594,11 @@ def plot_surface(self, X, Y, Z, *args, norm=None, vmin=None,
15941594
Bounds for the normalization.
15951595
15961596
shade : bool
1597-
Whether to shade the face colors.
1597+
Whether to shade the facecolors. Defaults to True. Shading is
1598+
always disabled when `cmap` is specified.
1599+
1600+
lightsource : `~matplotlib.colors.LightSource`
1601+
The lightsource to use when `shade` is True.
15981602
15991603
**kwargs :
16001604
Other arguments are forwarded to `.Poly3DCollection`.
@@ -1645,10 +1649,6 @@ def plot_surface(self, X, Y, Z, *args, norm=None, vmin=None,
16451649
cmap = kwargs.get('cmap', None)
16461650
shade = kwargs.pop('shade', cmap is None)
16471651

1648-
# Shade the data
1649-
if shade and cmap is not None and fcolors is not None:
1650-
fcolors = self._shade_colors_lightsource(Z, cmap, lightsource)
1651-
16521652
# evenly spaced, and including both endpoints
16531653
row_inds = list(range(0, rows-1, rstride)) + [rows-1]
16541654
col_inds = list(range(0, cols-1, cstride)) + [cols-1]
@@ -1689,7 +1689,7 @@ def get_normals(polygons):
16891689

16901690
if fcolors is not None:
16911691
if shade:
1692-
colset = self._shade_colors(colset, get_normals(polys))
1692+
colset = self._shade_colors(colset, get_normals(polys), lightsource)
16931693
polyc.set_facecolors(colset)
16941694
polyc.set_edgecolors(colset)
16951695
elif cmap:
@@ -1702,7 +1702,7 @@ def get_normals(polygons):
17021702
polyc.set_norm(norm)
17031703
else:
17041704
if shade:
1705-
colset = self._shade_colors(color, get_normals(polys))
1705+
colset = self._shade_colors(color, get_normals(polys), lightsource)
17061706
else:
17071707
colset = color
17081708
polyc.set_facecolors(colset)
@@ -1726,13 +1726,16 @@ def _generate_normals(self, polygons):
17261726
normals.append(np.cross(v1, v2))
17271727
return normals
17281728

1729-
def _shade_colors(self, color, normals):
1729+
def _shade_colors(self, color, normals, lightsource=None):
17301730
'''
17311731
Shade *color* using normal vectors given by *normals*.
17321732
*color* can also be an array of the same length as *normals*.
17331733
'''
1734+
if lightsource is None:
1735+
# chosen for backwards-compatibility
1736+
lightsource = LightSource(azdeg=225, altdeg=19.4712)
17341737

1735-
shade = np.array([np.dot(n / proj3d.mod(n), [-1, -1, 0.5])
1738+
shade = np.array([np.dot(n / proj3d.mod(n), lightsource.direction)
17361739
if proj3d.mod(n) else np.nan
17371740
for n in normals])
17381741
mask = ~np.isnan(shade)
@@ -1752,11 +1755,6 @@ def _shade_colors(self, color, normals):
17521755

17531756
return colors
17541757

1755-
def _shade_colors_lightsource(self, data, cmap, lightsource):
1756-
if lightsource is None:
1757-
lightsource = LightSource(azdeg=135, altdeg=55)
1758-
return lightsource.shade(data, cmap)
1759-
17601758
def plot_wireframe(self, X, Y, Z, *args, **kwargs):
17611759
"""
17621760
Plot a 3D wireframe.
@@ -1880,17 +1878,7 @@ def plot_wireframe(self, X, Y, Z, *args, **kwargs):
18801878
def plot_trisurf(self, *args, color=None, norm=None, vmin=None, vmax=None,
18811879
lightsource=None, **kwargs):
18821880
"""
1883-
============= ================================================
1884-
Argument Description
1885-
============= ================================================
1886-
*X*, *Y*, *Z* Data values as 1D arrays
1887-
*color* Color of the surface patches
1888-
*cmap* A colormap for the surface patches.
1889-
*norm* An instance of Normalize to map values to colors
1890-
*vmin* Minimum value to map
1891-
*vmax* Maximum value to map
1892-
*shade* Whether to shade the facecolors
1893-
============= ================================================
1881+
Plot a triangulated surface.
18941882
18951883
The (optional) triangulation can be specified in one of two ways;
18961884
either::
@@ -1915,10 +1903,29 @@ def plot_trisurf(self, *args, color=None, norm=None, vmin=None, vmax=None,
19151903
where *Z* is the array of values to contour, one per point
19161904
in the triangulation.
19171905
1918-
Other arguments are passed on to
1919-
:class:`~mpl_toolkits.mplot3d.art3d.Poly3DCollection`
1906+
Parameters
1907+
----------
1908+
X, Y, Z : array-like
1909+
Data values as 1D arrays.
1910+
color
1911+
Color of the surface patches.
1912+
cmap
1913+
A colormap for the surface patches.
1914+
norm : Normalize
1915+
An instance of Normalize to map values to colors.
1916+
vmin, vmax : scalar, optional, default: None
1917+
Minimum and maximum value to map.
1918+
shade : bool
1919+
Whether to shade the facecolors. Defaults to True. Shading is
1920+
always disabled when *cmap* is specified.
1921+
lightsource : `~matplotlib.colors.LightSource`
1922+
The lightsource to use when *shade* is True.
1923+
**kwargs
1924+
All other arguments are passed on to
1925+
:class:`~mpl_toolkits.mplot3d.art3d.Poly3DCollection`
19201926
1921-
**Examples:**
1927+
Examples
1928+
--------
19221929
19231930
.. plot:: gallery/mplot3d/trisurf3d.py
19241931
.. plot:: gallery/mplot3d/trisurf3d_2.py
@@ -1967,7 +1974,7 @@ def plot_trisurf(self, *args, color=None, norm=None, vmin=None, vmax=None,
19671974
v1 = verts[:, 0, :] - verts[:, 1, :]
19681975
v2 = verts[:, 1, :] - verts[:, 2, :]
19691976
normals = np.cross(v1, v2)
1970-
colset = self._shade_colors(color, normals)
1977+
colset = self._shade_colors(color, normals, lightsource)
19711978
else:
19721979
colset = color
19731980
polyc.set_facecolors(colset)

0 commit comments

Comments
 (0)