@@ -291,15 +291,19 @@ def draw_legend(self, plot):
291
291
return plot
292
292
293
293
position = plot .theme ._params ['legend_position' ]
294
+
294
295
# At what point (e.g [.94, .5]) on the figure
295
296
# to place which point (e.g 6, for center left) of
296
297
# the legend box
298
+ _x = 0.92
299
+ # Prevent overlap with the facet label
300
+ if isinstance (plot .facet , facet_grid ):
301
+ _x += .025 * len (plot .facet .rows )
297
302
lookup = {
298
- 'right' : (6 , (0.92 , 0.5 )), # center left
303
+ 'right' : (6 , (_x , 0.5 )), # center left
299
304
'left' : (7 , (0.07 , 0.5 )), # center right
300
305
'top' : (8 , (0.5 , 0.92 )), # bottom center
301
- 'bottom' : (9 , (0.5 , 0.07 )) # upper center
302
- }
306
+ 'bottom' : (9 , (0.5 , 0.07 ))} # upper center
303
307
loc , box_to_anchor = lookup [position ]
304
308
anchored_box = AnchoredOffsetbox (
305
309
loc = loc ,
@@ -309,8 +313,8 @@ def draw_legend(self, plot):
309
313
# Spacing goes here
310
314
bbox_to_anchor = box_to_anchor ,
311
315
bbox_transform = plot .figure .transFigure ,
312
- borderpad = 0. ,
313
- )
316
+ borderpad = 0. )
317
+
314
318
plot .figure ._themeable ['legend_background' ] = anchored_box
315
319
ax = plot .axs [0 ]
316
320
ax .add_artist (anchored_box )
@@ -387,14 +391,25 @@ def add_labels_and_title(plot):
387
391
xlabel = plot .labels .get ('x' , '' )
388
392
ylabel = plot .labels .get ('y' , '' )
389
393
title = plot .labels .get ('title' , '' )
394
+ center = 0.5
395
+
396
+ # This is finicky. Should be changed when MPL
397
+ # finally has a constraint based layout manager.
398
+ xtitle_y = 0.08
399
+ ytitle_x = 0.09
400
+ title_y = 0.92
401
+ if isinstance (plot .facet , facet_wrap ):
402
+ title_y += 0.025 * len (plot .facet .vars )
403
+ elif isinstance (plot .facet , facet_grid ):
404
+ title_y += 0.04 * len (plot .facet .cols )
390
405
391
406
d = dict (
392
- axis_title_x = fig .text (0.5 , 0.08 , xlabel ,
407
+ axis_title_x = fig .text (center , xtitle_y , xlabel ,
393
408
ha = 'center' , va = 'top' ),
394
- axis_title_y = fig .text (0.09 , 0.5 , ylabel ,
409
+ axis_title_y = fig .text (ytitle_x , center , ylabel ,
395
410
ha = 'right' , va = 'center' ,
396
411
rotation = 'vertical' ),
397
- plot_title = fig .text (0.5 , 0.92 , title ,
412
+ plot_title = fig .text (center , title_y , title ,
398
413
ha = 'center' , va = 'bottom' ))
399
414
400
415
fig ._themeable .update (d )
0 commit comments