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

Skip to content

Commit 0db6341

Browse files
committed
Make the "fraction" kwarg behave as it did before recent LightSource enhancements
1 parent ed5cd76 commit 0db6341

File tree

1 file changed

+18
-23
lines changed

1 file changed

+18
-23
lines changed

lib/matplotlib/colors.py

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,12 +1528,8 @@ def hillshade(self, elevation, vert_exag=1, dx=1, dy=1, fraction=1.):
15281528
Increases or decreases the contrast of the hillshade. Values
15291529
greater than one will cause intermediate values to move closer to
15301530
full illumination or shadow (and clipping any values that move
1531-
beyond 0 or 1). Values less than one will cause full shadow or
1532-
full illumination to move closer to a value of 0.5, thereby
1533-
decreasing contrast. Note that this is not mathematically or
1534-
visually the same as increasing/decreasing the vertical
1535-
exaggeration.
1536-
1531+
beyond 0 or 1). Note that this is not visually or mathematically
1532+
the same as vertical exaggeration.
15371533
Returns
15381534
-------
15391535
intensity : ndarray
@@ -1557,12 +1553,16 @@ def hillshade(self, elevation, vert_exag=1, dx=1, dy=1, fraction=1.):
15571553
+ np.cos(alt) * np.cos(slope)
15581554
* np.cos(az - aspect))
15591555

1560-
intensity -= intensity.min()
1561-
intensity /= intensity.ptp()
1562-
if fraction != 1.0:
1563-
intensity = fraction * (intensity - 0.5) + 0.5
1564-
if np.abs(fraction) > 1:
1565-
np.clip(intensity, 0, 1, intensity)
1556+
# Apply contrast stretch
1557+
imin, imax = intensity.min(), intensity.max()
1558+
intensity *= fraction
1559+
if np.abs(fraction) > 1:
1560+
np.clip(intensity, imin, imax, intensity)
1561+
1562+
# Rescale to 0-1, keeping range before contrast stretch
1563+
intensity -= imin
1564+
intensity /= (imax - imin)
1565+
15661566
return intensity
15671567

15681568
def shade(self, data, cmap, norm=None, blend_mode='hsv',
@@ -1608,11 +1608,8 @@ def shade(self, data, cmap, norm=None, blend_mode='hsv',
16081608
Increases or decreases the contrast of the hillshade. Values
16091609
greater than one will cause intermediate values to move closer to
16101610
full illumination or shadow (and clipping any values that move
1611-
beyond 0 or 1). Values less than one will cause full shadow or
1612-
full illumination to move closer to a value of 0.5, thereby
1613-
decreasing contrast. Note that this is not mathematically or
1614-
visually the same as increasing/decreasing the vertical
1615-
exaggeration.
1611+
beyond 0 or 1). Note that this is not visually or mathematically
1612+
the same as vertical exaggeration.
16161613
Additional kwargs are passed on to the *blend_mode* function.
16171614
16181615
Returns
@@ -1625,7 +1622,8 @@ def shade(self, data, cmap, norm=None, blend_mode='hsv',
16251622

16261623
rgb0 = cmap(norm(data))
16271624
rgb1 = self.shade_rgb(rgb0, elevation=data, blend_mode=blend_mode,
1628-
vert_exag=vert_exag, dx=dx, dy=dy, **kwargs)
1625+
vert_exag=vert_exag, dx=dx, dy=dy,
1626+
fraction=fraction, **kwargs)
16291627
# Don't overwrite the alpha channel, if present.
16301628
rgb0[..., :3] = rgb1[..., :3]
16311629
return rgb0
@@ -1649,11 +1647,8 @@ def shade_rgb(self, rgb, elevation, fraction=1., blend_mode='hsv',
16491647
Increases or decreases the contrast of the hillshade. Values
16501648
greater than one will cause intermediate values to move closer to
16511649
full illumination or shadow (and clipping any values that move
1652-
beyond 0 or 1). Values less than one will cause full shadow or
1653-
full illumination to move closer to a value of 0.5, thereby
1654-
decreasing contrast. Note that this is not mathematically or
1655-
visually the same as increasing/decreasing the vertical
1656-
exaggeration.
1650+
beyond 0 or 1). Note that this is not visually or mathematically
1651+
the same as vertical exaggeration.
16571652
blend_mode : {'hsv', 'overlay', 'soft'} or callable, optional
16581653
The type of blending used to combine the colormapped data values
16591654
with the illumination intensity. For backwards compatibility, this

0 commit comments

Comments
 (0)