From 618da9e886a6cdd62d7c7f5d8cc692ced38a849d Mon Sep 17 00:00:00 2001 From: "P. L. Lim" <2090236+pllim@users.noreply.github.com> Date: Tue, 13 Feb 2024 17:09:49 -0500 Subject: [PATCH 1/4] MNT: Compatibility with matplotlib 3.9 --- glue_jupyter/state_traitlets_helpers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/glue_jupyter/state_traitlets_helpers.py b/glue_jupyter/state_traitlets_helpers.py index f76a4405..94f899aa 100644 --- a/glue_jupyter/state_traitlets_helpers.py +++ b/glue_jupyter/state_traitlets_helpers.py @@ -1,4 +1,5 @@ import json +import matplotlib import traitlets from collections import defaultdict from traitlets.utils.bunch import Bunch @@ -6,7 +7,6 @@ from glue.core import BaseCartesianData, Subset, ComponentID from echo import CallbackList, CallbackDict from matplotlib.colors import Colormap -from matplotlib.cm import get_cmap MAGIC_IGNORE = '611cfa3b-ebb5-42d2-b5c7-ba9bce8b51a4' @@ -74,7 +74,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, matplotlib.colormaps[changes[name]]) else: setattr(state, name, changes[name]) From c5e5d34b2695e0f1de4e4efc377b5c394e0e4ce3 Mon Sep 17 00:00:00 2001 From: "P. L. Lim" <2090236+pllim@users.noreply.github.com> Date: Wed, 14 Feb 2024 11:29:14 -0500 Subject: [PATCH 2/4] Backward compatibility Co-authored-by: Derek Homeier <709020+dhomeier@users.noreply.github.com> --- glue_jupyter/state_traitlets_helpers.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/glue_jupyter/state_traitlets_helpers.py b/glue_jupyter/state_traitlets_helpers.py index 94f899aa..d3d1f793 100644 --- a/glue_jupyter/state_traitlets_helpers.py +++ b/glue_jupyter/state_traitlets_helpers.py @@ -1,12 +1,16 @@ import json -import matplotlib import traitlets from collections import defaultdict 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 +if MATPLOTLIB_GE_36: + from matplotlib import colormaps +else: + from matplotlib import cm as colormaps MAGIC_IGNORE = '611cfa3b-ebb5-42d2-b5c7-ba9bce8b51a4' @@ -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, matplotlib.colormaps[changes[name]]) + setattr(state, name, colormap.get_cmap(changes[name])) else: setattr(state, name, changes[name]) From e46cf11549915bea03b496c84a4368584daf2963 Mon Sep 17 00:00:00 2001 From: "P. L. Lim" <2090236+pllim@users.noreply.github.com> Date: Wed, 14 Feb 2024 11:29:52 -0500 Subject: [PATCH 3/4] Fix typo --- glue_jupyter/state_traitlets_helpers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/glue_jupyter/state_traitlets_helpers.py b/glue_jupyter/state_traitlets_helpers.py index d3d1f793..d4f39862 100644 --- a/glue_jupyter/state_traitlets_helpers.py +++ b/glue_jupyter/state_traitlets_helpers.py @@ -78,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, colormap.get_cmap(changes[name])) + setattr(state, name, colormaps.get_cmap(changes[name])) else: setattr(state, name, changes[name]) From 0175678ca8e5937b88b08bd5229287aae8152ff1 Mon Sep 17 00:00:00 2001 From: "P. L. Lim" <2090236+pllim@users.noreply.github.com> Date: Wed, 14 Feb 2024 11:33:08 -0500 Subject: [PATCH 4/4] matplotlib compat in utils.py --- glue_jupyter/utils.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/glue_jupyter/utils.py b/glue_jupyter/utils.py index c2479235..e77ab8dd 100644 --- a/glue_jupyter/utils.py +++ b/glue_jupyter/utils.py @@ -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): @@ -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)