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

Skip to content

Commit 35c51d5

Browse files
committed
* Exchanged LightSource shade parameters vmin and vmax with norm
parameter. * Added LightSource test case similar to example http://matplotlib.org/examples/pylab_examples/shading_example.html
1 parent d83ba6a commit 35c51d5

2 files changed

Lines changed: 30 additions & 7 deletions

File tree

lib/matplotlib/colors.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,22 +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, vmin=None, vmax=None):
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-
if vmin is None:
1369-
vmin = data.min()
1370-
if vmax is None:
1371-
vmax = data.max()
1368+
if norm is None:
1369+
norm = Normalize(vmin=data.min(), vmax=data.max())
13721370

1373-
rgb0 = cmap((data - vmin) / (vmax - vmin))
1371+
rgb0 = cmap((data - norm.vmin) / (norm.vmax - norm.vmin))
13741372
rgb1 = self.shade_rgb(rgb0, elevation=data)
13751373
rgb0[:, :, 0:3] = rgb1
13761374
return rgb0

lib/matplotlib/tests/test_colors.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,31 @@ def gray_from_float_rgba():
204204
assert_raises(ValueError, gray_from_float_rgb)
205205
assert_raises(ValueError, gray_from_float_rgba)
206206

207+
def test_light_source_shading_color_range():
208+
# see also http://matplotlib.org/examples/pylab_examples/shading_example.html
209+
from matplotlib.colors import LightSource
210+
211+
norm = mcolors.Normalize(vmin=0, vmax=50)
212+
# test data
213+
X, Y = np.mgrid[-5:5:0.05, -5:5:0.05]
214+
Z = np.sqrt(X**2 + Y**2) + np.sin(X**2 + Y**2)
215+
# create light source object.
216+
ls = LightSource(azdeg=0, altdeg=65)
217+
# shade data, creating an rgb array.
218+
rgb = ls.shade(Z, plt.cm.jet, norm=norm)
219+
# plot un-shaded and shaded images.
220+
plt.figure(figsize=(12,5))
221+
plt.subplot(121)
222+
plt.imshow(Z, cmap=plt.cm.jet, norm=norm)
223+
plt.title('imshow')
224+
plt.xticks([])
225+
plt.yticks([])
226+
plt.subplot(122)
227+
plt.imshow(rgb)
228+
plt.title('imshow with shading')
229+
plt.xticks([])
230+
plt.yticks([])
231+
plt.draw()
207232

208233
if __name__ == '__main__':
209234
import nose

0 commit comments

Comments
 (0)