test_rcmod.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. import numpy as np
  2. import matplotlib as mpl
  3. import nose
  4. import matplotlib.pyplot as plt
  5. import nose.tools as nt
  6. import numpy.testing as npt
  7. from .. import rcmod, palettes, utils
  8. class RCParamTester(object):
  9. def flatten_list(self, orig_list):
  10. iter_list = map(np.atleast_1d, orig_list)
  11. flat_list = [item for sublist in iter_list for item in sublist]
  12. return flat_list
  13. def assert_rc_params(self, params):
  14. for k, v in params.items():
  15. if isinstance(v, np.ndarray):
  16. npt.assert_array_equal(mpl.rcParams[k], v)
  17. else:
  18. nt.assert_equal((k, mpl.rcParams[k]), (k, v))
  19. class TestAxesStyle(RCParamTester):
  20. styles = ["white", "dark", "whitegrid", "darkgrid", "ticks"]
  21. def test_default_return(self):
  22. current = rcmod.axes_style()
  23. self.assert_rc_params(current)
  24. def test_key_usage(self):
  25. _style_keys = set(rcmod._style_keys)
  26. for style in self.styles:
  27. nt.assert_true(not set(rcmod.axes_style(style)) ^ _style_keys)
  28. def test_bad_style(self):
  29. with nt.assert_raises(ValueError):
  30. rcmod.axes_style("i_am_not_a_style")
  31. def test_rc_override(self):
  32. rc = {"axes.facecolor": "blue", "foo.notaparam": "bar"}
  33. out = rcmod.axes_style("darkgrid", rc)
  34. nt.assert_equal(out["axes.facecolor"], "blue")
  35. nt.assert_not_in("foo.notaparam", out)
  36. def test_set_style(self):
  37. for style in self.styles:
  38. style_dict = rcmod.axes_style(style)
  39. rcmod.set_style(style)
  40. self.assert_rc_params(style_dict)
  41. def test_style_context_manager(self):
  42. rcmod.set_style("darkgrid")
  43. orig_params = rcmod.axes_style()
  44. context_params = rcmod.axes_style("whitegrid")
  45. with rcmod.axes_style("whitegrid"):
  46. self.assert_rc_params(context_params)
  47. self.assert_rc_params(orig_params)
  48. @rcmod.axes_style("whitegrid")
  49. def func():
  50. self.assert_rc_params(context_params)
  51. func()
  52. self.assert_rc_params(orig_params)
  53. def test_style_context_independence(self):
  54. nt.assert_true(set(rcmod._style_keys) ^ set(rcmod._context_keys))
  55. def test_set_rc(self):
  56. rcmod.set(rc={"lines.linewidth": 4})
  57. nt.assert_equal(mpl.rcParams["lines.linewidth"], 4)
  58. rcmod.set()
  59. def test_set_with_palette(self):
  60. rcmod.reset_orig()
  61. rcmod.set(palette="deep")
  62. assert utils.get_color_cycle() == palettes.color_palette("deep", 10)
  63. rcmod.reset_orig()
  64. rcmod.set(palette="deep", color_codes=False)
  65. assert utils.get_color_cycle() == palettes.color_palette("deep", 10)
  66. rcmod.reset_orig()
  67. pal = palettes.color_palette("deep")
  68. rcmod.set(palette=pal)
  69. assert utils.get_color_cycle() == palettes.color_palette("deep", 10)
  70. rcmod.reset_orig()
  71. rcmod.set(palette=pal, color_codes=False)
  72. assert utils.get_color_cycle() == palettes.color_palette("deep", 10)
  73. rcmod.reset_orig()
  74. rcmod.set()
  75. def test_reset_defaults(self):
  76. rcmod.reset_defaults()
  77. self.assert_rc_params(mpl.rcParamsDefault)
  78. rcmod.set()
  79. def test_reset_orig(self):
  80. rcmod.reset_orig()
  81. self.assert_rc_params(mpl.rcParamsOrig)
  82. rcmod.set()
  83. class TestPlottingContext(RCParamTester):
  84. contexts = ["paper", "notebook", "talk", "poster"]
  85. def test_default_return(self):
  86. current = rcmod.plotting_context()
  87. self.assert_rc_params(current)
  88. def test_key_usage(self):
  89. _context_keys = set(rcmod._context_keys)
  90. for context in self.contexts:
  91. missing = set(rcmod.plotting_context(context)) ^ _context_keys
  92. nt.assert_true(not missing)
  93. def test_bad_context(self):
  94. with nt.assert_raises(ValueError):
  95. rcmod.plotting_context("i_am_not_a_context")
  96. def test_font_scale(self):
  97. notebook_ref = rcmod.plotting_context("notebook")
  98. notebook_big = rcmod.plotting_context("notebook", 2)
  99. font_keys = ["axes.labelsize", "axes.titlesize", "legend.fontsize",
  100. "xtick.labelsize", "ytick.labelsize", "font.size"]
  101. for k in font_keys:
  102. nt.assert_equal(notebook_ref[k] * 2, notebook_big[k])
  103. def test_rc_override(self):
  104. key, val = "grid.linewidth", 5
  105. rc = {key: val, "foo": "bar"}
  106. out = rcmod.plotting_context("talk", rc=rc)
  107. nt.assert_equal(out[key], val)
  108. nt.assert_not_in("foo", out)
  109. def test_set_context(self):
  110. for context in self.contexts:
  111. context_dict = rcmod.plotting_context(context)
  112. rcmod.set_context(context)
  113. self.assert_rc_params(context_dict)
  114. def test_context_context_manager(self):
  115. rcmod.set_context("notebook")
  116. orig_params = rcmod.plotting_context()
  117. context_params = rcmod.plotting_context("paper")
  118. with rcmod.plotting_context("paper"):
  119. self.assert_rc_params(context_params)
  120. self.assert_rc_params(orig_params)
  121. @rcmod.plotting_context("paper")
  122. def func():
  123. self.assert_rc_params(context_params)
  124. func()
  125. self.assert_rc_params(orig_params)
  126. class TestPalette(object):
  127. def test_set_palette(self):
  128. rcmod.set_palette("deep")
  129. assert utils.get_color_cycle() == palettes.color_palette("deep", 10)
  130. rcmod.set_palette("pastel6")
  131. assert utils.get_color_cycle() == palettes.color_palette("pastel6", 6)
  132. rcmod.set_palette("dark", 4)
  133. assert utils.get_color_cycle() == palettes.color_palette("dark", 4)
  134. rcmod.set_palette("Set2", color_codes=True)
  135. assert utils.get_color_cycle() == palettes.color_palette("Set2", 8)
  136. class TestFonts(object):
  137. def test_set_font(self):
  138. rcmod.set(font="Verdana")
  139. _, ax = plt.subplots()
  140. ax.set_xlabel("foo")
  141. try:
  142. nt.assert_equal(ax.xaxis.label.get_fontname(),
  143. "Verdana")
  144. except AssertionError:
  145. if has_verdana():
  146. raise
  147. else:
  148. raise nose.SkipTest("Verdana font is not present")
  149. finally:
  150. rcmod.set()
  151. def test_set_serif_font(self):
  152. rcmod.set(font="serif")
  153. _, ax = plt.subplots()
  154. ax.set_xlabel("foo")
  155. nt.assert_in(ax.xaxis.label.get_fontname(),
  156. mpl.rcParams["font.serif"])
  157. rcmod.set()
  158. def test_different_sans_serif(self):
  159. rcmod.set()
  160. rcmod.set_style(rc={"font.sans-serif": ["Verdana"]})
  161. _, ax = plt.subplots()
  162. ax.set_xlabel("foo")
  163. try:
  164. nt.assert_equal(ax.xaxis.label.get_fontname(),
  165. "Verdana")
  166. except AssertionError:
  167. if has_verdana():
  168. raise
  169. else:
  170. raise nose.SkipTest("Verdana font is not present")
  171. finally:
  172. rcmod.set()
  173. def has_verdana():
  174. """Helper to verify if Verdana font is present"""
  175. # This import is relatively lengthy, so to prevent its import for
  176. # testing other tests in this module not requiring this knowledge,
  177. # import font_manager here
  178. import matplotlib.font_manager as mplfm
  179. try:
  180. verdana_font = mplfm.findfont('Verdana', fallback_to_default=False)
  181. except: # noqa
  182. # if https://github.com/matplotlib/matplotlib/pull/3435
  183. # gets accepted
  184. return False
  185. # otherwise check if not matching the logic for a 'default' one
  186. try:
  187. unlikely_font = mplfm.findfont("very_unlikely_to_exist1234",
  188. fallback_to_default=False)
  189. except: # noqa
  190. # if matched verdana but not unlikely, Verdana must exist
  191. return True
  192. # otherwise -- if they match, must be the same default
  193. return verdana_font != unlikely_font