8000 Redo · matplotlib/matplotlib@9c98295 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9c98295

Browse files
committed
Redo
1 parent 3b1be53 commit 9c98295

File tree

2 files changed

+75
-32
lines changed

2 files changed

+75
-32
lines changed

lib/matplotlib/figure.py

Lines changed: 59 additions & 27 deletions
F438
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@ def __init__(self):
233233
del self._axes
234234

235235
self._suptitle = None
236+
self._supxlabel = None
237+
self._supylabel = None
236238

237239
# constrained_layout:
238240
self._layoutgrid = None
@@ -254,7 +256,6 @@ def __init__(self):
254256
self.images = []
255257
self.legends = []
256258
self.subfigs = []
257-
self._suptitle = None
258259
self.stale = True
259260
self.suppressComposite = None
260261

@@ -369,26 +370,27 @@ def get_window_extent(self, *args, **kwargs):
369370
"""
370371
return self.bbox
371372

372-
def suptitle(self, t, **kwargs):
373+
def _suplabels(self, t, info, **kwargs):
373374
"""
374-
Add a centered title to the figure.
375+
Add a centered {name} to the figure.
375376
376377
Parameters
377378
----------
378379
t : str
379-
The title text.
380+
The {name} text.
380381
381-
x : float, default: 0.5
382+
x : float, default {x0}
382383
The x location of the text in figure coordinates.
383384
384-
y : float, default: 0.98
385+
y : float, default {y0}
385386
The y location of the text in figure coordinates.
386387
387-
horizontalalignment, ha : {'center', 'left', right'}, default: 'center'
388+
horizontalalignment, ha : 'center', 'left', 'right', \
389+
default: {ha}
388390
The horizontal alignment of the text relative to (*x*, *y*).
389391
390-
verticalalignment, va : {'top', 'center', 'bottom', 'baseline'}, \
391-
default: 'top'
392+
verticalalignment, va : 'top', 'center', 'bottom', 'baseline', \
393+
default: {va}
392394
The vertical alignment of the text relative to (*x*, *y*).
393395
394396
fontsize, size : default: :rc:`figure.titlesize`
@@ -401,8 +403,8 @@ def suptitle(self, t, **kwargs):
401403
402404
Returns
403405
-------
404-
`.Text`
405-
The instance of the title.
406+
text
407+
The `.Text` instance of the {name}.
406408
407409
Other Parameters
408410
----------------
@@ -415,19 +417,20 @@ def suptitle(self, t, **kwargs):
415417
**kwargs
416418
Additional kwargs are `matplotlib.text.Text` properties.
417419
418-
Examples
419-
--------
420-
>>> fig.suptitle('This is the figure title', fontsize=12)
421420
"""
421+
422422
manual_position = ('x' in kwargs or 'y' in kwargs)
423+
suplab = getattr(self, info['name'])
423424

424-
x = kwargs.pop('x', 0.5)
425-
y = kwargs.pop('y', 0.98)
425+
x = kwargs.pop('x', info['x0'])
426+
y = kwargs.pop('y', info['y0'])
426427

427428
if 'horizontalalignment' not in kwargs and 'ha' not in kwargs:
428-
kwargs['horizontalalignment'] = 'center'
429+
kwargs['horizontalalignment'] = info['ha']
429430
if 'verticalalignment' not in kwargs and 'va' not in kwargs:
430-
kwargs['verticalalignment'] = 'top'
431+
kwargs['verticalalignment'] = info['va']
432+
if 'rotation' not in kwargs:
433+
kwargs['rotation'] = info['rotation']
431434

432435
if 'fontproperties' not in kwargs:
433436
if 'fontsize' not in kwargs and 'size' not in kwargs:
@@ -436,19 +439,45 @@ def suptitle(self, t, **kwargs):
436439
kwargs['weight'] = mpl.rcParams['figure.titleweight']
437440

438441
sup = self.text(x, y, t, **kwargs)
439-
if self._suptitle is not None:
440-
self._suptitle.set_text(t)
441-
self._suptitle.set_position((x, y))
442-
self._suptitle.update_from(sup)
442+
if suplab is not None:
443+
suplab.set_text(t)
444+
suplab.set_position((x, y))
445+
suplab.update_from(sup)
443446
sup.remove()
444447
else:
445-
self._suptitle = sup
446-
448+
suplab = sup
447449
if manual_position:
448-
self._suptitle.set_in_layout(False)
449-
450+
suplab.set_in_layout(False)
451+
setattr(self, info['name'], suplab)
450452
self.stale = True
451-
return self._suptitle
453+
return suplab
454+
455+
def D966 suptitle(self, t, **kwargs):
456+
# docstring from _suplabels...
457+
info = {'name': '_suptitle', 'x0': 0.5, 'y0': 0.98,
458+
'ha': 'center', 'va': 'top', 'rotation': 0}
459+
return self._suplabels(t, info, **kwargs)
460+
461+
suptitle.__doc__ = _suplabels.__doc__.format(
462+
x0=0.5, y0=0.98, name='suptitle', ha='center', va='top')
463+
464+
def supxlabel(self, t, **kwargs):
465+
# docstring from _suplabels...
466+
info = {'name': '_supxlabel', 'x0': 0.5, 'y0': 0.01,
467+
'ha': 'center', 'va': 'bottom', 'rotation': 0}
468+
return self._suplabels(t, info, **kwargs)
469+
470+
supxlabel.__doc__ = _suplabels.__doc__.format(
471+
x0=0.5, y0=0.01, name='supxlabel', ha='center', va='bottom')
472+
473+
def supylabel(self, t, **kwargs):
474+
# docstring from _suplabels...
475+
info = {'name': '_supylabel', 'x0': 0.02, 'y0': 0.5,
476+
'ha': 'left', 'va': 'center', 'rotation': 90}
477+
return self._suplabels(t, info, **kwargs)
478+
479+
supylabel.__doc__ = _suplabels.__doc__.format(
480+
x0=0.02, y0=0.5, name='supylabel', ha='left', va='center')
452481

453482
def get_edgecolor(self):
454483
"""Get the edge color of the Figure rectangle."""
@@ -2801,6 +2830,9 @@ def clf(self, keep_observers=False):
28012830
if not keep_observers:
28022831
self._axobservers = cbook.CallbackRegistry()
28032832
self._suptitle = None
2833+
self._supxlabel = None
2834+
self._supylabel = None
2835+
28042836
if self.get_constrained_layout():
28052837
self.init_layoutgrid()
28062838
self.stale = True

lib/matplotlib/tight_layout.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,20 +108,31 @@ def auto_adjust_subplotpars(
108108
if not margin_left:
109109
margin_left = (max(hspaces[:, 0].max(), 0)
110110
+ pad_inches / fig_width_inch)
111+
suplabel = fig._supylabel
112+
if suplabel and suplabel.get_in_layout():
113+
rel_width = fig.transFigure.inverted().transform_bbox(
114+
suplabel.get_window_extent(renderer)).width
115+
margin_left += rel_width + pad_inches / fig_width_inch
116+
111117
if not margin_right:
112118
margin_right = (max(hspaces[:, -1].max(), 0)
113119
+ pad_inches / fig_width_inch)
114120
if not margin_top:
115121
margin_top = (max(vspaces[0, :].max(), 0)
116122
+ pad_inches / fig_height_inch)
117-
suptitle = fig._suptitle
118-
if suptitle and suptitle.get_in_layout():
119-
rel_suptitle_height = fig.transFigure.inverted().transform_bbox(
120-
suptitle.get_window_extent(renderer)).height
121-
margin_top += rel_suptitle_height + pad_inches / fig_height_inch
123+
suplabel = fig._suptitle
124+
if suplabel and suplabel.get_in_layout():
125+
rel_height = fig.transFigure.inverted().transform_bbox(
126+
suplabel.get_window_extent(renderer)).height
127+
margin_top += rel_height + pad_inches / fig_height_inch
122128
if not margin_bottom:
123129
margin_bottom = (max(vspaces[-1, :].max(), 0)
124130
+ pad_inches / fig_height_inch)
131+
suplabel = fig._supxlabel
132+
if suplabel and suplabel.get_in_layout():
133+
rel_height = fig.transFigure.inverted().transform_bbox(
134+
suplabel.get_window_extent(renderer)).height
135+
margin_bottom += rel_height + pad_inches / fig_height_inch
125136

126137
if margin_left + margin_right >= 1:
127138
cbook._warn_external('Tight layout not applied. The left and right '

0 commit comments

Comments
 (0)
0