8000 Merge pull request #13966 from dstansby/custom-cbar · matplotlib/matplotlib@366a5d1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 366a5d1

Browse files
authored
Merge pull request #13966 from dstansby/custom-cbar
Fix colorbar setting without artist
2 parents 2f046b4 + 3d11b3f commit 366a5d1

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

lib/matplotlib/cm.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ def _generate_cmap(name, lutsize):
9292
else:
9393
return colors.LinearSegmentedColormap.from_list(name, spec, lutsize)
9494

95+
9596
LUTSIZE = mpl.rcParams['image.lut']
9697

9798
# Generate the reversed specifications (all at once, to avoid
@@ -325,6 +326,16 @@ def set_clim(self, vmin=None, vmax=None):
325326
self.norm.vmax = colors._sanitize_extrema(vmax)
326327
self.changed()
327328

329+
def get_alpha(self):
330+
"""
331+
Returns
332+
-------
333+
alpha : float
334+
Always returns 1.
335+
"""
336+
# This method is intended to be overridden by Artist sub-classes
337+
return 1.
338+
328339
def set_cmap(self, cmap):
329340
"""
330341
set the colormap for luminance data

lib/matplotlib/tests/test_colorbar.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import numpy as np
22
import pytest
33

4+
from matplotlib import cm
5+
import matplotlib.colors as mcolors
6+
47
from matplotlib import rc_context
58
from matplotlib.testing.decorators import image_comparison
69
import matplotlib.pyplot as plt
710
from matplotlib.colors import (BoundaryNorm, LogNorm, PowerNorm, Normalize,
811
DivergingNorm)
9-
from matplotlib.cm import get_cmap
1012
from matplotlib.colorbar import ColorbarBase, _ColorbarLogLocator
1113
from matplotlib.ticker import LogLocator, LogFormatter, FixedLocator
1214

@@ -20,7 +22,7 @@ def _get_cmap_norms():
2022
colorbar_extension_length.
2123
"""
2224
# Create a color map and specify the levels it represents.
23-
cmap = get_cmap("RdBu", lut=5)
25+
cmap = cm.get_cmap("RdBu", lut=5)
2426
clevs = [-5., -2.5, -.5, .5, 1.5, 3.5]
2527
# Define norms for the color maps.
2628
norms = dict()
@@ -240,7 +242,7 @@ def test_colorbar_closed_patch():
240242
ax4 = fig.add_axes([0.05, 0.25, 0.9, 0.1])
241243
ax5 = fig.add_axes([0.05, 0.05, 0.9, 0.1])
242244

243-
cmap = get_cmap("RdBu", lut=5)
245+
cmap = cm.get_cmap("RdBu", lut=5)
244246

245247
im = ax1.pcolormesh(np.linspace(0, 10, 16).reshape((4, 4)), cmap=cmap)
246248

@@ -558,3 +560,11 @@ def test_extend_colorbar_customnorm():
558560
cb = fig.colorbar(pcm, ax=ax[0], extend='both')
559561
np.testing.assert_allclose(cb.ax.get_position().extents,
560562
[0.78375, 0.536364, 0.796147, 0.9], rtol=1e-3)
563+
564+
565+
def test_mappable_no_alpha():
566+
fig, ax = plt.subplots()
567+
sm = cm.ScalarMappable(norm=mcolors.Normalize(), cmap='viridis')
568+
fig.colorbar(sm)
569+
sm.set_cmap('plasma')
570+
plt.draw()

0 commit comments

Comments
 (0)
0