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)