Skip to content
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@

- Ensure the current axes (`plt.gca()`) is not changed by calling `mpu.colorbar(...)` ([#136](https://github.com/mathause/mplotutils/pull/136)).

### Internal changes

- Align internal usage of `ListedColormaps` with changes in [matplotlib/matplotlib#29135](https://github.com/matplotlib/matplotlib/pull/29135)
([#145](https://github.com/mathause/mplotutils/pull/145), and [#147](https://github.com/mathause/mplotutils/pull/147)).

## v0.5.0 (27.03.2024)

Version v0.5.0 aligns passing multiple axes to `colorbar` with matplotlib.
Expand Down
5 changes: 4 additions & 1 deletion mplotutils/_colormaps.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import itertools

import matplotlib as mpl
import numpy as np
from matplotlib.colors import from_levels_and_colors
Expand Down Expand Up @@ -54,7 +56,8 @@ def _color_palette(cmap, n_colors):

colors_i = np.linspace(0, 1.0, n_colors)
if isinstance(cmap, (list, tuple)):
# we have a list of colors
# expand or truncate the list of colors to n_colors
cmap = list(itertools.islice(itertools.cycle(cmap), n_colors))
cmap = ListedColormap(cmap)
pal = cmap(colors_i)
elif isinstance(cmap, str):
Expand Down
19 changes: 18 additions & 1 deletion mplotutils/tests/test_colormap.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,25 @@ def test_from_levels_and_cmap_seaborn_cmap():


def test_from_levels_and_cmap_colorstring():
pytest.importorskip("seaborn")

levels = [1, 2, 3]
cmap, norm = mpu.from_levels_and_cmap(levels, "0.1")
assert_cmap_norm(cmap, norm, levels, extend="neither")

np.testing.assert_equal(cmap.colors[0], np.array([0.1, 0.1, 0.1, 1.0]))
np.testing.assert_equal(cmap.colors[1], np.array([0.1, 0.1, 0.1, 1.0]))


def test_from_levels_and_cmap_color_list_explicit():

levels = [1, 2, 3, 4, 5]

# ensure colors are repeated (although that can also be unexpected)
cmap, norm = mpu.from_levels_and_cmap(levels, ["b", "k"])

assert_cmap_norm(cmap, norm, levels, extend="neither")

np.testing.assert_equal(cmap.colors[0], np.array([0.0, 0.0, 1.0, 1.0])) # blue
np.testing.assert_equal(cmap.colors[1], np.array([0.0, 0.0, 0.0, 1.0])) # black
np.testing.assert_equal(cmap.colors[2], np.array([0.0, 0.0, 1.0, 1.0])) # blue
np.testing.assert_equal(cmap.colors[3], np.array([0.0, 0.0, 0.0, 1.0])) # black
Loading