1import pytest 2 3# make sure to skip these tests entirely if numpy/matplotlib are not present 4np = pytest.importorskip("numpy") 5matplotlib = pytest.importorskip("matplotlib") 6plt = pytest.importorskip("matplotlib.pyplot") 7 8from xontrib import mplhooks 9 10skip_if_mpl2 = pytest.mark.skipif( 11 matplotlib.__version__.startswith("2"), reason="Bug in matplotlib v2" 12) 13 14# some default settings that are temporarily changed by mpl 15FONT_SIZE = 22 16FACE_COLOR = (0.0, 1.0, 0.0, 1.0) 17DPI = 80 18 19 20def create_figure(): 21 """Simply create a figure with the default settings""" 22 f, ax = plt.subplots() 23 ax.plot(np.arange(20), np.arange(20)) 24 # set the figure parameters such that mpl will require changes 25 f.set_facecolor(FACE_COLOR) 26 f.dpi = DPI 27 matplotlib.rcParams.update({"font.size": FONT_SIZE}) 28 return f 29 30 31@skip_if_mpl2 32def test_mpl_preserve_font_size(): 33 """Make sure that matplotlib preserves font size settings""" 34 f = create_figure() 35 width, height = f.canvas.get_width_height() 36 print(width, height) 37 s = mplhooks.figure_to_tight_array(f, 0.5 * width, 0.5 * height, True) 38 exp = FONT_SIZE 39 obs = matplotlib.rcParams["font.size"] 40 plt.close(f) 41 assert exp == obs 42 43 44@skip_if_mpl2 45def test_mpl_preserve_face_color(): 46 """Make sure that the figure preserves face color settings""" 47 f = create_figure() 48 width, height = f.canvas.get_width_height() 49 s = mplhooks.figure_to_tight_array(f, 0.5 * width, 0.5 * height, True) 50 exp = FACE_COLOR 51 obs = f.get_facecolor() 52 plt.close(f) 53 assert exp == obs 54 55 56@skip_if_mpl2 57def test_mpl_preserve_width(): 58 """Make sure that the figure preserves width settings""" 59 f = create_figure() 60 width, height = f.canvas.get_width_height() 61 s = mplhooks.figure_to_tight_array(f, 0.5 * width, 0.5 * height, True) 62 exp = width 63 newwidth, newheight = f.canvas.get_width_height() 64 obs = newwidth 65 plt.close(f) 66 assert exp == obs 67 68 69@skip_if_mpl2 70def test_mpl_preserve_height(): 71 """Make sure that the figure preserves height settings""" 72 f = create_figure() 73 width, height = f.canvas.get_width_height() 74 s = mplhooks.figure_to_tight_array(f, 0.5 * width, 0.5 * height, True) 75 exp = height 76 newwidth, newheight = f.canvas.get_width_height() 77 obs = newheight 78 plt.close(f) 79 assert exp == obs 80 81 82def test_mpl_preserve_dpi(): 83 """Make sure that the figure preserves height settings""" 84 f = create_figure() 85 width, height = f.canvas.get_width_height() 86 s = mplhooks.figure_to_tight_array(f, 0.5 * width, 0.5 * height, False) 87 exp = DPI 88 obs = f.dpi 89 plt.close(f) 90 assert exp == obs 91 92 93@skip_if_mpl2 94def test_mpl_preserve_image_tight(): 95 """Make sure that the figure preserves height settings""" 96 f = create_figure() 97 exp = mplhooks.figure_to_rgb_array(f) 98 width, height = f.canvas.get_width_height() 99 s = mplhooks.figure_to_tight_array(f, 0.5 * width, 0.5 * height, True) 100 obs = mplhooks.figure_to_rgb_array(f) 101 plt.close(f) 102 assert np.all(exp == obs) 103 104 105def test_mpl_preserve_standard(): 106 """Make sure that the figure preserves height settings""" 107 f = create_figure() 108 exp = mplhooks.figure_to_rgb_array(f) 109 width, height = f.canvas.get_width_height() 110 s = mplhooks.figure_to_tight_array(f, 0.5 * width, 0.5 * height, False) 111 obs = mplhooks.figure_to_rgb_array(f) 112 plt.close(f) 113 assert np.all(exp == obs) 114