17
17
18
18
import matplotlib
19
19
import matplotlib as mpl
20
+ from matplotlib import rc_context
21
+ import matplotlib .transforms as mtransforms
20
22
from matplotlib .testing .decorators import (
21
23
image_comparison , check_figures_equal , remove_ticks_and_titles )
22
24
import matplotlib .pyplot as plt
23
25
import matplotlib .markers as mmarkers
24
26
import matplotlib .patches as mpatches
25
27
import matplotlib .colors as mcolors
28
+
26
29
from numpy .testing import (
27
30
assert_allclose , assert_array_equal , assert_array_almost_equal )
28
31
from matplotlib .cbook import (
@@ -6035,3 +6038,204 @@ def invert(x):
6035
6038
fig .canvas .draw ()
6036
6039
fig .set_size_inches ((7 , 4 ))
6037
6040
assert_allclose (ax .get_position ().extents , [0.125 , 0.1 , 0.9 , 0.9 ])
6041
+
6042
+
6043
+
6044
+
6045
+ def color_boxes (fig , axs ):
6046
+ """
6047
+ Helper for extent tests below:
6048
+ """
6049
+ fig .canvas .draw ()
6050
+
6051
+ renderer = fig .canvas .get_renderer ()
6052
+ bbaxis = []
6053
+ for nn , axx in enumerate ([axs .xaxis , axs .yaxis ]):
6054
+ bb = axx .get_tightbbox (renderer )
6055
+ if bb :
6056
+ axisr = plt .Rectangle ((bb .x0 , bb .y0 ), width = bb .width ,
6057
+ height = bb .height , linewidth = 0.7 , edgecolor = 'y' ,
6058
+ facecolor = "none" , transform = None , zorder = 3 )
6059
+ fig .add_artist (axisr )
6060
+ bbaxis += [bb ]
6061
+
6062
+ bbspines = []
6063
+ for nn , a in enumerate (['bottom' , 'top' , 'left' , 'right' ]):
6064
+ bb = axs .spines [a ].get_window_extent (renderer )
6065
+ if bb :
6066
+ spiner = plt .Rectangle ((bb .x0 , bb .y0 ), width = bb .width ,
6067
+ height = bb .height , linewidth = 0.7 ,
6068
+ edgecolor = "green" , facecolor = "none" ,
6069
+ transform = None , zorder = 3 )
6070
+ fig .add_artist (spiner )
6071
+ bbspines += [bb ]
6072
+
6073
+ bb = axs .get_window_extent ()
6074
+ if bb :
6075
+ rect2 = plt .Rectangle ((bb .x0 , bb .y0 ), width = bb .width , height = bb .height ,
6076
+ linewidth = 1.5 , edgecolor = "magenta" ,
6077
+ facecolor = "none" , transform = None , zorder = 2 )
6078
+ fig .add_artist (rect2 )
6079
+ bbax = bb
6080
+
6081
+ bb2 = axs .get_tightbbox (renderer )
6082
+ if bb2 :
6083
+ rect2 = plt .Rectangle ((bb2 .x0 , bb2 .y0 ), width = bb2 .width ,
6084
+ height = bb2 .height , linewidth = 3 , edgecolor = "red" ,
6085
+ facecolor = "none" , transform = None , zorder = 1 )
6086
+ fig .add_artist (rect2 )
6087
+ bbtb = bb2
6088
+ return bbaxis , bbspines , bbax , bbtb
6089
+
6090
+
6091
+ def test_normal_axes_extents ():
6092
+ with rc_context ({'_internal.classic_mode' : False }):
6093
+ fig , ax = plt .subplots (dpi = 200 , figsize = (6 , 6 ))
6094
+ fig .canvas .draw ()
6095
+ plt .close (fig )
6096
+ bbaxis , bbspines , bbax , bbtb = color_boxes (fig , ax )
6097
+
6098
+ # test the axis bboxes
6099
+ target = [
6100
+ [123.375 , 75.88888888888886 , 983.25 , 33.0 ],
6101
+ [85.51388888888889 , 99.99999999999997 , 53.375 , 993.0 ]
6102
+ ]
6103
+ for nn , b in enumerate (bbaxis ):
6104
+ targetbb = mtransforms .Bbox .from_bounds (* target [nn ])
6105
+ assert_array_almost_equal (b .bounds , targetbb .bounds , decimal = 2 )
6106
+
6107
+ target = [
6108
+ [150.0 , 119.999 , 930.0 , 11.111 ],
6109
+ [150.0 , 1080.0 , 930.0 , 0.0 ],
6110
+ [150.0 , 119.9999 , 11.111 , 960.0 ],
6111
+ [1068.8888 , 119.9999 , 11.111 , 960.0 ]
6112
+ ]
6113
+ for nn , b in enumerate (bbspines ):
6114
+ targetbb = mtransforms .Bbox .from_bounds (* target [nn ])
6115
+ assert_array_almost_equal (b .bounds , targetbb .bounds , decimal = 2 )
6116
+
6117
+ target = [150.0 , 119.99999999999997 , 930.0 , 960.0 ]
6118
+ targetbb = mtransforms .Bbox .from_bounds (* target )
6119
+ assert_array_almost_equal (bbax .bounds , targetbb .bounds , decimal = 2 )
6120
+
6121
+ target = [85.5138 , 75.88888 , 1021.11 , 1017.11 ]
6122
+ targetbb = mtransforms .Bbox .from_bounds (* target )
6123
+ assert_array_almost_equal (bbtb .bounds , targetbb .bounds , decimal = 2 )
6124
+
6125
+ # test that get_position roundtrips to get_window_extent
6126
+ axbb = ax .get_position ().transformed (fig .transFigure ).bounds
6127
+ assert_array_almost_equal (axbb , ax .get_window_extent ().bounds , decimal = 2 )
6128
+
6129
+
6130
+
6131
+ def test_nodecorator_extents ():
6132
+ with rc_context ({'_internal.classic_mode' : False }):
6133
+ fig , ax = plt .subplots (dpi = 200 , figsize = (6 , 6 ))
6134
+ fig .canvas .draw ()
6135
+ ax .set (xticklabels = [], yticklabels = [])
6136
+ bbaxis , bbspines , bbax , bbtb = color_boxes (fig , ax )
6137
+
6138
+ # test the axis bboxes
6139
+ target = [
6140
+ None ,
6141
+ None
6142
+ ]
6143
+ for nn , b in enumerate (bbaxis ):
6144
+ assert b is None
6145
+
6146
+ target = [
6147
+ [150.0 , 119.999 , 930.0 , 11.111 ],
6148
+ [150.0 , 1080.0 , 930.0 , 0.0 ],
6149
+ [150.0 , 119.9999 , 11.111 , 960.0 ],
6150
+ [1068.8888 , 119.9999 , 11.111 , 960.0 ]
6151
+ ]
6152
+ for nn , b in enumerate (bbspines ):
6153
+ targetbb = mtransforms .Bbox .from_bounds (* target [nn ])
6154
+ assert_allclose (b .bounds , targetbb .bounds , atol = 1e-2 )
6155
+
6156
+ target = [150.0 , 119.99999999999997 , 930.0 , 960.0 ]
6157
+ targetbb = mtransforms .Bbox .from_bounds (* target )
6158
+ assert_allclose (bbax .bounds , targetbb .bounds , atol = 1e-2 )
6159
+
6160
+ target = [150. , 120. , 930. , 960. ]
6161
+ targetbb = mtransforms .Bbox .from_bounds (* target )
6162
+ assert_allclose (bbtb .bounds , targetbb .bounds , atol = 1e-2 )
6163
+
6164
+
6165
+ def test_displaced_spine_extents ():
6166
+ with rc_context ({'_internal.classic_mode' : False }):
6167
+ fig , ax = plt .subplots (dpi = 200 , figsize = (6 , 6 ))
6168
+ ax .set (xticklabels = [], yticklabels = [])
6169
+ ax .spines ['bottom' ].set_position (('axes' , - 0.1 ))
6170
+ fig .canvas .draw ()
6171
+ bbaxis , bbspines , bbax , bbtb = color_boxes (fig , ax )
6172
+
6173
+ target = [
6174
+ [150. , 24. , 930. , 11.111111 ],
6175
+ [150.0 , 1080.0 , 930.0 , 0.0 ],
6176
+ [150.0 , 119.9999 , 11.111 , 960.0 ],
6177
+ [1068.8888 , 119.9999 , 11.111 , 960.0 ]
6178
+ ]
6179
+ for nn , b in enumerate (bbspines ):
6180
+ targetbb = mtransforms .Bbox .from_bounds (* target [nn ])
6181
+
6182
+ target = [150.0 , 119.99999999999997 , 930.0 , 960.0 ]
6183
+ targetbb = mtransforms .Bbox .from_bounds (* target )
6184
+ assert_allclose (bbax .bounds , targetbb .bounds , atol = 1e-2 )
6185
+
6186
+ target = [150. , 24. , 930. , 1056. ]
6187
+ targetbb = mtransforms .Bbox .from_bounds (* target )
6188
+ assert_allclose (bbtb .bounds , targetbb .bounds , atol = 1e-2 )
6189
+
6190
+
6191
+ def test_tickdirs_extents ():
6192
+ """
6193
+ Switch the tickdirs and make sure the bboxes switch with them
6194
+ """
6195
+ targets = [[[150.0 , 120.0 , 930.0 , 11.1111 ],
6196
+ [150.0 , 120.0 , 11.111 , 960.0 ]],
6197
+ [[150.0 , 108.8889 , 930.0 , 11.111111111111114 ],
6198
+ [138.889 , 120 , 11.111 , 960.0 ]],
6199
+ [[150.0 , 114.44444444444441 , 930.0 , 11.111111111111114 ],
6200
+ [144.44444444444446 , 119.999 , 11.111 , 960.0 ]]]
6201
+ for dnum , dirs in enumerate (['in' , 'out' , 'inout' ]):
6202
+ with rc_context ({'_internal.classic_mode' : False }):
6203
+ fig , ax = plt .subplots (dpi = 200 , figsize = (6 , 6 ))
6204
+ ax .tick_params (direction = dirs )
6205
+ fig .canvas .draw ()
6206
+ bbaxis , bbspines , bbax , bbtb = color_boxes (fig , ax )
6207
+ for nn , num in enumerate ([0 , 2 ]):
6208
+ targetbb = mtransforms .Bbox .from_bounds (* targets [dnum ][nn ])
6209
+ assert_allclose (bbspines [num ].bounds , targetbb .bounds ,
6210
+ atol = 1e-2 )
6211
+
6212
+
6213
+ def test_minor_accountedfor_extents ():
6214
+ with rc_context ({'_internal.classic_mode' : False }):
6215
+ fig , ax = plt .subplots (dpi = 200 , figsize = (6 , 6 ))
6216
+ fig .canvas .draw ()
6217
+ ax .tick_params (which = 'both' , direction = 'out' )
6218
+
6219
+ bbaxis , bbspines , bbax , bbtb = color_boxes (fig , ax )
6220
+ bbaxis , bbspines , bbax , bbtb = color_boxes (fig , ax )
6221
+ targets = [[150.0 , 108.88888888888886 , 930.0 , 11.111111111111114 ],
6222
+ [138.8889 , 119.9999 , 11.1111 , 960.0 ]]
6223
+ for n in range (2 ):
6224
+ targetbb = mtransforms .Bbox .from_bounds (* targets [n ])
6225
+ assert_allclose (bbspines [n * 2 ].bounds , targetbb .bounds ,
6226
+ atol = 1e-2 )
6227
+
6228
+ fig , ax = plt .subplots (dpi = 200 , figsize = (6 , 6 ))
6229
+ fig .canvas .draw ()
6230
+ ax .tick_params (which = 'both' , direction = 'out' )
6231
+ ax .minorticks_on ()
6232
+ ax .tick_params (axis = 'both' , which = 'minor' , length = 30 )
6233
+ fig .canvas .draw ()
6234
+ bbaxis , bbspines , bbax , bbtb = color_boxes (fig , ax )
6235
+ targets = [[150.0 , 36.66666666666663 , 930.0 , 83.33333333333334 ],
6236
+ [66.6667 , 120.0 , 83.3333 , 960.0 ]]
6237
+
6238
+ for n in range (2 ):
6239
+ targetbb = mtransforms .Bbox .from_bounds (* targets [n ])
6240
+ assert_allclose (bbspines [n * 2 ].bounds , targetbb .bounds ,
6241
+ atol = 1e-2 )
0 commit comments