test_rcparams.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. from collections import OrderedDict
  2. import copy
  3. import os
  4. from pathlib import Path
  5. import subprocess
  6. import sys
  7. from unittest import mock
  8. from cycler import cycler, Cycler
  9. import pytest
  10. import matplotlib as mpl
  11. from matplotlib import cbook
  12. import matplotlib.pyplot as plt
  13. import matplotlib.colors as mcolors
  14. import numpy as np
  15. from matplotlib.rcsetup import (
  16. validate_bool,
  17. validate_bool_maybe_none,
  18. validate_color,
  19. validate_colorlist,
  20. validate_cycler,
  21. validate_float,
  22. validate_fontweight,
  23. validate_hatch,
  24. validate_hist_bins,
  25. validate_int,
  26. validate_markevery,
  27. validate_stringlist,
  28. _validate_linestyle,
  29. _listify_validator)
  30. def test_rcparams(tmpdir):
  31. mpl.rc('text', usetex=False)
  32. mpl.rc('lines', linewidth=22)
  33. usetex = mpl.rcParams['text.usetex']
  34. linewidth = mpl.rcParams['lines.linewidth']
  35. rcpath = Path(tmpdir) / 'test_rcparams.rc'
  36. rcpath.write_text('lines.linewidth: 33')
  37. # test context given dictionary
  38. with mpl.rc_context(rc={'text.usetex': not usetex}):
  39. assert mpl.rcParams['text.usetex'] == (not usetex)
  40. assert mpl.rcParams['text.usetex'] == usetex
  41. # test context given filename (mpl.rc sets linewidth to 33)
  42. with mpl.rc_context(fname=rcpath):
  43. assert mpl.rcParams['lines.linewidth'] == 33
  44. assert mpl.rcParams['lines.linewidth'] == linewidth
  45. # test context given filename and dictionary
  46. with mpl.rc_context(fname=rcpath, rc={'lines.linewidth': 44}):
  47. assert mpl.rcParams['lines.linewidth'] == 44
  48. assert mpl.rcParams['lines.linewidth'] == linewidth
  49. # test context as decorator (and test reusability, by calling func twice)
  50. @mpl.rc_context({'lines.linewidth': 44})
  51. def func():
  52. assert mpl.rcParams['lines.linewidth'] == 44
  53. func()
  54. func()
  55. # test rc_file
  56. mpl.rc_file(rcpath)
  57. assert mpl.rcParams['lines.linewidth'] == 33
  58. def test_RcParams_class():
  59. rc = mpl.RcParams({'font.cursive': ['Apple Chancery',
  60. 'Textile',
  61. 'Zapf Chancery',
  62. 'cursive'],
  63. 'font.family': 'sans-serif',
  64. 'font.weight': 'normal',
  65. 'font.size': 12})
  66. expected_repr = """
  67. RcParams({'font.cursive': ['Apple Chancery',
  68. 'Textile',
  69. 'Zapf Chancery',
  70. 'cursive'],
  71. 'font.family': ['sans-serif'],
  72. 'font.size': 12.0,
  73. 'font.weight': 'normal'})""".lstrip()
  74. assert expected_repr == repr(rc)
  75. expected_str = """
  76. font.cursive: ['Apple Chancery', 'Textile', 'Zapf Chancery', 'cursive']
  77. font.family: ['sans-serif']
  78. font.size: 12.0
  79. font.weight: normal""".lstrip()
  80. assert expected_str == str(rc)
  81. # test the find_all functionality
  82. assert ['font.cursive', 'font.size'] == sorted(rc.find_all('i[vz]'))
  83. assert ['font.family'] == list(rc.find_all('family'))
  84. def test_rcparams_update():
  85. rc = mpl.RcParams({'figure.figsize': (3.5, 42)})
  86. bad_dict = {'figure.figsize': (3.5, 42, 1)}
  87. # make sure validation happens on input
  88. with pytest.raises(ValueError), \
  89. pytest.warns(UserWarning, match="validate"):
  90. rc.update(bad_dict)
  91. def test_rcparams_init():
  92. with pytest.raises(ValueError), \
  93. pytest.warns(UserWarning, match="validate"):
  94. mpl.RcParams({'figure.figsize': (3.5, 42, 1)})
  95. def test_Bug_2543():
  96. # Test that it possible to add all values to itself / deepcopy
  97. # This was not possible because validate_bool_maybe_none did not
  98. # accept None as an argument.
  99. # https://github.com/matplotlib/matplotlib/issues/2543
  100. # We filter warnings at this stage since a number of them are raised
  101. # for deprecated rcparams as they should. We don't want these in the
  102. # printed in the test suite.
  103. with cbook._suppress_matplotlib_deprecation_warning():
  104. with mpl.rc_context():
  105. _copy = mpl.rcParams.copy()
  106. for key in _copy:
  107. mpl.rcParams[key] = _copy[key]
  108. with mpl.rc_context():
  109. copy.deepcopy(mpl.rcParams)
  110. # real test is that this does not raise
  111. assert validate_bool_maybe_none(None) is None
  112. assert validate_bool_maybe_none("none") is None
  113. with pytest.raises(ValueError):
  114. validate_bool_maybe_none("blah")
  115. with pytest.raises(ValueError):
  116. validate_bool(None)
  117. with pytest.raises(ValueError):
  118. with mpl.rc_context():
  119. mpl.rcParams['svg.fonttype'] = True
  120. legend_color_tests = [
  121. ('face', {'color': 'r'}, mcolors.to_rgba('r')),
  122. ('face', {'color': 'inherit', 'axes.facecolor': 'r'},
  123. mcolors.to_rgba('r')),
  124. ('face', {'color': 'g', 'axes.facecolor': 'r'}, mcolors.to_rgba('g')),
  125. ('edge', {'color': 'r'}, mcolors.to_rgba('r')),
  126. ('edge', {'color': 'inherit', 'axes.edgecolor': 'r'},
  127. mcolors.to_rgba('r')),
  128. ('edge', {'color': 'g', 'axes.facecolor': 'r'}, mcolors.to_rgba('g'))
  129. ]
  130. legend_color_test_ids = [
  131. 'same facecolor',
  132. 'inherited facecolor',
  133. 'different facecolor',
  134. 'same edgecolor',
  135. 'inherited edgecolor',
  136. 'different facecolor',
  137. ]
  138. @pytest.mark.parametrize('color_type, param_dict, target', legend_color_tests,
  139. ids=legend_color_test_ids)
  140. def test_legend_colors(color_type, param_dict, target):
  141. param_dict[f'legend.{color_type}color'] = param_dict.pop('color')
  142. get_func = f'get_{color_type}color'
  143. with mpl.rc_context(param_dict):
  144. _, ax = plt.subplots()
  145. ax.plot(range(3), label='test')
  146. leg = ax.legend()
  147. assert getattr(leg.legendPatch, get_func)() == target
  148. def test_mfc_rcparams():
  149. mpl.rcParams['lines.markerfacecolor'] = 'r'
  150. ln = mpl.lines.Line2D([1, 2], [1, 2])
  151. assert ln.get_markerfacecolor() == 'r'
  152. def test_mec_rcparams():
  153. mpl.rcParams['lines.markeredgecolor'] = 'r'
  154. ln = mpl.lines.Line2D([1, 2], [1, 2])
  155. assert ln.get_markeredgecolor() == 'r'
  156. def test_axes_titlecolor_rcparams():
  157. mpl.rcParams['axes.titlecolor'] = 'r'
  158. _, ax = plt.subplots()
  159. title = ax.set_title("Title")
  160. assert title.get_color() == 'r'
  161. def test_Issue_1713(tmpdir):
  162. rcpath = Path(tmpdir) / 'test_rcparams.rc'
  163. rcpath.write_text('timezone: UTC', encoding='UTF-32-BE')
  164. with mock.patch('locale.getpreferredencoding', return_value='UTF-32-BE'):
  165. rc = mpl.rc_params_from_file(rcpath, True, False)
  166. assert rc.get('timezone') == 'UTC'
  167. def generate_validator_testcases(valid):
  168. validation_tests = (
  169. {'validator': validate_bool,
  170. 'success': (*((_, True) for _ in
  171. ('t', 'y', 'yes', 'on', 'true', '1', 1, True)),
  172. *((_, False) for _ in
  173. ('f', 'n', 'no', 'off', 'false', '0', 0, False))),
  174. 'fail': ((_, ValueError)
  175. for _ in ('aardvark', 2, -1, [], ))
  176. },
  177. {'validator': validate_stringlist,
  178. 'success': (('', []),
  179. ('a,b', ['a', 'b']),
  180. ('aardvark', ['aardvark']),
  181. ('aardvark, ', ['aardvark']),
  182. ('aardvark, ,', ['aardvark']),
  183. (['a', 'b'], ['a', 'b']),
  184. (('a', 'b'), ['a', 'b']),
  185. (iter(['a', 'b']), ['a', 'b']),
  186. (np.array(['a', 'b']), ['a', 'b']),
  187. ((1, 2), ['1', '2']),
  188. (np.array([1, 2]), ['1', '2']),
  189. ),
  190. 'fail': ((set(), ValueError),
  191. (1, ValueError),
  192. )
  193. },
  194. {'validator': _listify_validator(validate_int, n=2),
  195. 'success': ((_, [1, 2])
  196. for _ in ('1, 2', [1.5, 2.5], [1, 2],
  197. (1, 2), np.array((1, 2)))),
  198. 'fail': ((_, ValueError)
  199. for _ in ('aardvark', ('a', 1),
  200. (1, 2, 3)
  201. ))
  202. },
  203. {'validator': _listify_validator(validate_float, n=2),
  204. 'success': ((_, [1.5, 2.5])
  205. for _ in ('1.5, 2.5', [1.5, 2.5], [1.5, 2.5],
  206. (1.5, 2.5), np.array((1.5, 2.5)))),
  207. 'fail': ((_, ValueError)
  208. for _ in ('aardvark', ('a', 1),
  209. (1, 2, 3)
  210. ))
  211. },
  212. {'validator': validate_cycler,
  213. 'success': (('cycler("color", "rgb")',
  214. cycler("color", 'rgb')),
  215. (cycler('linestyle', ['-', '--']),
  216. cycler('linestyle', ['-', '--'])),
  217. ("""(cycler("color", ["r", "g", "b"]) +
  218. cycler("mew", [2, 3, 5]))""",
  219. (cycler("color", 'rgb') +
  220. cycler("markeredgewidth", [2, 3, 5]))),
  221. ("cycler(c='rgb', lw=[1, 2, 3])",
  222. cycler('color', 'rgb') + cycler('linewidth', [1, 2, 3])),
  223. ("cycler('c', 'rgb') * cycler('linestyle', ['-', '--'])",
  224. (cycler('color', 'rgb') *
  225. cycler('linestyle', ['-', '--']))),
  226. (cycler('ls', ['-', '--']),
  227. cycler('linestyle', ['-', '--'])),
  228. (cycler(mew=[2, 5]),
  229. cycler('markeredgewidth', [2, 5])),
  230. ),
  231. # This is *so* incredibly important: validate_cycler() eval's
  232. # an arbitrary string! I think I have it locked down enough,
  233. # and that is what this is testing.
  234. # TODO: Note that these tests are actually insufficient, as it may
  235. # be that they raised errors, but still did an action prior to
  236. # raising the exception. We should devise some additional tests
  237. # for that...
  238. 'fail': ((4, ValueError), # Gotta be a string or Cycler object
  239. ('cycler("bleh, [])', ValueError), # syntax error
  240. ('Cycler("linewidth", [1, 2, 3])',
  241. ValueError), # only 'cycler()' function is allowed
  242. ('1 + 2', ValueError), # doesn't produce a Cycler object
  243. ('os.system("echo Gotcha")', ValueError), # os not available
  244. ('import os', ValueError), # should not be able to import
  245. ('def badjuju(a): return a; badjuju(cycler("color", "rgb"))',
  246. ValueError), # Should not be able to define anything
  247. # even if it does return a cycler
  248. ('cycler("waka", [1, 2, 3])', ValueError), # not a property
  249. ('cycler(c=[1, 2, 3])', ValueError), # invalid values
  250. ("cycler(lw=['a', 'b', 'c'])", ValueError), # invalid values
  251. (cycler('waka', [1, 3, 5]), ValueError), # not a property
  252. (cycler('color', ['C1', 'r', 'g']), ValueError) # no CN
  253. )
  254. },
  255. {'validator': validate_hatch,
  256. 'success': (('--|', '--|'), ('\\oO', '\\oO'),
  257. ('/+*/.x', '/+*/.x'), ('', '')),
  258. 'fail': (('--_', ValueError),
  259. (8, ValueError),
  260. ('X', ValueError)),
  261. },
  262. {'validator': validate_colorlist,
  263. 'success': (('r,g,b', ['r', 'g', 'b']),
  264. (['r', 'g', 'b'], ['r', 'g', 'b']),
  265. ('r, ,', ['r']),
  266. (['', 'g', 'blue'], ['g', 'blue']),
  267. ([np.array([1, 0, 0]), np.array([0, 1, 0])],
  268. np.array([[1, 0, 0], [0, 1, 0]])),
  269. (np.array([[1, 0, 0], [0, 1, 0]]),
  270. np.array([[1, 0, 0], [0, 1, 0]])),
  271. ),
  272. 'fail': (('fish', ValueError),
  273. ),
  274. },
  275. {'validator': validate_color,
  276. 'success': (('None', 'none'),
  277. ('none', 'none'),
  278. ('AABBCC', '#AABBCC'), # RGB hex code
  279. ('AABBCC00', '#AABBCC00'), # RGBA hex code
  280. ('tab:blue', 'tab:blue'), # named color
  281. ('C12', 'C12'), # color from cycle
  282. ('(0, 1, 0)', (0.0, 1.0, 0.0)), # RGB tuple
  283. ((0, 1, 0), (0, 1, 0)), # non-string version
  284. ('(0, 1, 0, 1)', (0.0, 1.0, 0.0, 1.0)), # RGBA tuple
  285. ((0, 1, 0, 1), (0, 1, 0, 1)), # non-string version
  286. ),
  287. 'fail': (('tab:veryblue', ValueError), # invalid name
  288. ('(0, 1)', ValueError), # tuple with length < 3
  289. ('(0, 1, 0, 1, 0)', ValueError), # tuple with length > 4
  290. ('(0, 1, none)', ValueError), # cannot cast none to float
  291. ('(0, 1, "0.5")', ValueError), # last one not a float
  292. ),
  293. },
  294. {'validator': validate_hist_bins,
  295. 'success': (('auto', 'auto'),
  296. ('fd', 'fd'),
  297. ('10', 10),
  298. ('1, 2, 3', [1, 2, 3]),
  299. ([1, 2, 3], [1, 2, 3]),
  300. (np.arange(15), np.arange(15))
  301. ),
  302. 'fail': (('aardvark', ValueError),
  303. )
  304. },
  305. {'validator': validate_markevery,
  306. 'success': ((None, None),
  307. (1, 1),
  308. (0.1, 0.1),
  309. ((1, 1), (1, 1)),
  310. ((0.1, 0.1), (0.1, 0.1)),
  311. ([1, 2, 3], [1, 2, 3]),
  312. (slice(2), slice(None, 2, None)),
  313. (slice(1, 2, 3), slice(1, 2, 3))
  314. ),
  315. 'fail': (((1, 2, 3), TypeError),
  316. ([1, 2, 0.3], TypeError),
  317. (['a', 2, 3], TypeError),
  318. ([1, 2, 'a'], TypeError),
  319. ((0.1, 0.2, 0.3), TypeError),
  320. ((0.1, 2, 3), TypeError),
  321. ((1, 0.2, 0.3), TypeError),
  322. ((1, 0.1), TypeError),
  323. ((0.1, 1), TypeError),
  324. (('abc'), TypeError),
  325. ((1, 'a'), TypeError),
  326. ((0.1, 'b'), TypeError),
  327. (('a', 1), TypeError),
  328. (('a', 0.1), TypeError),
  329. ('abc', TypeError),
  330. ('a', TypeError),
  331. (object(), TypeError)
  332. )
  333. },
  334. {'validator': _validate_linestyle,
  335. 'success': (('-', '-'), ('solid', 'solid'),
  336. ('--', '--'), ('dashed', 'dashed'),
  337. ('-.', '-.'), ('dashdot', 'dashdot'),
  338. (':', ':'), ('dotted', 'dotted'),
  339. ('', ''), (' ', ' '),
  340. ('None', 'none'), ('none', 'none'),
  341. ('DoTtEd', 'dotted'), # case-insensitive
  342. ('1, 3', (0, (1, 3))),
  343. ([1.23, 456], (0, [1.23, 456.0])),
  344. ([1, 2, 3, 4], (0, [1.0, 2.0, 3.0, 4.0])),
  345. ((0, [1, 2]), (0, [1, 2])),
  346. ((-1, [1, 2]), (-1, [1, 2])),
  347. ),
  348. 'fail': (('aardvark', ValueError), # not a valid string
  349. (b'dotted', ValueError),
  350. ('dotted'.encode('utf-16'), ValueError),
  351. ([1, 2, 3], ValueError), # sequence with odd length
  352. (1.23, ValueError), # not a sequence
  353. (("a", [1, 2]), ValueError), # wrong explicit offset
  354. ((1, [1, 2, 3]), ValueError), # odd length sequence
  355. (([1, 2], 1), ValueError), # inverted offset/onoff
  356. )
  357. },
  358. )
  359. for validator_dict in validation_tests:
  360. validator = validator_dict['validator']
  361. if valid:
  362. for arg, target in validator_dict['success']:
  363. yield validator, arg, target
  364. else:
  365. for arg, error_type in validator_dict['fail']:
  366. yield validator, arg, error_type
  367. @pytest.mark.parametrize('validator, arg, target',
  368. generate_validator_testcases(True))
  369. def test_validator_valid(validator, arg, target):
  370. res = validator(arg)
  371. if isinstance(target, np.ndarray):
  372. np.testing.assert_equal(res, target)
  373. elif not isinstance(target, Cycler):
  374. assert res == target
  375. else:
  376. # Cyclers can't simply be asserted equal. They don't implement __eq__
  377. assert list(res) == list(target)
  378. @pytest.mark.parametrize('validator, arg, exception_type',
  379. generate_validator_testcases(False))
  380. def test_validator_invalid(validator, arg, exception_type):
  381. with pytest.raises(exception_type):
  382. validator(arg)
  383. @pytest.mark.parametrize('weight, parsed_weight', [
  384. ('bold', 'bold'),
  385. ('BOLD', ValueError), # weight is case-sensitive
  386. (100, 100),
  387. ('100', 100),
  388. (np.array(100), 100),
  389. # fractional fontweights are not defined. This should actually raise a
  390. # ValueError, but historically did not.
  391. (20.6, 20),
  392. ('20.6', ValueError),
  393. ([100], ValueError),
  394. ])
  395. def test_validate_fontweight(weight, parsed_weight):
  396. if parsed_weight is ValueError:
  397. with pytest.raises(ValueError):
  398. validate_fontweight(weight)
  399. else:
  400. assert validate_fontweight(weight) == parsed_weight
  401. def test_keymaps():
  402. key_list = [k for k in mpl.rcParams if 'keymap' in k]
  403. for k in key_list:
  404. assert isinstance(mpl.rcParams[k], list)
  405. def test_rcparams_reset_after_fail():
  406. # There was previously a bug that meant that if rc_context failed and
  407. # raised an exception due to issues in the supplied rc parameters, the
  408. # global rc parameters were left in a modified state.
  409. with mpl.rc_context(rc={'text.usetex': False}):
  410. assert mpl.rcParams['text.usetex'] is False
  411. with pytest.raises(KeyError):
  412. with mpl.rc_context(rc=OrderedDict([('text.usetex', True),
  413. ('test.blah', True)])):
  414. pass
  415. assert mpl.rcParams['text.usetex'] is False
  416. @pytest.mark.skipif(sys.platform != "linux", reason="Linux only")
  417. def test_backend_fallback_headless(tmpdir):
  418. env = {**os.environ,
  419. "DISPLAY": "", "MPLBACKEND": "", "MPLCONFIGDIR": str(tmpdir)}
  420. with pytest.raises(subprocess.CalledProcessError):
  421. subprocess.run(
  422. [sys.executable, "-c",
  423. ("import matplotlib;" +
  424. "matplotlib.use('tkagg');" +
  425. "import matplotlib.pyplot")
  426. ],
  427. env=env, check=True)
  428. @pytest.mark.skipif(sys.platform == "linux" and not os.environ.get("DISPLAY"),
  429. reason="headless")
  430. def test_backend_fallback_headful(tmpdir):
  431. pytest.importorskip("tkinter")
  432. env = {**os.environ, "MPLBACKEND": "", "MPLCONFIGDIR": str(tmpdir)}
  433. backend = subprocess.check_output(
  434. [sys.executable, "-c",
  435. "import matplotlib.pyplot; print(matplotlib.get_backend())"],
  436. env=env, universal_newlines=True)
  437. # The actual backend will depend on what's installed, but at least tkagg is
  438. # present.
  439. assert backend.strip().lower() != "agg"