Skip to content

Commit 1a9fa9e

Browse files
committed
Merge pull request matplotlib#2650 from syngron/master
Lightsource shade method parameters for color range definition
2 parents 4f5bc02 + cf4dfd8 commit 1a9fa9e

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

lib/matplotlib/colors.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,17 +1355,20 @@ def __init__(self, azdeg=315, altdeg=45,
13551355
self.hsv_min_sat = hsv_min_sat
13561356
self.hsv_max_sat = hsv_max_sat
13571357

1358-
def shade(self, data, cmap):
1358+
def shade(self, data, cmap, norm=None):
13591359
"""
13601360
Take the input data array, convert to HSV values in the
13611361
given colormap, then adjust those color values
1362-
to given the impression of a shaded relief map with a
1362+
to give the impression of a shaded relief map with a
13631363
specified light source.
13641364
RGBA values are returned, which can then be used to
13651365
plot the shaded image with imshow.
13661366
"""
13671367

1368-
rgb0 = cmap((data - data.min()) / (data.max() - data.min()))
1368+
if norm is None:
1369+
norm = Normalize(vmin=data.min(), vmax=data.max())
1370+
1371+
rgb0 = cmap(norm(data))
13691372
rgb1 = self.shade_rgb(rgb0, elevation=data)
13701373
rgb0[:, :, 0:3] = rgb1
13711374
return rgb0

lib/matplotlib/tests/test_colors.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,34 @@ def gray_from_float_rgba():
205205
assert_raises(ValueError, gray_from_float_rgba)
206206

207207

208+
def test_light_source_shading_color_range():
209+
# see also
210+
#http://matplotlib.org/examples/pylab_examples/shading_example.html
211+
212+
from matplotlib.colors import LightSource
213+
from matplotlib.colors import Normalize
214+
215+
refinput = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
216+
norm = Normalize(vmin=0, vmax=50)
217+
ls = LightSource(azdeg=0, altdeg=65)
218+
testoutput = ls.shade(refinput, plt.cm.jet, norm=norm)
219+
refoutput = np.array([
220+
[[0., 0., 0.58912656, 1.],
221+
[0., 0., 0.67825312, 1.],
222+
[0., 0., 0.76737968, 1.],
223+
[0., 0., 0.85650624, 1.]],
224+
[[0., 0., 0.9456328, 1.],
225+
[0., 0., 1., 1.],
226+
[0., 0.04901961, 1., 1.],
227+
[0., 0.12745098, 1., 1.]],
228+
[[0., 0.22156863, 1., 1.],
229+
[0., 0.3, 1., 1.],
230+
[0., 0.37843137, 1., 1.],
231+
[0., 0.45686275, 1., 1.]]
232+
])
233+
assert_array_almost_equal(refoutput, testoutput)
234+
235+
208236
if __name__ == '__main__':
209237
import nose
210238
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

0 commit comments

Comments
 (0)