Skip to content

Commit 0b48873

Browse files
authored
add tests for from_levels_and_cmap (#44)
* add tests for from_levels_and_cmap * more tests * add seaborn to list of optional dependencies
1 parent 30f3e57 commit 0b48873

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

ci/requirements/py37-min-all-deps.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ dependencies:
77
- cartopy=0.17
88
- matplotlib-base=3.4
99
- numpy=1.17
10+
- seaborn=0.11
1011
- setuptools=40.4
1112
- xarray=0.15
1213
# for testing

docs/installation.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
## Optional dependencies
1111

12+
- [seaborn](https://seaborn.pydata.org/) (0.11 or later)
1213
- [xarray](http://xarray.pydata.org/) (0.15 or later)
1314

1415
## Instructions

mplotutils/tests/test_colormap.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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

Comments
 (0)