Skip to content

Commit

Permalink
Use mpl.colormaps.get_cmap for compatibility with matplotlib 3.9 (#…
Browse files Browse the repository at this point in the history
…422)

Compatibility with matplotlib 3.9: use ``colormaps.get_cmap`` from 3.6 on

Co-authored-by: Derek Homeier <[email protected]>
  • Loading branch information
pllim and dhomeier authored Feb 14, 2024
1 parent ed24ff3 commit 8886a79
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
8 changes: 6 additions & 2 deletions glue_jupyter/state_traitlets_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
from traitlets.utils.bunch import Bunch
from glue.core.state_objects import State
from glue.core import BaseCartesianData, Subset, ComponentID
from glue.utils.matplotlib import MATPLOTLIB_GE_36
from echo import CallbackList, CallbackDict
from matplotlib.colors import Colormap
from matplotlib.cm import get_cmap
if MATPLOTLIB_GE_36:
from matplotlib import colormaps
else:
from matplotlib import cm as colormaps

MAGIC_IGNORE = '611cfa3b-ebb5-42d2-b5c7-ba9bce8b51a4'

Expand Down Expand Up @@ -74,7 +78,7 @@ def update_state_from_dict(state, changes):
if changes[name] != MAGIC_IGNORE and getattr(state, name) != changes[name]:
if name == 'cmap':
if changes[name] != state.cmap.name:
setattr(state, name, get_cmap(changes[name]))
setattr(state, name, colormaps.get_cmap(changes[name]))
else:
setattr(state, name, changes[name])

Expand Down
11 changes: 8 additions & 3 deletions glue_jupyter/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@
import functools
import collections
import time
from io import BytesIO as StringIO

import ipyvue
import matplotlib
import PIL.Image
import numpy as np
from io import BytesIO as StringIO

from glue.core import BaseCartesianData
from glue.utils.matplotlib import MATPLOTLIB_GE_36

if MATPLOTLIB_GE_36:
from matplotlib import colormaps
else:
from matplotlib import cm as colormaps


def float_or_none(x):
Expand All @@ -27,7 +32,7 @@ def rgba_to_png_data(rgba):
def scalar_to_png_data(data, colormap='viridis'):
mask = ~np.isfinite(data)
intensity = np.ma.masked_array(data, mask)
colormap = matplotlib.cm.get_cmap(colormap)
colormap = colormaps.get_cmap(colormap)
colormap.set_bad(alpha=0)
data = colormap(intensity, bytes=True)
return rgba_to_png_data(data)
Expand Down

0 comments on commit 8886a79

Please sign in to comment.