Skip to content

Commit

Permalink
Trac #33491: fix usage of Matplotlib color maps
Browse files Browse the repository at this point in the history
Matplotlib used to have a global dictionary of color maps. By now, it is
considered internal API and does not contain newer color maps anymore,
so some of them cannot be used in Sage:

{{{
sage: density_plot(x, (-1,1), (-1,1), cmap='turbo')
/usr/lib/python3.10/site-
packages/sage/repl/rich_output/display_manager.py:608: RichReprWarning:
Exception in _rich_repr_ while displaying object:
Color map turbo not known (type import matplotlib.cm;
matplotlib.cm.datad.keys() for valid names)
}}}

URL: https://trac.sagemath.org/33491
Reported by: gh-mwageringel
Ticket author(s): Markus Wageringel
Reviewer(s): Travis Scrimshaw
  • Loading branch information
Release Manager committed Mar 26, 2022
2 parents 29f7b5a + 2022ed6 commit cbd71f5
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions src/sage/plot/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
For a list of color maps in Sage, evaluate::
sage: sorted(colormaps)
['Accent', 'Blues', 'BrBG', ...]
['Accent', ...]
These are imported from matplotlib's cm_ module.
These are imported from matplotlib's colormaps_ collection.
.. _cm: http://matplotlib.sourceforge.net/api/cm_api.html
.. _colormaps: https://matplotlib.org/stable/gallery/color/colormap_reference.html
"""

# ****************************************************************************
Expand All @@ -41,10 +41,6 @@
from colorsys import hsv_to_rgb, hls_to_rgb, rgb_to_hsv, rgb_to_hls


# matplotlib color maps, loaded on-demand
cm = None


colors_dict = {
'automatic' : '#add8e6', # 173, 216, 230
'aliceblue' : '#f0f8ff', # 240, 248, 255
Expand Down Expand Up @@ -1332,7 +1328,7 @@ def get_cmap(cmap):
and color names. For a list of map names, evaluate::
sage: sorted(colormaps)
['Accent', 'Blues', ...]
['Accent', ...]
See :func:`rgbcolor` for valid list/tuple element formats.
Expand Down Expand Up @@ -1360,25 +1356,31 @@ def get_cmap(cmap):
sage: get_cmap('jolies')
Traceback (most recent call last):
...
RuntimeError: Color map jolies not known (type import matplotlib.cm; matplotlib.cm.datad.keys() for valid names)
RuntimeError: Color map jolies not known (type "import matplotlib; list(matplotlib.colormaps.keys())" for valid names)
sage: get_cmap('mpl')
Traceback (most recent call last):
...
RuntimeError: Color map mpl not known (type import matplotlib.cm; matplotlib.cm.datad.keys() for valid names)
RuntimeError: Color map mpl not known (type "import matplotlib; list(matplotlib.colormaps.keys())" for valid names)
TESTS:
Check that :trac:`33491` is fixed::
sage: get_cmap('turbo')
<matplotlib.colors.ListedColormap object at 0x...>
"""
# matplotlib color maps
global cm
if not cm:
from matplotlib import cm
from matplotlib.colors import ListedColormap, Colormap

if isinstance(cmap, Colormap):
return cmap

elif isinstance(cmap, str):
if cmap not in cm.datad:
raise RuntimeError("Color map %s not known (type import matplotlib.cm; matplotlib.cm.datad.keys() for valid names)" % cmap)
return cm.__dict__[cmap]
from matplotlib import colormaps
try:
return colormaps[cmap]
except KeyError:
raise RuntimeError('Color map %s not known (type "import matplotlib; list(matplotlib.colormaps.keys())" for valid names)' % cmap)

elif isinstance(cmap, (list, tuple)):
cmap = [rgbcolor(_) for _ in cmap]
Expand Down Expand Up @@ -1424,7 +1426,7 @@ class Colormaps(MutableMapping):
For a list of map names, evaluate::
sage: sorted(colormaps)
['Accent', 'Blues', ...]
['Accent', ...]
"""
def __init__(self):
"""
Expand Down Expand Up @@ -1453,13 +1455,12 @@ def load_maps(self):
sage: maps.load_maps()
sage: len(maps.maps)>60
True
sage: 'viridis' in maps
True
"""
global cm
if not cm:
from matplotlib import cm
if not self.maps:
for cmap in cm.datad:
self.maps[cmap] = cm.__getattribute__(cmap)
from matplotlib import colormaps
self.maps.update(colormaps)

def __dir__(self):
"""
Expand Down

0 comments on commit cbd71f5

Please sign in to comment.