Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c676f45

Browse files
committed
TST: fix tests
1 parent 5cc5541 commit c676f45

File tree

2 files changed

+197
-202
lines changed

2 files changed

+197
-202
lines changed

lib/matplotlib/tests/test_axes.py

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
import matplotlib.markers as mmarkers
2424
import matplotlib.patches as mpatches
2525
import matplotlib.colors as mcolors
26+
import matplotlib.transforms as mtransforms
2627
from numpy.testing import (
2728
assert_allclose, assert_array_equal, assert_array_almost_equal)
29+
from matplotlib import rc_context
2830
from matplotlib.cbook import (
2931
IgnoredKeywordWarning, MatplotlibDeprecationWarning)
3032

@@ -6063,3 +6065,198 @@ def invert(x):
60636065
fig.canvas.draw()
60646066
fig.set_size_inches((7, 4))
60656067
assert_allclose(ax.get_position().extents, [0.125, 0.1, 0.9, 0.9])
6068+
6069+
6070+
def color_boxes(fig, axs):
6071+
"""
6072+
Helper for the tests below that test the extents of various axes elements
6073+
"""
6074+
fig.canvas.draw()
6075+
6076+
renderer = fig.canvas.get_renderer()
6077+
bbaxis = []
6078+
for nn, axx in enumerate([axs.xaxis, axs.yaxis]):
6079+
bb = axx.get_tightbbox(renderer)
6080+
if bb:
6081+
axisr = plt.Rectangle((bb.x0, bb.y0), width=bb.width,
6082+
height=bb.height, linewidth=0.7, edgecolor='y',
6083+
facecolor="none", transform=None, zorder=3)
6084+
fig.add_artist(axisr)
6085+
bbaxis += [bb]
6086+
6087+
bbspines = []
6088+
for nn, a in enumerate(['bottom', 'top', 'left', 'right']):
6089+
bb = axs.spines[a].get_window_extent(renderer)
6090+
spiner = plt.Rectangle((bb.x0, bb.y0), width=bb.width,
6091+
height=bb.height, linewidth=0.7,
6092+
edgecolor="green", facecolor="none",
6093+
transform=None, zorder=3)
6094+
fig.add_artist(spiner)
6095+
bbspines += [bb]
6096+
6097+
bb = axs.get_window_extent()
6098+
rect2 = plt.Rectangle((bb.x0, bb.y0), width=bb.width, height=bb.height,
6099+
linewidth=1.5, edgecolor="magenta",
6100+
facecolor="none", transform=None, zorder=2)
6101+
fig.add_artist(rect2)
6102+
bbax = bb
6103+
6104+
bb2 = axs.get_tightbbox(renderer)
6105+
rect2 = plt.Rectangle((bb2.x0, bb2.y0), width=bb2.width,
6106+
height=bb2.height, linewidth=3, edgecolor="red",
6107+
facecolor="none", transform=None, zorder=1)
6108+
fig.add_artist(rect2)
6109+
bbtb = bb2
6110+
return bbaxis, bbspines, bbax, bbtb
6111+
6112+
6113+
def test_normal_axes():
6114+
with rc_context({'_internal.classic_mode': False}):
6115+
fig, ax = plt.subplots(dpi=200, figsize=(6, 6))
6116+
fig.canvas.draw()
6117+
plt.close(fig)
6118+
bbaxis, bbspines, bbax, bbtb = color_boxes(fig, ax)
6119+
6120+
# test the axis bboxes
6121+
target = [
6122+
[123.375, 75.88888888888886, 983.25, 33.0],
6123+
[85.51388888888889, 99.99999999999997, 53.375, 993.0]
6124+
]
6125+
for nn, b in enumerate(bbaxis):
6126+
targetbb = mtransforms.Bbox.from_bounds(*target[nn])
6127+
assert_array_almost_equal(b.bounds, targetbb.bounds, decimal=2)
6128+
6129+
target = [
6130+
[150.0, 119.999, 930.0, 11.111],
6131+
[150.0, 1080.0, 930.0, 0.0],
6132+
[150.0, 119.9999, 11.111, 960.0],
6133+
[1068.8888, 119.9999, 11.111, 960.0]
6134+
]
6135+
for nn, b in enumerate(bbspines):
6136+
targetbb = mtransforms.Bbox.from_bounds(*target[nn])
6137+
assert_array_almost_equal(b.bounds, targetbb.bounds, decimal=2)
6138+
6139+
target = [150.0, 119.99999999999997, 930.0, 960.0]
6140+
targetbb = mtransforms.Bbox.from_bounds(*target)
6141+
assert_array_almost_equal(bbax.bounds, targetbb.bounds, decimal=2)
6142+
6143+
target = [85.5138, 75.88888, 1021.11, 1017.11]
6144+
targetbb = mtransforms.Bbox.from_bounds(*target)
6145+
assert_array_almost_equal(bbtb.bounds, targetbb.bounds, decimal=2)
6146+
6147+
# test that get_position roundtrips to get_window_extent
6148+
axbb = ax.get_position().transformed(fig.transFigure).bounds
6149+
assert_array_almost_equal(axbb, ax.get_window_extent().bounds, decimal=2)
6150+
6151+
6152+
def test_nodecorator():
6153+
with rc_context({'_internal.classic_mode': False}):
6154+
fig, ax = plt.subplots(dpi=200, figsize=(6, 6))
6155+
fig.canvas.draw()
6156+
ax.set(xticklabels=[], yticklabels=[])
6157+
bbaxis, bbspines, bbax, bbtb = color_boxes(fig, ax)
6158+
6159+
# test the axis bboxes
6160+
target = [
6161+
None,
6162+
None
6163+
]
6164+
for nn, b in enumerate(bbaxis):
6165+
assert b is None
6166+
6167+
target = [
6168+
[150.0, 119.999, 930.0, 11.111],
6169+
[150.0, 1080.0, 930.0, 0.0],
6170+
[150.0, 119.9999, 11.111, 960.0],
6171+
[1068.8888, 119.9999, 11.111, 960.0]
6172+
]
6173+
for nn, b in enumerate(bbspines):
6174+
targetbb = mtransforms.Bbox.from_bounds(*target[nn])
6175+
assert_allclose(b.bounds, targetbb.bounds, atol=1e-2)
6176+
6177+
target = [150.0, 119.99999999999997, 930.0, 960.0]
6178+
targetbb = mtransforms.Bbox.from_bounds(*target)
6179+
assert_allclose(bbax.bounds, targetbb.bounds, atol=1e-2)
6180+
6181+
target = [150., 120., 930., 960.]
6182+
targetbb = mtransforms.Bbox.from_bounds(*target)
6183+
assert_allclose(bbtb.bounds, targetbb.bounds, atol=1e-2)
6184+
6185+
6186+
def test_displaced_spine():
6187+
with rc_context({'_internal.classic_mode': False}):
6188+
fig, ax = plt.subplots(dpi=200, figsize=(6, 6))
6189+
ax.set(xticklabels=[], yticklabels=[])
6190+
ax.spines['bottom'].set_position(('axes', -0.1))
6191+
fig.canvas.draw()
6192+
bbaxis, bbspines, bbax, bbtb = color_boxes(fig, ax)
6193+
6194+
target = [
6195+
[150., 24., 930., 11.111111],
6196+
[150.0, 1080.0, 930.0, 0.0],
6197+
[150.0, 119.9999, 11.111, 960.0],
6198+
[1068.8888, 119.9999, 11.111, 960.0]
6199+
]
6200+
for nn, b in enumerate(bbspines):
6201+
targetbb = mtransforms.Bbox.from_bounds(*target[nn])
6202+
6203+
target = [150.0, 119.99999999999997, 930.0, 960.0]
6204+
targetbb = mtransforms.Bbox.from_bounds(*target)
6205+
assert_allclose(bbax.bounds, targetbb.bounds, atol=1e-2)
6206+
6207+
target = [150., 24., 930., 1056.]
6208+
targetbb = mtransforms.Bbox.from_bounds(*target)
6209+
assert_allclose(bbtb.bounds, targetbb.bounds, atol=1e-2)
6210+
6211+
6212+
def test_tickdirs():
6213+
"""
6214+
Switch the tickdirs and make sure the bboxes switch with them
6215+
"""
6216+
targets = [[[150.0, 120.0, 930.0, 11.1111],
6217+
[150.0, 120.0, 11.111, 960.0]],
6218+
[[150.0, 108.8889, 930.0, 11.111111111111114],
6219+
[138.889, 120, 11.111, 960.0]],
6220+
[[150.0, 114.44444444444441, 930.0, 11.111111111111114],
6221+
[144.44444444444446, 119.999, 11.111, 960.0]]]
6222+
for dnum, dirs in enumerate(['in', 'out', 'inout']):
6223+
with rc_context({'_internal.classic_mode': False}):
6224+
fig, ax = plt.subplots(dpi=200, figsize=(6, 6))
6225+
ax.tick_params(direction=dirs)
6226+
fig.canvas.draw()
6227+
bbaxis, bbspines, bbax, bbtb = color_boxes(fig, ax)
6228+
for nn, num in enumerate([0, 2]):
6229+
targetbb = mtransforms.Bbox.from_bounds(*targets[dnum][nn])
6230+
assert_allclose(bbspines[num].bounds, targetbb.bounds,
6231+
atol=1e-2)
6232+
6233+
6234+
def test_minor_accountedfor():
6235+
with rc_context({'_internal.classic_mode': False}):
6236+
fig, ax = plt.subplots(dpi=200, figsize=(6, 6))
6237+
fig.canvas.draw()
6238+
ax.tick_params(which='both', direction='out')
6239+
6240+
bbaxis, bbspines, bbax, bbtb = color_boxes(fig, ax)
6241+
bbaxis, bbspines, bbax, bbtb = color_boxes(fig, ax)
6242+
targets = [[150.0, 108.88888888888886, 930.0, 11.111111111111114],
6243+
[138.8889, 119.9999, 11.1111, 960.0]]
6244+
for n in range(2):
6245+
targetbb = mtransforms.Bbox.from_bounds(*targets[n])
6246+
assert_allclose(bbspines[n * 2].bounds, targetbb.bounds,
6247+
atol=1e-2)
6248+
6249+
fig, ax = plt.subplots(dpi=200, figsize=(6, 6))
6250+
fig.canvas.draw()
6251+
ax.tick_params(which='both', direction='out')
6252+
ax.minorticks_on()
6253+
ax.tick_params(axis='both', which='minor', length=30)
6254+
fig.canvas.draw()
6255+
bbaxis, bbspines, bbax, bbtb = color_boxes(fig, ax)
6256+
targets = [[150.0, 36.66666666666663, 930.0, 83.33333333333334],
6257+
[66.6667, 120.0, 83.3333, 960.0]]
6258+
6259+
for n in range(2):
6260+
targetbb = mtransforms.Bbox.from_bounds(*targets[n])
6261+
assert_allclose(bbspines[n * 2].bounds, targetbb.bounds,
6262+
atol=1e-2)

0 commit comments

Comments
 (0)