Skip to content

Commit cf4dfd8

Browse files
committed
- Simplification of norm usage in shade() method
- basic example and assert statement in LightSource test
1 parent a489dbf commit cf4dfd8

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

lib/matplotlib/colors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1368,7 +1368,7 @@ def shade(self, data, cmap, norm=None):
13681368
if norm is None:
13691369
norm = Normalize(vmin=data.min(), vmax=data.max())
13701370

1371-
rgb0 = cmap((data - norm.vmin) / (norm.vmax - norm.vmin))
1371+
rgb0 = cmap(norm(data))
13721372
rgb1 = self.shade_rgb(rgb0, elevation=data)
13731373
rgb0[:, :, 0:3] = rgb1
13741374
return rgb0

lib/matplotlib/tests/test_colors.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -210,28 +210,27 @@ def test_light_source_shading_color_range():
210210
#http://matplotlib.org/examples/pylab_examples/shading_example.html
211211

212212
from matplotlib.colors import LightSource
213+
from matplotlib.colors import Normalize
213214

214-
norm = mcolors.Normalize(vmin=0, vmax=50)
215-
# test data
216-
X, Y = np.mgrid[-5:5:0.05, -5:5:0.05]
217-
Z = np.sqrt(X**2 + Y**2) + np.sin(X**2 + Y**2)
218-
# create light source object.
215+
refinput = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
216+
norm = Normalize(vmin=0, vmax=50)
219217
ls = LightSource(azdeg=0, altdeg=65)
220-
# shade data, creating an rgb array.
221-
rgb = ls.shade(Z, plt.cm.jet, norm=norm)
222-
# plot un-shaded and shaded images.
223-
plt.figure(figsize=(12, 5))
224-
plt.subplot(121)
225-
plt.imshow(Z, cmap=plt.cm.jet, norm=norm)
226-
plt.title('imshow')
227-
plt.xticks([])
228-
plt.yticks([])
229-
plt.subplot(122)
230-
plt.imshow(rgb)
231-
plt.title('imshow with shading')
232-
plt.xticks([])
233-
plt.yticks([])
234-
plt.draw()
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)
235234

236235

237236
if __name__ == '__main__':

0 commit comments

Comments
 (0)