8000 Fix colorbar setting without artist · matplotlib/matplotlib@81ef307 · GitHub
[go: up one dir, main page]

Skip to content

Commit 81ef307

Browse files
committed
Fix colorbar setting without artist
1 parent 557cdfa commit 81ef307

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

lib/matplotlib/colorbar.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1232,7 +1232,8 @@ def update_normal(self, mappable):
12321232

12331233
_log.debug('colorbar update normal %r %r', mappable.norm, self.norm)
12341234
self.mappable = mappable
1235-
self.set_alpha(mappable.get_alpha())
1235+
if hasattr(mappable, 'get_alpha'):
1236+
self.set_alpha(mappable.get_alpha())
12361237
self.cmap = mappable.cmap
12371238
if mappable.norm != self.norm:
12381239
self.norm = mappable.norm

lib/matplotlib/tests/test_colorbar.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
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
@@ -20,7 +23,7 @@ def _get_cmap_norms():
2023
colorbar_extension_length.
2124
"""
2225
# Create a color map and specify the levels it represents.
23-
cmap = get_cmap("RdBu", lut=5)
26+
cmap = cm.get_cmap("RdBu", lut=5)
2427
clevs = [-5., -2.5, -.5, .5, 1.5, 3.5]
2528
# Define norms for the color maps.
2629
norms = dict()
@@ -240,7 +243,7 @@ def test_colorbar_closed_patch():
240243
ax4 = fig.add_axes([0.05, 0.25, 0.9, 0.1])
241244
ax5 = fig.add_axes([0.05, 0.05, 0.9, 0.1])
242245

243-
cmap = get_cmap("RdBu", lut=5)
246+
cmap = cm.get_cmap("RdBu", lut=5)
244247

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

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

0 commit comments

Comments
 (0)
0