You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug]: pcolormesh passes alpha value down to colormap instead of alpha blending #29761
a colormap that uses the alpha channel for color mapping
results in the colormap using the alpha parameter from pcolormesh as a constant.
Expected result would be that all resulting values from the colormapping are additionally blended with the alpha parameter from pcolormesh.
Code for reproduction
importmatplotlib.pyplotaspltimportmatplotlib.colorscdict={'red':[(0.0, None, 1.0),
(1.0, 1.0, None)],
'green': [(0.0, None, 0.0),
(1.0, 0.0, None)],
'blue': [(0.0, None, 0.0),
(1.0, 0.0, None)],
'alpha': [(0.0, None, 0.0),
(1.0, 1.0, None)]}
cmap=matplotlib.colors.LinearSegmentedColormap('custom',cdict,256)
H= [[1,2,3]]
yedges= [0,1]
xedges= [0,1,2,3]
f, (ax1, ax2) =plt.subplots(2, 1)
# case 1: no alpha, colormap defines alpha of valuesax1.pcolormesh(xedges, yedges, H, cmap=cmap)
ax1.set_title("pcolormesh without alpha parameter")
# case 2: with alpha, alpha is passed to colormap and every value has the same alphaax2.pcolormesh(xedges, yedges, H, cmap=cmap, alpha=0.5)
ax2.set_title("pcolormesh with alpha parameter 0.5")
plt.show()
Actual outcome
Expected outcome
The expected outcome is, that the "normal" output of pcolormesh is additionally alpha blended.
Additional information
This happens when an alpha parameter is passed to pcolormesh.
I suspect that the alpha parameter is passed to the colormap call, which effectively sets the colormap result alpha value to a constant.
There was some recent discussion in #28437 about how the various alphas should interact. I think different people have different expectations for whether one should take precedence, whether we should blend, a combination of some sort. It would be good to handle this consistently throughout the library.
I do find it a bit odd that we have a colormap with alpha, but then if blend we actually get different alphas rendered. I'm thinking about the case where we add a colorbar to this only red linear alpha ramp. If we blend, the image values would not map to the colorbar values anymore because that blending stage happens later.
Indeed, colormapping with alpha is problematic. In particular with the white-to-read colormap, adding alpha makes the values more whitish, which gives the impression of a value shift.
My use case was to create multiple 2d histograms that are rendered on top of each other.
Therefore, I was confused about the resulting blended image.
After figuring out the behavior, I solved it by adjusting the maximum alpha value in the colormap instead of using the alpha parameter of pcolormesh.
Activity
greglucas commentedon Mar 16, 2025
There was some recent discussion in #28437 about how the various alphas should interact. I think different people have different expectations for whether one should take precedence, whether we should blend, a combination of some sort. It would be good to handle this consistently throughout the library.
I do find it a bit odd that we have a colormap with alpha, but then if blend we actually get different alphas rendered. I'm thinking about the case where we add a colorbar to this only red linear alpha ramp. If we blend, the image values would not map to the colorbar values anymore because that blending stage happens later.
jklymak commentedon Mar 16, 2025
As a workaround, you can convert your data to rgba (data->norm->colormap) apply your alpha mask to the alpha channel, and pass to pcolormesh as rgba.
timhoffm commentedon Mar 16, 2025
Indeed, colormapping with alpha is problematic. In particular with the white-to-read colormap, adding alpha makes the values more whitish, which gives the impression of a value shift.
aw32 commentedon Mar 18, 2025
My use case was to create multiple 2d histograms that are rendered on top of each other.
Therefore, I was confused about the resulting blended image.
After figuring out the behavior, I solved it by adjusting the maximum alpha value in the colormap instead of using the alpha parameter of pcolormesh.
dikshajangra12918-oss commentedon Apr 19, 2026
@timhoffm
Is there a clear direction for fixing this? I'd like to work on it for GSoC.
timhoffm commentedon Apr 19, 2026
@dikshajangra12918-oss no there isn’t.
Also note that the GSoC application deadline was March 31.