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