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