2016-10-03 22:45:14 +09:00
|
|
|
import pytest
|
|
|
|
|
2016-10-03 23:55:39 +09:00
|
|
|
# make sure to skip these tests entirely if numpy/matplotlib are not present
|
2016-10-03 22:45:14 +09:00
|
|
|
np = pytest.importorskip("numpy")
|
|
|
|
matplotlib = pytest.importorskip("matplotlib")
|
|
|
|
plt = pytest.importorskip("matplotlib.pyplot")
|
|
|
|
|
2016-10-03 15:59:47 +09:00
|
|
|
from xontrib import mplhooks
|
|
|
|
|
2018-08-30 09:18:49 -05:00
|
|
|
skip_if_mpl2 = pytest.mark.skipif(
|
|
|
|
matplotlib.__version__.startswith("2"), reason="Bug in matplotlib v2"
|
|
|
|
)
|
2016-10-03 15:59:47 +09:00
|
|
|
|
|
|
|
# some default settings that are temporarily changed by mpl
|
2018-08-30 09:18:49 -05:00
|
|
|
FONT_SIZE = 22
|
|
|
|
FACE_COLOR = (0.0, 1.0, 0.0, 1.0)
|
|
|
|
DPI = 80
|
2016-10-03 15:59:47 +09:00
|
|
|
|
|
|
|
|
|
|
|
def create_figure():
|
|
|
|
"""Simply create a figure with the default settings"""
|
|
|
|
f, ax = plt.subplots()
|
|
|
|
ax.plot(np.arange(20), np.arange(20))
|
|
|
|
# set the figure parameters such that mpl will require changes
|
|
|
|
f.set_facecolor(FACE_COLOR)
|
2016-10-09 23:13:54 +08:00
|
|
|
f.dpi = DPI
|
2018-08-30 09:18:49 -05:00
|
|
|
matplotlib.rcParams.update({"font.size": FONT_SIZE})
|
2016-10-03 15:59:47 +09:00
|
|
|
return f
|
|
|
|
|
|
|
|
|
2017-01-26 17:26:28 -05:00
|
|
|
@skip_if_mpl2
|
2016-10-03 15:59:47 +09:00
|
|
|
def test_mpl_preserve_font_size():
|
|
|
|
"""Make sure that matplotlib preserves font size settings"""
|
|
|
|
f = create_figure()
|
|
|
|
width, height = f.canvas.get_width_height()
|
2017-01-26 17:26:28 -05:00
|
|
|
print(width, height)
|
2018-08-30 09:18:49 -05:00
|
|
|
s = mplhooks.figure_to_tight_array(f, 0.5 * width, 0.5 * height, True)
|
2016-10-03 15:59:47 +09:00
|
|
|
exp = FONT_SIZE
|
2018-08-30 09:18:49 -05:00
|
|
|
obs = matplotlib.rcParams["font.size"]
|
2016-10-03 15:59:47 +09:00
|
|
|
plt.close(f)
|
|
|
|
assert exp == obs
|
|
|
|
|
|
|
|
|
2017-01-26 17:26:28 -05:00
|
|
|
@skip_if_mpl2
|
2016-10-03 15:59:47 +09:00
|
|
|
def test_mpl_preserve_face_color():
|
|
|
|
"""Make sure that the figure preserves face color settings"""
|
|
|
|
f = create_figure()
|
|
|
|
width, height = f.canvas.get_width_height()
|
2018-08-30 09:18:49 -05:00
|
|
|
s = mplhooks.figure_to_tight_array(f, 0.5 * width, 0.5 * height, True)
|
2016-10-03 15:59:47 +09:00
|
|
|
exp = FACE_COLOR
|
|
|
|
obs = f.get_facecolor()
|
|
|
|
plt.close(f)
|
|
|
|
assert exp == obs
|
|
|
|
|
|
|
|
|
2017-01-26 17:26:28 -05:00
|
|
|
@skip_if_mpl2
|
2016-10-03 15:59:47 +09:00
|
|
|
def test_mpl_preserve_width():
|
|
|
|
"""Make sure that the figure preserves width settings"""
|
|
|
|
f = create_figure()
|
|
|
|
width, height = f.canvas.get_width_height()
|
2018-08-30 09:18:49 -05:00
|
|
|
s = mplhooks.figure_to_tight_array(f, 0.5 * width, 0.5 * height, True)
|
2016-10-03 15:59:47 +09:00
|
|
|
exp = width
|
|
|
|
newwidth, newheight = f.canvas.get_width_height()
|
|
|
|
obs = newwidth
|
|
|
|
plt.close(f)
|
|
|
|
assert exp == obs
|
|
|
|
|
|
|
|
|
2017-01-26 17:26:28 -05:00
|
|
|
@skip_if_mpl2
|
2016-10-03 15:59:47 +09:00
|
|
|
def test_mpl_preserve_height():
|
|
|
|
"""Make sure that the figure preserves height settings"""
|
|
|
|
f = create_figure()
|
|
|
|
width, height = f.canvas.get_width_height()
|
2018-08-30 09:18:49 -05:00
|
|
|
s = mplhooks.figure_to_tight_array(f, 0.5 * width, 0.5 * height, True)
|
2016-10-03 15:59:47 +09:00
|
|
|
exp = height
|
|
|
|
newwidth, newheight = f.canvas.get_width_height()
|
|
|
|
obs = newheight
|
|
|
|
plt.close(f)
|
|
|
|
assert exp == obs
|
2016-10-09 23:13:54 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_mpl_preserve_dpi():
|
|
|
|
"""Make sure that the figure preserves height settings"""
|
|
|
|
f = create_figure()
|
|
|
|
width, height = f.canvas.get_width_height()
|
2018-08-30 09:18:49 -05:00
|
|
|
s = mplhooks.figure_to_tight_array(f, 0.5 * width, 0.5 * height, False)
|
2016-10-09 23:13:54 +08:00
|
|
|
exp = DPI
|
|
|
|
obs = f.dpi
|
|
|
|
plt.close(f)
|
|
|
|
assert exp == obs
|
|
|
|
|
|
|
|
|
2017-01-26 17:26:28 -05:00
|
|
|
@skip_if_mpl2
|
2016-10-09 23:13:54 +08:00
|
|
|
def test_mpl_preserve_image_tight():
|
|
|
|
"""Make sure that the figure preserves height settings"""
|
|
|
|
f = create_figure()
|
|
|
|
exp = mplhooks.figure_to_rgb_array(f)
|
|
|
|
width, height = f.canvas.get_width_height()
|
2018-08-30 09:18:49 -05:00
|
|
|
s = mplhooks.figure_to_tight_array(f, 0.5 * width, 0.5 * height, True)
|
2016-10-09 23:13:54 +08:00
|
|
|
obs = mplhooks.figure_to_rgb_array(f)
|
|
|
|
plt.close(f)
|
|
|
|
assert np.all(exp == obs)
|
|
|
|
|
|
|
|
|
|
|
|
def test_mpl_preserve_standard():
|
|
|
|
"""Make sure that the figure preserves height settings"""
|
|
|
|
f = create_figure()
|
|
|
|
exp = mplhooks.figure_to_rgb_array(f)
|
|
|
|
width, height = f.canvas.get_width_height()
|
2018-08-30 09:18:49 -05:00
|
|
|
s = mplhooks.figure_to_tight_array(f, 0.5 * width, 0.5 * height, False)
|
2016-10-09 23:13:54 +08:00
|
|
|
obs = mplhooks.figure_to_rgb_array(f)
|
|
|
|
plt.close(f)
|
|
|
|
assert np.all(exp == obs)
|