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

Skip to content

Commit badc5b9

Browse files
committed
Add new method Colormap.with_alpha()
We currently do not have a way of adjusting only the transparency of the mapped colors of the artist, e.g. the fill in contourf(). The Artist alpha parameter also affects lines and hatches. The simplest solution is to support creating semi-transparent colormaps, which can then be used via `contourf(..., cmap=plt.colormaps['jet'].with_alpha(0.5))`. See also #29044 (comment).
1 parent 685ea2b commit badc5b9

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Tuning transparency of colormaps
2+
--------------------------------
3+
The new method `.Colormap.with_alpha` allows to create a new colormap with the same
4+
color values but a new uniform alpha value. This is handy if you want to modify only
5+
the transparency of mapped colors for an Artist.

lib/matplotlib/colors.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,21 @@ def _set_extremes(self):
925925
self._lut[self._i_over] = self._lut[self.N - 1]
926926
self._lut[self._i_bad] = self._rgba_bad
927927

928+
def with_alpha(self, alpha):
929+
"""
930+
Return a copy of the colormap with a new uniform transparency.
931+
932+
Parameters
933+
----------
934+
alpha : float
935+
The alpha blending value, between 0 (transparent) and 1 (opaque).
936+
"""
937+
new_cm = self.copy()
938+
if not new_cm._isinit:
939+
new_cm._init()
940+
new_cm._lut[:, 3] = alpha
941+
return new_cm
942+
928943
def _init(self):
929944
"""Generate the lookup table, ``self._lut``."""
930945
raise NotImplementedError("Abstract class only")

lib/matplotlib/colors.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ class Colormap:
103103
under: ColorType | None = ...,
104104
over: ColorType | None = ...
105105
) -> Colormap: ...
106+
def with_alpha(self, alpha: float) -> Colormap: ...
106107
def is_gray(self) -> bool: ...
107108
def resampled(self, lutsize: int) -> Colormap: ...
108109
def reversed(self, name: str | None = ...) -> Colormap: ...

lib/matplotlib/tests/test_colors.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import matplotlib.scale as mscale
2121
from matplotlib.rcsetup import cycler
2222
from matplotlib.testing.decorators import image_comparison, check_figures_equal
23-
from matplotlib.colors import is_color_like, to_rgba_array
23+
from matplotlib.colors import is_color_like, to_rgba_array, ListedColormap
2424

2525

2626
@pytest.mark.parametrize('N, result', [
@@ -221,6 +221,17 @@ def test_colormap_return_types():
221221
assert cmap(x2d).shape == x2d.shape + (4,)
222222

223223

224+
def test_colormap_with_alpha():
225+
cmap = ListedColormap(["red", "green", ("blue", 0.8)])
226+
cmap2 = cmap.with_alpha(0.5)
227+
# color is the same:
228+
vals = [0, 0.5, 1] # numeric positions that map to the listed colors
229+
assert_array_equal(cmap(vals)[:, :2], cmap2(vals)[:, :2])
230+
# alpha of cmap2 is changed:
231+
assert_array_equal(cmap(vals)[:, 3], [1, 1, 0.8])
232+
assert_array_equal(cmap2(vals)[:, 3], [0.5, 0.5, 0.5])
233+
234+
224235
def test_BoundaryNorm():
225236
"""
226237
GitHub issue #1258: interpolation was failing with numpy

0 commit comments

Comments
 (0)