From dc2e073e429068c178ec6bccc971d5736fc4e560 Mon Sep 17 00:00:00 2001 From: Thor Whalen <1906276+thorwhalen@users.noreply.github.com> Date: Mon, 7 Sep 2026 01:13:36 +0200 Subject: [PATCH] Use plt.get_cmap instead of removed-in-3.11 plt.cm.get_cmap `scatter_and_color_according_to_y` resolved its colormap via `plt.cm.get_cmap` (i.e. `matplotlib.cm.get_cmap`), which matplotlib deprecated in 3.7 with `removal='3.11'`. Since the package pins no matplotlib upper bound, the continuous-`y` branch of this public, `__init__`-exported function would start raising `AttributeError` as soon as users pick up 3.11. Switch to `plt.get_cmap`, the non-deprecated spelling matplotlib's own warning points at. Verified equivalent under matplotlib 3.10 for every argument form the call site can produce -- str, `Colormap`, and `None` all yield the same colormap type and an identical 256-sample lookup table, and invalid input still raises `ValueError` from both spellings. The change is therefore observationally inert apart from dropping the warning. Adds a regression test asserting the float-`y` path emits no `MatplotlibDeprecationWarning`; it fails on the unmodified code with the `get_cmap ... will be removed in 3.11` message and passes after. Suite: 74 passed before, 75 passed after (the one new test). Two pre-existing failures under the CI `--doctest-modules` invocation are untouched and unrelated: `oplot/my.py` fails to import (`nympy` typo) and the `plot_stats.smooth_scores` doctest fails; both reproduce identically on the unmodified tree. Claude-Session: https://claude.ai/code/session_01L1aQPB34n7PU7jmbztSjBe --- oplot/plot_data_set.py | 2 +- tests/test_plot_data_set.py | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/oplot/plot_data_set.py b/oplot/plot_data_set.py index 5e87911..62da3de 100644 --- a/oplot/plot_data_set.py +++ b/oplot/plot_data_set.py @@ -220,7 +220,7 @@ def scatter_and_color_according_to_y( elif isinstance(y[0], float): legend = False - cm = plt.cm.get_cmap(cmap_col) + cm = plt.get_cmap(cmap_col) colors = None col = 'continuous' no_tag = True diff --git a/tests/test_plot_data_set.py b/tests/test_plot_data_set.py index 800d671..0319a50 100644 --- a/tests/test_plot_data_set.py +++ b/tests/test_plot_data_set.py @@ -1,8 +1,11 @@ """Tests for oplot.plot_data_set module""" +import warnings + import pytest import numpy as np import matplotlib.pyplot as plt +from matplotlib import MatplotlibDeprecationWarning from matplotlib.axes import Axes from oplot.plot_data_set import ( @@ -112,6 +115,27 @@ def test_scatter_with_float_y(self): scatter_and_color_according_to_y(X, y, projection='2d', dim_reduct='PCA') plt.close('all') + def test_scatter_with_float_y_emits_no_deprecation_warning(self): + """Continuous-y colouring must not rely on deprecated matplotlib APIs. + + ``matplotlib.cm.get_cmap`` (a.k.a. ``plt.cm.get_cmap``) is deprecated + since matplotlib 3.7 and slated for removal in 3.11, which would turn + the float-``y`` branch into an ``AttributeError``. + """ + np.random.seed(42) + X = np.random.rand(50, 4) + y = np.random.rand(50) + with warnings.catch_warnings(record=True) as recorded: + warnings.simplefilter('always') + scatter_and_color_according_to_y(X, y, projection='2d', dim_reduct='PCA') + plt.close('all') + deprecations = [ + str(w.message) + for w in recorded + if issubclass(w.category, MatplotlibDeprecationWarning) + ] + assert not deprecations, f'deprecated matplotlib API used: {deprecations}' + def test_scatter_1d(self): """Test 1D scatter plot""" np.random.seed(42)