21
21
from matplotlib import lines as mlines
22
22
from matplotlib import mlab
23
23
from matplotlib import patches as mpatches
24
+ from matplotlib import path as mpath
24
25
from matplotlib import quiver as mquiver
25
26
from matplotlib import scale as mscale
26
27
from matplotlib import table as mtable
@@ -770,13 +771,14 @@ def cla(self):
770
771
771
772
self .grid (self ._gridOn )
772
773
props = font_manager .FontProperties (size = rcParams ['axes.titlesize' ])
773
- self .title = mtext .Text (
774
- x = 0.5 , y = 1.02 , text = '' ,
774
+ self .titleOffsetTrans = mtransforms .Affine2D ()
775
+ self .title = mtext .Text (
776
+ x = 0.5 , y = 1.00 , text = '' ,
775
777
fontproperties = props ,
776
778
verticalalignment = 'bottom' ,
777
779
horizontalalignment = 'center' ,
778
780
)
779
- self .title .set_transform (self .transAxes )
781
+ self .title .set_transform (self .transAxes + self . titleOffsetTrans )
780
782
self .title .set_clip_box (None )
781
783
782
784
self ._set_artist_props (self .title )
@@ -800,6 +802,8 @@ def cla(self):
800
802
self .xaxis .set_clip_path (self .axesPatch )
801
803
self .yaxis .set_clip_path (self .axesPatch )
802
804
805
+ self .titleOffsetTrans .clear ()
806
+
803
807
def clear (self ):
804
808
'clear the axes'
805
809
self .cla ()
@@ -905,14 +909,14 @@ def get_data_ratio(self):
905
909
ysize = max (math .fabs (ymax - ymin ), 1e-30 )
906
910
return ysize / xsize
907
911
908
- def apply_aspect (self ):
912
+ def apply_aspect (self , currentPosition ):
909
913
'''
910
914
Use self._aspect and self._adjustable to modify the
911
915
axes box or the view limits.
912
916
'''
913
917
aspect = self .get_aspect ()
914
918
if aspect == 'auto' :
915
- self .set_position ( self . _originalPosition , 'active' )
919
+ self .set_position (currentPosition , 'active' )
916
920
return
917
921
918
922
if aspect == 'equal' :
@@ -929,7 +933,7 @@ def apply_aspect(self):
929
933
fig_aspect = figH / figW
930
934
if self ._adjustable == 'box' :
931
935
box_aspect = A * self .get_data_ratio ()
932
- pb = self . _originalPosition .frozen ()
936
+ pb = currentPosition .frozen ()
933
937
pb1 = pb .shrunk_to_aspect (box_aspect , pb , fig_aspect )
934
938
self .set_position (pb1 .anchored (self .get_anchor (), pb ), 'active' )
935
939
return
@@ -1289,25 +1293,48 @@ def autoscale_view(self, tight=False, scalex=True, scaley=True):
1289
1293
YL = ylocator .autoscale ()
1290
1294
self .set_ybound (YL )
1291
1295
1296
+ def adjust_for_axis_text (self , renderer ):
1297
+ pad_pixels = rcParams ['xtick.major.pad' ] * self .figure .dpi / 72.0
1298
+ inverse_transFigure = self .figure .transFigure .inverted ()
1299
+ t_text , b_text = self .xaxis .get_text_heights (renderer )
1300
+ l_text , r_text = self .yaxis .get_text_widths (renderer )
1301
+ title_height = self .title .get_window_extent (renderer ).height
1302
+ title_height += pad_pixels * 2.0
1303
+ original_t_text = t_text
1304
+
1305
+ ((l_text , t_text ),
1306
+ (r_text , b_text ),
1307
+ (dummy , title_height )) = inverse_transFigure .transform (
1308
+ ((l_text , t_text ),
1309
+ (r_text , b_text ),
1310
+ (0.0 , title_height )))
1311
+ x0 , y0 , x1 , y1 = self .get_position (True ).extents
1312
+ # Adjust the title
1313
+ self .titleOffsetTrans .clear ().translate (
1314
+ 0 , original_t_text + pad_pixels * 2.0 )
1315
+ return mtransforms .Bbox .from_extents (
1316
+ x0 + l_text , y0 + b_text , x1 - r_text ,
1317
+ y1 - t_text - title_height )
1318
+
1292
1319
#### Drawing
1293
1320
def draw (self , renderer = None , inframe = False ):
1294
1321
"Draw everything (plot lines, axes, labels)"
1295
- if renderer is None :
1322
+ if renderer is None :
1296
1323
renderer = self ._cachedRenderer
1297
1324
1298
1325
if renderer is None :
1299
1326
raise RuntimeError ('No renderer defined' )
1300
1327
if not self .get_visible (): return
1301
1328
renderer .open_group ('axes' )
1302
- self .apply_aspect ()
1329
+
1330
+ currentPosition = self .adjust_for_axis_text (renderer )
1331
+ self .apply_aspect (currentPosition )
1303
1332
1304
1333
if self .axison and self ._frameon :
1305
1334
self .axesPatch .draw (renderer )
1306
1335
1307
1336
artists = []
1308
1337
1309
-
1310
-
1311
1338
if len (self .images )<= 1 or renderer .option_image_nocomposite ():
1312
1339
for im in self .images :
1313
1340
im .draw (renderer )
@@ -1319,7 +1346,6 @@ def draw(self, renderer=None, inframe=False):
1319
1346
ims = [(im .make_image (mag ),0 ,0 )
1320
1347
for im in self .images if im .get_visible ()]
1321
1348
1322
-
1323
1349
im = mimage .from_images (self .bbox .height * mag ,
1324
1350
self .bbox .width * mag ,
1325
1351
ims )
@@ -1360,6 +1386,14 @@ def draw(self, renderer=None, inframe=False):
1360
1386
a .draw (renderer )
1361
1387
1362
1388
renderer .close_group ('axes' )
1389
+
1390
+ # ### DEBUGGING
1391
+ # gc = renderer.new_gc()
1392
+ # gc.set_linewidth(2.0)
1393
+ # x0, y0, x1, y1 = self.get_position(True).extents
1394
+ # renderer.draw_path(gc, mpath.Path(
1395
+ # [[x0, y0], [x0, y1], [x1, y1], [x1, y0], [x0, y0]]),
1396
+ # self.figure.transFigure)
1363
1397
self ._cachedRenderer = renderer
1364
1398
1365
1399
def draw_artist (self , a ):
0 commit comments