From 0b56e073cc5768f6a940f430b6bc18e5cc17b8bd Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Thu, 1 Feb 2024 09:07:17 +0100 Subject: [PATCH] Add an rcparam for image.interpolation_stage. --- doc/users/next_whats_new/interpolation_stage_rc.rst | 4 ++++ lib/matplotlib/image.py | 5 ++--- lib/matplotlib/mpl-data/matplotlibrc | 1 + lib/matplotlib/rcsetup.py | 13 +++++++------ lib/matplotlib/tests/test_image.py | 9 +++++++++ 5 files changed, 23 insertions(+), 9 deletions(-) create mode 100644 doc/users/next_whats_new/interpolation_stage_rc.rst diff --git a/doc/users/next_whats_new/interpolation_stage_rc.rst b/doc/users/next_whats_new/interpolation_stage_rc.rst new file mode 100644 index 000000000000..bd3ecc563e5d --- /dev/null +++ b/doc/users/next_whats_new/interpolation_stage_rc.rst @@ -0,0 +1,4 @@ +``image.interpolation_stage`` rcParam +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +This new rcParam controls whether image interpolation occurs in "data" space or +in "rgba" space. diff --git a/lib/matplotlib/image.py b/lib/matplotlib/image.py index 3741c2cdecb3..5b0152505397 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -789,10 +789,9 @@ def set_interpolation_stage(self, s): ---------- s : {'data', 'rgba'} or None Whether to apply up/downsampling interpolation in data or RGBA - space. + space. If None, use :rc:`image.interpolation_stage`. """ - if s is None: - s = "data" # placeholder for maybe having rcParam + s = mpl._val_or_rc(s, 'image.interpolation_stage') _api.check_in_list(['data', 'rgba'], s=s) self._interpolation_stage = s self.stale = True diff --git a/lib/matplotlib/mpl-data/matplotlibrc b/lib/matplotlib/mpl-data/matplotlibrc index 301afc38456b..29ffb20f4280 100644 --- a/lib/matplotlib/mpl-data/matplotlibrc +++ b/lib/matplotlib/mpl-data/matplotlibrc @@ -603,6 +603,7 @@ ## *************************************************************************** #image.aspect: equal # {equal, auto} or a number #image.interpolation: antialiased # see help(imshow) for options +#image.interpolation_stage: data # see help(imshow) for options #image.cmap: viridis # A colormap name (plasma, magma, etc.) #image.lut: 256 # the size of the colormap lookup table #image.origin: upper # {lower, upper} diff --git a/lib/matplotlib/rcsetup.py b/lib/matplotlib/rcsetup.py index 6abc8372222d..a326d22f039a 100644 --- a/lib/matplotlib/rcsetup.py +++ b/lib/matplotlib/rcsetup.py @@ -1051,12 +1051,13 @@ def _convert_validator_spec(key, conv): "bb", "frak", "scr", "regular"], "mathtext.fallback": _validate_mathtext_fallback, - "image.aspect": validate_aspect, # equal, auto, a number - "image.interpolation": validate_string, - "image.cmap": _validate_cmap, # gray, jet, etc. - "image.lut": validate_int, # lookup table - "image.origin": ["upper", "lower"], - "image.resample": validate_bool, + "image.aspect": validate_aspect, # equal, auto, a number + "image.interpolation": validate_string, + "image.interpolation_stage": ["data", "rgba"], + "image.cmap": _validate_cmap, # gray, jet, etc. + "image.lut": validate_int, # lookup table + "image.origin": ["upper", "lower"], + "image.resample": validate_bool, # Specify whether vector graphics backends will combine all images on a # set of Axes into a single composite image "image.composite_image": validate_bool, diff --git a/lib/matplotlib/tests/test_image.py b/lib/matplotlib/tests/test_image.py index 17d0c011ba63..232790a68ebb 100644 --- a/lib/matplotlib/tests/test_image.py +++ b/lib/matplotlib/tests/test_image.py @@ -1462,6 +1462,15 @@ def test_rgba_antialias(): cmap=cmap, vmin=-1.2, vmax=1.2) +def test_rc_interpolation_stage(): + for val in ["data", "rgba"]: + with mpl.rc_context({"image.interpolation_stage": val}): + assert plt.imshow([[1, 2]]).get_interpolation_stage() == val + for val in ["DATA", "foo", None]: + with pytest.raises(ValueError): + mpl.rcParams["image.interpolation_stage"] = val + + # We check for the warning with a draw() in the test, but we also need to # filter the warning as it is emitted by the figure test decorator @pytest.mark.filterwarnings(r'ignore:Data with more than .* '