|
| 1 | +import numpy as np |
| 2 | +from numpy.testing import (assert_allclose, assert_almost_equal, |
| 3 | + assert_array_equal, assert_array_almost_equal) |
| 4 | + |
| 5 | +from matplotlib import rc_context |
| 6 | +import matplotlib.pyplot as plt |
| 7 | +import matplotlib.transforms as mtransforms |
| 8 | + |
| 9 | + |
| 10 | +def color_boxes(fig, axs): |
| 11 | + fig.canvas.draw() |
| 12 | + |
| 13 | + renderer = fig.canvas.get_renderer() |
| 14 | + bbaxis = [] |
| 15 | + for nn, axx in enumerate([axs.xaxis, axs.yaxis]): |
| 16 | + bb = axx.get_tightbbox(renderer) |
| 17 | + if bb: |
| 18 | + axisr = plt.Rectangle((bb.x0, bb.y0), width=bb.width, |
| 19 | + height=bb.height, linewidth=0.7, edgecolor='y', |
| 20 | + facecolor="none", transform=None, zorder=3) |
| 21 | + fig.add_artist(axisr) |
| 22 | + bbaxis += [bb] |
| 23 | + |
| 24 | + bbspines = [] |
| 25 | + for nn, a in enumerate(['bottom', 'top', 'left', 'right']): |
| 26 | + bb = axs.spines[a].get_window_extent(renderer) |
| 27 | + if bb: |
| 28 | + spiner = plt.Rectangle((bb.x0, bb.y0), width=bb.width, |
| 29 | + height=bb.height, linewidth=0.7, |
| 30 | + edgecolor="green", facecolor="none", |
| 31 | + transform=None, zorder=3) |
| 32 | + fig.add_artist(spiner) |
| 33 | + bbspines += [bb] |
| 34 | + |
| 35 | + bb = axs.get_window_extent() |
| 36 | + if bb: |
| 37 | + rect2 = plt.Rectangle((bb.x0, bb.y0), width=bb.width, height=bb.height, |
| 38 | + linewidth=1.5, edgecolor="magenta", |
| 39 | + facecolor="none", transform=None, zorder=2) |
| 40 | + fig.add_artist(rect2) |
| 41 | + bbax = bb |
| 42 | + |
| 43 | + bb2 = axs.get_tightbbox(renderer) |
| 44 | + if bb2: |
| 45 | + rect2 = plt.Rectangle((bb2.x0, bb2.y0), width=bb2.width, |
| 46 | + height=bb2.height, linewidth=3, edgecolor="red", |
| 47 | + facecolor="none", transform=None, zorder=1) |
| 48 | + fig.add_artist(rect2) |
| 49 | + bbtb = bb2 |
| 50 | + return bbaxis, bbspines, bbax, bbtb |
| 51 | + |
| 52 | + |
| 53 | +def test_normal_axes(): |
| 54 | + with rc_context({'_internal.classic_mode': False}): |
| 55 | + fig, ax = plt.subplots(dpi=200, figsize=(6, 6)) |
| 56 | + fig.canvas.draw() |
| 57 | + plt.close(fig) |
| 58 | + bbaxis, bbspines, bbax, bbtb = color_boxes(fig, ax) |
| 59 | + |
| 60 | + # test the axis bboxes |
| 61 | + target = [ |
| 62 | + [123.375, 75.88888888888886, 983.25, 33.0], |
| 63 | + [85.51388888888889, 99.99999999999997, 53.375, 993.0] |
| 64 | + ] |
| 65 | + for nn, b in enumerate(bbaxis): |
| 66 | + targetbb = mtransforms.Bbox.from_bounds(*target[nn]) |
| 67 | + assert_array_almost_equal(b.bounds, targetbb.bounds, decimal=2) |
| 68 | + |
| 69 | + target = [ |
| 70 | + [150.0, 119.999, 930.0, 11.111], |
| 71 | + [150.0, 1080.0, 930.0, 0.0], |
| 72 | + [150.0, 119.9999, 11.111, 960.0], |
| 73 | + [1068.8888, 119.9999, 11.111, 960.0] |
| 74 | + ] |
| 75 | + for nn, b in enumerate(bbspines): |
| 76 | + targetbb = mtransforms.Bbox.from_bounds(*target[nn]) |
| 77 | + assert_array_almost_equal(b.bounds, targetbb.bounds, decimal=2) |
| 78 | + |
| 79 | + target = [150.0, 119.99999999999997, 930.0, 960.0] |
| 80 | + targetbb = mtransforms.Bbox.from_bounds(*target) |
| 81 | + assert_array_almost_equal(bbax.bounds, targetbb.bounds, decimal=2) |
| 82 | + |
| 83 | + target = [85.5138, 75.88888, 1021.11, 1017.11] |
| 84 | + targetbb = mtransforms.Bbox.from_bounds(*target) |
| 85 | + assert_array_almost_equal(bbtb.bounds, targetbb.bounds, decimal=2) |
| 86 | + |
| 87 | + # test that get_position roundtrips to get_window_extent |
| 88 | + axbb = ax.get_position().transformed(fig.transFigure).bounds |
| 89 | + assert_array_almost_equal(axbb, ax.get_window_extent().bounds, decimal=2) |
| 90 | + |
| 91 | + |
| 92 | +def test_nodecorator(): |
| 93 | + with rc_context({'_internal.classic_mode': False}): |
| 94 | + fig, ax = plt.subplots(dpi=200, figsize=(6, 6)) |
| 95 | + fig.canvas.draw() |
| 96 | + ax.set(xticklabels=[], yticklabels=[]) |
| 97 | + bbaxis, bbspines, bbax, bbtb = color_boxes(fig, ax) |
| 98 | + |
| 99 | + # test the axis bboxes |
| 100 | + target = [ |
| 101 | + None, |
| 102 | + None |
| 103 | + ] |
| 104 | + for nn, b in enumerate(bbaxis): |
| 105 | + assert b is None |
| 106 | + |
| 107 | + target = [ |
| 108 | + [150.0, 119.999, 930.0, 11.111], |
| 109 | + [150.0, 1080.0, 930.0, 0.0], |
| 110 | + [150.0, 119.9999, 11.111, 960.0], |
| 111 | + [1068.8888, 119.9999, 11.111, 960.0] |
| 112 | + ] |
| 113 | + for nn, b in enumerate(bbspines): |
| 114 | + targetbb = mtransforms.Bbox.from_bounds(*target[nn]) |
| 115 | + assert_allclose(b.bounds, targetbb.bounds, atol=1e-2) |
| 116 | + |
| 117 | + target = [150.0, 119.99999999999997, 930.0, 960.0] |
| 118 | + targetbb = mtransforms.Bbox.from_bounds(*target) |
| 119 | + assert_allclose(bbax.bounds, targetbb.bounds, atol=1e-2) |
| 120 | + |
| 121 | + target = [150., 120., 930., 960.] |
| 122 | + targetbb = mtransforms.Bbox.from_bounds(*target) |
| 123 | + assert_allclose(bbtb.bounds, targetbb.bounds, atol=1e-2) |
| 124 | + |
| 125 | + |
| 126 | +def test_displaced_spine(): |
| 127 | + with rc_context({'_internal.classic_mode': False}): |
| 128 | + fig, ax = plt.subplots(dpi=200, figsize=(6, 6)) |
| 129 | + ax.set(xticklabels=[], yticklabels=[]) |
| 130 | + ax.spines['bottom'].set_position(('axes', -0.1)) |
| 131 | + fig.canvas.draw() |
| 132 | + bbaxis, bbspines, bbax, bbtb = color_boxes(fig, ax) |
| 133 | + |
| 134 | + target = [ |
| 135 | + [150., 24., 930., 11.111111], |
| 136 | + [150.0, 1080.0, 930.0, 0.0], |
| 137 | + [150.0, 119.9999, 11.111, 960.0], |
| 138 | + [1068.8888, 119.9999, 11.111, 960.0] |
| 139 | + ] |
| 140 | + for nn, b in enumerate(bbspines): |
| 141 | + targetbb = mtransforms.Bbox.from_bounds(*target[nn]) |
| 142 | + |
| 143 | + target = [150.0, 119.99999999999997, 930.0, 960.0] |
| 144 | + targetbb = mtransforms.Bbox.from_bounds(*target) |
| 145 | + assert_allclose(bbax.bounds, targetbb.bounds, atol=1e-2) |
| 146 | + |
| 147 | + target = [150., 24., 930., 1056.] |
| 148 | + targetbb = mtransforms.Bbox.from_bounds(*target) |
| 149 | + assert_allclose(bbtb.bounds, targetbb.bounds, atol=1e-2) |
| 150 | + |
| 151 | + |
| 152 | +def test_tickdirs(): |
| 153 | + """ |
| 154 | + Switch the tickdirs and make sure the bboxes switch with them |
| 155 | + """ |
| 156 | + targets = [[[150.0, 120.0, 930.0, 11.1111], |
| 157 | + [150.0, 120.0, 11.111, 960.0]], |
| 158 | + [[150.0, 108.8889, 930.0, 11.111111111111114], |
| 159 | + [138.889, 120, 11.111, 960.0]], |
| 160 | + [[150.0, 114.44444444444441, 930.0, 11.111111111111114], |
| 161 | + [144.44444444444446, 119.999, 11.111, 960.0]]] |
| 162 | + for dnum, dirs in enumerate(['in', 'out', 'inout']): |
| 163 | + with rc_context({'_internal.classic_mode': False}): |
| 164 | + fig, ax = plt.subplots(dpi=200, figsize=(6, 6)) |
| 165 | + ax.tick_params(direction=dirs) |
| 166 | + fig.canvas.draw() |
| 167 | + bbaxis, bbspines, bbax, bbtb = color_boxes(fig, ax) |
| 168 | + for nn, num in enumerate([0, 2]): |
| 169 | + targetbb = mtransforms.Bbox.from_bounds(*targets[dnum][nn]) |
| 170 | + assert_allclose(bbspines[num].bounds, targetbb.bounds, |
| 171 | + atol=1e-2) |
| 172 | + |
| 173 | + |
| 174 | +def test_minor_accountedfor(): |
| 175 | + with rc_context({'_internal.classic_mode': False}): |
| 176 | + fig, ax = plt.subplots(dpi=200, figsize=(6, 6)) |
| 177 | + fig.canvas.draw() |
| 178 | + ax.tick_params(which='both', direction='out') |
| 179 | + |
| 180 | + bbaxis, bbspines, bbax, bbtb = color_boxes(fig, ax) |
| 181 | + bbaxis, bbspines, bbax, bbtb = color_boxes(fig, ax) |
| 182 | + targets = [[150.0, 108.88888888888886, 930.0, 11.111111111111114], |
| 183 | + [138.8889, 119.9999, 11.1111, 960.0]] |
| 184 | + for n in range(2): |
| 185 | + targetbb = mtransforms.Bbox.from_bounds(*targets[n]) |
| 186 | + assert_allclose(bbspines[n * 2].bounds, targetbb.bounds, |
| 187 | + atol=1e-2) |
| 188 | + |
| 189 | + fig, ax = plt.subplots(dpi=200, figsize=(6, 6)) |
| 190 | + fig.canvas.draw() |
| 191 | + ax.tick_params(which='both', direction='out') |
| 192 | + ax.minorticks_on() |
| 193 | + ax.tick_params(axis='both', which='minor', length=30) |
| 194 | + fig.canvas.draw() |
| 195 | + bbaxis, bbspines, bbax, bbtb = color_boxes(fig, ax) |
| 196 | + targets = [[150.0, 36.66666666666663, 930.0, 83.33333333333334], |
| 197 | + [66.6667, 120.0, 83.3333, 960.0]] |
| 198 | + |
| 199 | + for n in range(2): |
| 200 | + targetbb = mtransforms.Bbox.from_bounds(*targets[n]) |
| 201 | + assert_allclose(bbspines[n * 2].bounds, targetbb.bounds, |
| 202 | + atol=1e-2) |
0 commit comments