1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
import pytest
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy.testing as npt
from seaborn import rcmod, palettes, utils
def has_verdana():
"""Helper to verify if Verdana font is present"""
# This import is relatively lengthy, so to prevent its import for
# testing other tests in this module not requiring this knowledge,
# import font_manager here
import matplotlib.font_manager as mplfm
try:
verdana_font = mplfm.findfont('Verdana', fallback_to_default=False)
except: # noqa
# if https://github.com/matplotlib/matplotlib/pull/3435
# gets accepted
return False
# otherwise check if not matching the logic for a 'default' one
try:
unlikely_font = mplfm.findfont("very_unlikely_to_exist1234",
fallback_to_default=False)
except: # noqa
# if matched verdana but not unlikely, Verdana must exist
return True
# otherwise -- if they match, must be the same default
return verdana_font != unlikely_font
class RCParamFixtures:
@pytest.fixture(autouse=True)
def reset_params(self):
yield
rcmod.reset_orig()
def flatten_list(self, orig_list):
iter_list = map(np.atleast_1d, orig_list)
flat_list = [item for sublist in iter_list for item in sublist]
return flat_list
def assert_rc_params(self, params):
for k, v in params.items():
# Various subtle issues in matplotlib lead to unexpected
# values for the backend rcParam, which isn't relevant here
if k == "backend":
continue
if isinstance(v, np.ndarray):
npt.assert_array_equal(mpl.rcParams[k], v)
else:
assert mpl.rcParams[k] == v
def assert_rc_params_equal(self, params1, params2):
for key, v1 in params1.items():
# Various subtle issues in matplotlib lead to unexpected
# values for the backend rcParam, which isn't relevant here
if key == "backend":
continue
v2 = params2[key]
if isinstance(v1, np.ndarray):
npt.assert_array_equal(v1, v2)
else:
assert v1 == v2
class TestAxesStyle(RCParamFixtures):
styles = ["white", "dark", "whitegrid", "darkgrid", "ticks"]
def test_default_return(self):
current = rcmod.axes_style()
self.assert_rc_params(current)
def test_key_usage(self):
_style_keys = set(rcmod._style_keys)
for style in self.styles:
assert not set(rcmod.axes_style(style)) ^ _style_keys
def test_bad_style(self):
with pytest.raises(ValueError):
rcmod.axes_style("i_am_not_a_style")
def test_rc_override(self):
rc = {"axes.facecolor": "blue", "foo.notaparam": "bar"}
out = rcmod.axes_style("darkgrid", rc)
assert out["axes.facecolor"] == "blue"
assert "foo.notaparam" not in out
def test_set_style(self):
for style in self.styles:
style_dict = rcmod.axes_style(style)
rcmod.set_style(style)
self.assert_rc_params(style_dict)
def test_style_context_manager(self):
rcmod.set_style("darkgrid")
orig_params = rcmod.axes_style()
context_params = rcmod.axes_style("whitegrid")
with rcmod.axes_style("whitegrid"):
self.assert_rc_params(context_params)
self.assert_rc_params(orig_params)
@rcmod.axes_style("whitegrid")
def func():
self.assert_rc_params(context_params)
func()
self.assert_rc_params(orig_params)
def test_style_context_independence(self):
assert set(rcmod._style_keys) ^ set(rcmod._context_keys)
def test_set_rc(self):
rcmod.set_theme(rc={"lines.linewidth": 4})
assert mpl.rcParams["lines.linewidth"] == 4
rcmod.set_theme()
def test_set_with_palette(self):
rcmod.reset_orig()
rcmod.set_theme(palette="deep")
assert utils.get_color_cycle() == palettes.color_palette("deep", 10)
rcmod.reset_orig()
rcmod.set_theme(palette="deep", color_codes=False)
assert utils.get_color_cycle() == palettes.color_palette("deep", 10)
rcmod.reset_orig()
pal = palettes.color_palette("deep")
rcmod.set_theme(palette=pal)
assert utils.get_color_cycle() == palettes.color_palette("deep", 10)
rcmod.reset_orig()
rcmod.set_theme(palette=pal, color_codes=False)
assert utils.get_color_cycle() == palettes.color_palette("deep", 10)
rcmod.reset_orig()
rcmod.set_theme()
def test_reset_defaults(self):
rcmod.reset_defaults()
self.assert_rc_params(mpl.rcParamsDefault)
rcmod.set_theme()
def test_reset_orig(self):
rcmod.reset_orig()
self.assert_rc_params(mpl.rcParamsOrig)
rcmod.set_theme()
def test_set_is_alias(self):
rcmod.set_theme(context="paper", style="white")
params1 = mpl.rcParams.copy()
rcmod.reset_orig()
rcmod.set_theme(context="paper", style="white")
params2 = mpl.rcParams.copy()
self.assert_rc_params_equal(params1, params2)
rcmod.set_theme()
class TestPlottingContext(RCParamFixtures):
contexts = ["paper", "notebook", "talk", "poster"]
def test_default_return(self):
current = rcmod.plotting_context()
self.assert_rc_params(current)
def test_key_usage(self):
_context_keys = set(rcmod._context_keys)
for context in self.contexts:
missing = set(rcmod.plotting_context(context)) ^ _context_keys
assert not missing
def test_bad_context(self):
with pytest.raises(ValueError):
rcmod.plotting_context("i_am_not_a_context")
def test_font_scale(self):
notebook_ref = rcmod.plotting_context("notebook")
notebook_big = rcmod.plotting_context("notebook", 2)
font_keys = [
"font.size",
"axes.labelsize", "axes.titlesize",
"xtick.labelsize", "ytick.labelsize",
"legend.fontsize", "legend.title_fontsize",
]
for k in font_keys:
assert notebook_ref[k] * 2 == notebook_big[k]
def test_rc_override(self):
key, val = "grid.linewidth", 5
rc = {key: val, "foo": "bar"}
out = rcmod.plotting_context("talk", rc=rc)
assert out[key] == val
assert "foo" not in out
def test_set_context(self):
for context in self.contexts:
context_dict = rcmod.plotting_context(context)
rcmod.set_context(context)
self.assert_rc_params(context_dict)
def test_context_context_manager(self):
rcmod.set_context("notebook")
orig_params = rcmod.plotting_context()
context_params = rcmod.plotting_context("paper")
with rcmod.plotting_context("paper"):
self.assert_rc_params(context_params)
self.assert_rc_params(orig_params)
@rcmod.plotting_context("paper")
def func():
self.assert_rc_params(context_params)
func()
self.assert_rc_params(orig_params)
class TestPalette(RCParamFixtures):
def test_set_palette(self):
rcmod.set_palette("deep")
assert utils.get_color_cycle() == palettes.color_palette("deep", 10)
rcmod.set_palette("pastel6")
assert utils.get_color_cycle() == palettes.color_palette("pastel6", 6)
rcmod.set_palette("dark", 4)
assert utils.get_color_cycle() == palettes.color_palette("dark", 4)
rcmod.set_palette("Set2", color_codes=True)
assert utils.get_color_cycle() == palettes.color_palette("Set2", 8)
assert mpl.colors.same_color(
mpl.rcParams["patch.facecolor"], palettes.color_palette()[0]
)
class TestFonts(RCParamFixtures):
_no_verdana = not has_verdana()
@pytest.mark.skipif(_no_verdana, reason="Verdana font is not present")
def test_set_font(self):
rcmod.set_theme(font="Verdana")
_, ax = plt.subplots()
ax.set_xlabel("foo")
assert ax.xaxis.label.get_fontname() == "Verdana"
rcmod.set_theme()
def test_set_serif_font(self):
rcmod.set_theme(font="serif")
_, ax = plt.subplots()
ax.set_xlabel("foo")
assert ax.xaxis.label.get_fontname() in mpl.rcParams["font.serif"]
rcmod.set_theme()
@pytest.mark.skipif(_no_verdana, reason="Verdana font is not present")
def test_different_sans_serif(self):
rcmod.set_theme()
rcmod.set_style(rc={"font.sans-serif": ["Verdana"]})
_, ax = plt.subplots()
ax.set_xlabel("foo")
assert ax.xaxis.label.get_fontname() == "Verdana"
rcmod.set_theme()
|