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