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

Skip to content

[Bug]: Colorbar extend patches do not have correct alpha #22970

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

Closed
ortk95 opened this issue May 4, 2022 · 12 comments · Fixed by #23260
Closed

[Bug]: Colorbar extend patches do not have correct alpha #22970

ortk95 opened this issue May 4, 2022 · 12 comments · Fixed by #23260
Labels
Good first issue Open a pull request against these issues if there are no active ones! topic: color/alpha topic: color/colorbar
Milestone

Comments

@ortk95
Copy link

ortk95 commented May 4, 2022

Bug summary

When a colorbar has extend != 'neither', the triangles at the end of the colorbar always have alpha=1, even when the rest of the colorbar has a different alpha value.

Code for reproduction

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt

img = np.random.rand(5, 10)
alpha = 0.5

plt.subplot(1, 2, 1)
plt.imshow(img, alpha=alpha, cmap='RdBu')
plt.colorbar(extend='both', orientation='horizontal', label='Current behaviour')

# Temporary fix to manually set the alpha of the extend patches
# doesn't look perfect, but is an improvement over the current behaviour
plt.subplot(1, 2, 2)
plt.imshow(img, alpha=alpha, cmap='RdBu')
cb = plt.colorbar(extend='both', orientation='horizontal', label='Temporary fix')
for patch in cb._extend_patches:
    patch.set_alpha(alpha)

plt.show()

Actual outcome

image

Expected outcome

The triangles at the end of the colorbar should have the same alpha as the colorbar (i.e. roughly like the temporary fix above)

Additional information

No response

Operating system

OS/X

Matplotlib Version

3.5.2

Matplotlib Backend

module://matplotlib_inline.backend_inline

Python version

Python 3.10.4

Jupyter version

No response

Installation

conda

@jklymak
Copy link
Member

jklymak commented May 4, 2022

What happens if you do set_over and set_under? I actually think the behaviour of the Colorbar alpha matching the alpha of the image is questionable behaviour.

@ortk95
Copy link
Author

ortk95 commented May 4, 2022

What happens if you do set_over and set_under?

I still get the inconsistent behaviour - transparent (alpha < 1) colorbar, non-transparent (alpha = 1) triangles - with set_over/set_under:

import numpy as np
import matplotlib.pyplot as plt

img = np.random.rand(10, 10)
alpha = 0.3

cmap = plt.get_cmap('Greys').copy()
cmap.set_under('r')
cmap.set_over('b')

plt.imshow(img, alpha=alpha, cmap=cmap, vmin=0.25, vmax=0.75)
plt.colorbar(extend='both')
plt.show()

image

I actually think the behaviour of the Colorbar alpha matching the alpha of the image is questionable behaviour

I think there are cases where each behaviour can be desirable, e.g. in the image above, the greys would probably be more difficult to understand if the colorbar wasn't transparent. Maybe there should be option when creating the colorbar to specify if it takes the image's alpha or just has alpha = 1?

@ortk95
Copy link
Author

ortk95 commented May 4, 2022

I've just tested this with an older conda environment and the colorbar and triangles have consistent alpha with matplotlib 3.4.3, so this appears to be an issue introduced in a recent update
image
Figure generated using code in #22970 (comment) using matplotlib 3.4.3 (rather than 3.5.2)

@greglucas
Copy link
Contributor

You can specify an RGBA fill in the over:
cmap.set_over((1, 0, 0, 0.2))
which does show a light fill, but I tend to agree that the old way was more consistent with what someone would expect. I think this is probably due to the previous versions drawing the extends as part of the primary mesh, while now it is a separate patch.

I think the fix here is to add self.alpha into the PathPatch creation of the extends.

patch = mpatches.PathPatch(
mpath.Path(xy), facecolor=color, linewidth=0,
antialiased=False, transform=self.ax.transAxes,
hatch=hatches[0], clip_on=False,
# Place it right behind the standard patches, which is
# needed if we updated the extends
zorder=np.nextafter(self.ax.patch.zorder, -np.inf))

@jklymak
Copy link
Member

jklymak commented May 4, 2022

Yes, we definitely changed how the extents are handled.

OTOH someone, at some point, went in and put plumbing in to change the Colorbar alpha to match the mappable alpha. I guess that's OK-ish if the the mappable has one alpha, but becomes meaningless if the mappable has multiple alphas (as an image can). Better in this case would be to make the colormap have an alpha. I'm not convinced that the original feature was that great.

@greglucas greglucas added the Good first issue Open a pull request against these issues if there are no active ones! label May 4, 2022
@greglucas greglucas added this to the v3.5.3 milestone May 4, 2022
@greglucas
Copy link
Contributor

Better in this case would be to make the colormap have an alpha

I agree. The colormap does already have an alpha, but it can be out of sync with the image here since the image/mappable has a different alpha. Sorting out how to get the colormap and image to keep their alpha's in-sync may be a bit of a bigger refactor though. I think the fix I put above is a minimal update to get this working again.

@jklymak
Copy link
Member

jklymak commented May 4, 2022

Right, I guess I just think our API gets to be messy because of too many automatic hooks that then overlap in functionality.

@kkoutris
Copy link

I would like to work on this. Is anyone working on it?

@tacaswell
Copy link
Member

@kkoutris Go for it! If there is not a linked PR it is safe to assume there is not active work going on.

@kkoutris
Copy link

@tacaswell if you have the time, can you guide me a bit on what to change to fix this issue?

@jklymak
Copy link
Member

jklymak commented May 10, 2022

@kkoutris #22970 (comment) basically says how to fix the issue.

@Wikignometry
Copy link

Since I don't see a PR, @kkoutris are you still working on this or could I pick up this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Good first issue Open a pull request against these issues if there are no active ones! topic: color/alpha topic: color/colorbar
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants