From 142ff280d90c65c949d1f724a0583b7ac76f2e24 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Tue, 16 Apr 2019 10:09:34 +0100 Subject: [PATCH 1/2] Fix colorbar setting without artist --- lib/matplotlib/cm.py | 9 +++++++++ lib/matplotlib/tests/test_colorbar.py | 16 +++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/cm.py b/lib/matplotlib/cm.py index 030612f724a2..66c25fada51b 100644 --- a/lib/matplotlib/cm.py +++ b/lib/matplotlib/cm.py @@ -325,6 +325,15 @@ 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. + """ + 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() From 3d11b3fa3bbdaeb56b63c15d3ec87b52738d2925 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Wed, 17 Apr 2019 10:27:40 +0100 Subject: [PATCH 2/2] Add comment to get_alpha --- lib/matplotlib/cm.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/matplotlib/cm.py b/lib/matplotlib/cm.py index 66c25fada51b..5e3358b9f2b4 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 @@ -332,6 +333,7 @@ def get_alpha(self): alpha : float Always returns 1. """ + # This method is intended to be overridden by Artist sub-classes return 1. def set_cmap(self, cmap):