From ebef85d2a0a15926c07ea02a8dea11a41c1cbcd2 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Wed, 17 Apr 2019 12:20:34 +0200 Subject: [PATCH] Backport PR #13966: Fix colorbar setting without artist --- lib/matplotlib/cm.py | 11 +++++++++++ lib/matplotlib/tests/test_colorbar.py | 16 +++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/cm.py b/lib/matplotlib/cm.py index 26b1766e84dd..94cf2ceee24c 100644 --- a/lib/matplotlib/cm.py +++ b/lib/matplotlib/cm.py @@ -92,6 +92,7 @@ def _generate_cmap(name, lutsize): else: return colors.LinearSegmentedColormap.from_list(name, spec, lutsize) + LUTSIZE = mpl.rcParams['image.lut'] # Generate the reversed specifications (all at once, to avoid @@ -331,6 +332,16 @@ def set_clim(self, vmin=None, vmax=None): self.norm.vmax = colors._sanitize_extrema(vmax) self.changed() + def get_alpha(self): + """ + Returns + ------- + alpha : float + Always returns 1. + """ + # This method is intended to be overridden by Artist sub-classes + return 1. + def set_cmap(self, cmap): """ set the colormap for luminance data diff --git a/lib/matplotlib/tests/test_colorbar.py b/lib/matplotlib/tests/test_colorbar.py index 0244101ebcb0..c65ad02c31a5 100644 --- a/lib/matplotlib/tests/test_colorbar.py +++ b/lib/matplotlib/tests/test_colorbar.py @@ -1,12 +1,14 @@ import numpy as np import pytest +from matplotlib import cm +import matplotlib.colors as mcolors + from matplotlib import rc_context from matplotlib.testing.decorators import image_comparison import matplotlib.pyplot as plt from matplotlib.colors import (BoundaryNorm, LogNorm, PowerNorm, Normalize, DivergingNorm) -from matplotlib.cm import get_cmap from matplotlib.colorbar import ColorbarBase, _ColorbarLogLocator from matplotlib.ticker import LogLocator, LogFormatter, FixedLocator @@ -20,7 +22,7 @@ def _get_cmap_norms(): colorbar_extension_length. """ # Create a color map and specify the levels it represents. - cmap = get_cmap("RdBu", lut=5) + cmap = cm.get_cmap("RdBu", lut=5) clevs = [-5., -2.5, -.5, .5, 1.5, 3.5] # Define norms for the color maps. norms = dict() @@ -240,7 +242,7 @@ def test_colorbar_closed_patch(): ax4 = fig.add_axes([0.05, 0.25, 0.9, 0.1]) ax5 = fig.add_axes([0.05, 0.05, 0.9, 0.1]) - cmap = get_cmap("RdBu", lut=5) + cmap = cm.get_cmap("RdBu", lut=5) im = ax1.pcolormesh(np.linspace(0, 10, 16).reshape((4, 4)), cmap=cmap) @@ -558,3 +560,11 @@ def test_extend_colorbar_customnorm(): cb = fig.colorbar(pcm, ax=ax[0], extend='both') np.testing.assert_allclose(cb.ax.get_position().extents, [0.78375, 0.536364, 0.796147, 0.9], rtol=1e-3) + + +def test_mappable_no_alpha(): + fig, ax = plt.subplots() + sm = cm.ScalarMappable(norm=mcolors.Normalize(), cmap='viridis') + fig.colorbar(sm) + sm.set_cmap('plasma') + plt.draw()