|
| 1 | +import matplotlib |
| 2 | +import matplotlib.pyplot as plt |
| 3 | +import numpy as np |
| 4 | +import pytest |
| 5 | + |
| 6 | +import mplotutils as mpu |
| 7 | + |
| 8 | + |
| 9 | +def test_from_levels_and_cmap_not_list(): |
| 10 | + |
| 11 | + with pytest.raises(ValueError, match="'levels' must be a list of levels"): |
| 12 | + mpu.from_levels_and_cmap(3, "Greys") |
| 13 | + |
| 14 | + |
| 15 | +def assert_cmap_norm(cmap, norm, levels, extend): |
| 16 | + |
| 17 | + zeros = np.zeros(4) |
| 18 | + |
| 19 | + assert isinstance(cmap, matplotlib.colors.ListedColormap) |
| 20 | + |
| 21 | + assert cmap.N == len(levels) - 1 |
| 22 | + assert cmap.colorbar_extend == extend |
| 23 | + np.testing.assert_allclose(cmap.get_bad(), zeros) |
| 24 | + |
| 25 | + if extend in ("both", "min"): |
| 26 | + assert np.not_equal(cmap.get_under(), zeros).all() |
| 27 | + else: |
| 28 | + assert np.equal(cmap.get_under(), zeros).all() |
| 29 | + |
| 30 | + if extend in ("both", "max"): |
| 31 | + assert np.not_equal(cmap.get_over(), zeros).all() |
| 32 | + else: |
| 33 | + assert np.equal(cmap.get_over(), zeros).all() |
| 34 | + |
| 35 | + assert isinstance(norm, matplotlib.colors.BoundaryNorm) |
| 36 | + np.testing.assert_allclose(norm.boundaries, levels) |
| 37 | + |
| 38 | + |
| 39 | +def test_from_levels_and_cmap(): |
| 40 | + |
| 41 | + levels = [1, 2, 3] |
| 42 | + extend = "neither" |
| 43 | + cmap, norm = mpu.from_levels_and_cmap(levels, "viridis", extend=extend) |
| 44 | + assert_cmap_norm(cmap, norm, levels, extend) |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.parametrize("extend", ("neither", "min", "max", "both")) |
| 48 | +def test_from_levels_and_cmap_extend(extend): |
| 49 | + |
| 50 | + levels = [1, 2, 3] |
| 51 | + cmap, norm = mpu.from_levels_and_cmap(levels, "viridis", extend=extend) |
| 52 | + assert_cmap_norm(cmap, norm, levels, extend) |
| 53 | + |
| 54 | + |
| 55 | +def test_from_levels_and_cmap_levels(): |
| 56 | + |
| 57 | + levels = np.arange(-0.35, 0.2, 0.36) |
| 58 | + cmap, norm = mpu.from_levels_and_cmap(levels, "viridis") |
| 59 | + assert_cmap_norm(cmap, norm, levels, extend="neither") |
| 60 | + |
| 61 | + |
| 62 | +def test_from_levels_and_cmap_color_list(): |
| 63 | + |
| 64 | + cmap = plt.get_cmap("viridis").colors |
| 65 | + levels = [1, 2, 3] |
| 66 | + cmap, norm = mpu.from_levels_and_cmap(levels, cmap) |
| 67 | + assert_cmap_norm(cmap, norm, levels, extend="neither") |
| 68 | + |
| 69 | + |
| 70 | +def test_from_levels_and_cmap_LinearSegmentedColormap(): |
| 71 | + |
| 72 | + cmap = plt.cm.RdYlGn |
| 73 | + levels = [1, 2, 3] |
| 74 | + cmap, norm = mpu.from_levels_and_cmap(levels, cmap) |
| 75 | + assert_cmap_norm(cmap, norm, levels, extend="neither") |
| 76 | + |
| 77 | + |
| 78 | +def test_from_levels_and_cmap_seaborn_cmap(): |
| 79 | + |
| 80 | + pytest.importorskip("seaborn") |
| 81 | + |
| 82 | + levels = [1, 2, 3] |
| 83 | + cmap, norm = mpu.from_levels_and_cmap(levels, "tab10") |
| 84 | + assert_cmap_norm(cmap, norm, levels, extend="neither") |
| 85 | + |
| 86 | + |
| 87 | +def test_from_levels_and_cmap_colorstring(): |
| 88 | + |
| 89 | + pytest.importorskip("seaborn") |
| 90 | + |
| 91 | + levels = [1, 2, 3] |
| 92 | + cmap, norm = mpu.from_levels_and_cmap(levels, "0.1") |
| 93 | + assert_cmap_norm(cmap, norm, levels, extend="neither") |
0 commit comments