Skip to content

Commit

Permalink
Fix matplotlib.colormaps
Browse files Browse the repository at this point in the history
  • Loading branch information
hhoppe committed Jan 4, 2023
1 parent 01fdedc commit a0f0583
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
8 changes: 5 additions & 3 deletions mediapy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
"""

__docformat__ = 'google'
__version__ = '1.1.3'
__version__ = '1.1.4'
__version_info__ = tuple(int(num) for num in __version__.split('.'))

import base64
Expand All @@ -124,7 +124,6 @@

import IPython.display
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import numpy.typing as npt
import PIL.Image
Expand Down Expand Up @@ -756,7 +755,10 @@ def to_rgb(
vmax = np.amax(np.where(np.isfinite(a), a, -np.inf)) if vmax is None else vmax
a = (a.astype('float') - vmin) / (vmax - vmin + np.finfo(float).eps)
if isinstance(cmap, str):
rgb_from_scalar = matplotlib.colormaps[cmap]
if hasattr(matplotlib, 'colormaps'):
rgb_from_scalar = matplotlib.colormaps[cmap] # Newer version.
else:
rgb_from_scalar = matplotlib.pyplot.cm.get_cmap(cmap)
else:
rgb_from_scalar = cmap
a = rgb_from_scalar(a)
Expand Down
6 changes: 5 additions & 1 deletion mediapy_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,11 @@ def test_uint8_to_rgb(self):
a = np.array([100, 120, 140], dtype=np.uint8)

def gray(x):
return matplotlib.colormaps['gray'](x)[..., :3]
if hasattr(matplotlib, 'colormaps'):
cmap = matplotlib.colormaps['gray'] # Newer version.
else:
cmap = matplotlib.pyplot.cm.get_cmap('gray')
return cmap(x)[..., :3]

self.assert_all_close(media.to_rgb(a), gray([0.0, 0.5, 1.0]))
self.assert_all_close(
Expand Down

0 comments on commit a0f0583

Please sign in to comment.