From 2d8f3be5a980450da1f2aa37f8eb9652259a07da Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Tue, 21 Sep 2021 18:49:39 +0200 Subject: [PATCH] Fix clim handling for pcolor{,mesh}. Previously, clim would be implicitly handled as part of kwargs, and therefore get overwritten when the norm is set. The solution is to set the norm first. --- lib/matplotlib/axes/_axes.py | 18 +++++------------- lib/matplotlib/tests/test_axes.py | 8 ++++++++ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index cef0f8c3c88d..f9b63e4d85c7 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -5845,12 +5845,8 @@ def pcolor(self, *args, shading=None, alpha=None, norm=None, cmap=None, kwargs.setdefault('snap', False) - collection = mcoll.PolyCollection(verts, **kwargs) - - collection.set_alpha(alpha) - collection.set_array(C) - collection.set_cmap(cmap) - collection.set_norm(norm) + collection = mcoll.PolyCollection( + verts, array=C, cmap=cmap, norm=norm, alpha=alpha, **kwargs) collection._scale_norm(norm, vmin, vmax) self._pcolor_grid_deprecation_helper() @@ -6079,14 +6075,10 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None, # convert to one dimensional array C = C.ravel() - collection = mcoll.QuadMesh( - coords, antialiased=antialiased, shading=shading, **kwargs) snap = kwargs.get('snap', rcParams['pcolormesh.snap']) - collection.set_snap(snap) - collection.set_alpha(alpha) - collection.set_array(C) - collection.set_cmap(cmap) - collection.set_norm(norm) + collection = mcoll.QuadMesh( + coords, antialiased=antialiased, shading=shading, snap=snap, + array=C, cmap=cmap, norm=norm, alpha=alpha, **kwargs) collection._scale_norm(norm, vmin, vmax) self._pcolor_grid_deprecation_helper() diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 2227af37c1d1..e96f9d5605d6 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -7232,3 +7232,11 @@ def test_empty_line_plots(): _, ax = plt.subplots() line = ax.plot([], []) assert len(line) == 1 + + +def test_clim(): + ax = plt.figure().add_subplot() + for plot_method in [ax.imshow, ax.pcolor, ax.pcolormesh, ax.pcolorfast]: + clim = (7, 8) + norm = plot_method([[0, 1], [2, 3]], clim=clim).norm + assert (norm.vmin, norm.vmax) == clim