Thanks to visit codestin.com
Credit goes to github.com

Skip to content

FIX: Check for colorbar creation with multi-dimensional alpha #20788

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
merged 1 commit into from
Aug 6, 2021
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
13 changes: 10 additions & 3 deletions lib/matplotlib/colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,9 @@ def __init__(self, ax, mappable=None, *, cmap=None,
extend = norm.extend
else:
extend = 'neither'
self.alpha = alpha
self.alpha = None
# Call set_alpha to handle array-like alphas properly
self.set_alpha(alpha)
self.cmap = cmap
self.norm = norm
self.values = values
Expand Down Expand Up @@ -934,8 +936,13 @@ def set_label(self, label, *, loc=None, **kwargs):
self.stale = True

def set_alpha(self, alpha):
"""Set the transparency between 0 (transparent) and 1 (opaque)."""
self.alpha = alpha
"""
Set the transparency between 0 (transparent) and 1 (opaque).

If an array is provided, *alpha* will be set to None to use the
transparency values associated with the colormap.
"""
self.alpha = None if isinstance(alpha, np.ndarray) else alpha

def _set_scale(self, scale, **kwargs):
"""
Expand Down
12 changes: 12 additions & 0 deletions lib/matplotlib/tests/test_colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,18 @@ def test_mappable_no_alpha():
plt.draw()


def test_mappable_2d_alpha():
fig, ax = plt.subplots()
x = np.arange(1, 5).reshape(2, 2)/4
pc = ax.pcolormesh(x, alpha=x)
cb = fig.colorbar(pc, ax=ax)
# The colorbar's alpha should be None and the mappable should still have
# the original alpha array
assert cb.alpha is None
assert pc.get_alpha() is x
fig.draw_no_output()


def test_colorbar_label():
"""
Test the label parameter. It should just be mapped to the xlabel/ylabel of
Expand Down