Skip to content

Backport PR #13966 on branch v3.1.x (Fix colorbar setting without artist) #13975

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/matplotlib/cm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 13 additions & 3 deletions lib/matplotlib/tests/test_colorbar.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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()
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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()