ENH: supxlabel and supylabel · matplotlib/matplotlib@d53bc29 · GitHub
Skip to content

Commit d53bc29

Browse files
committed
ENH: supxlabel and supylabel
1 parent 81b962a commit d53bc29

3 files changed

Lines changed: 84 additions & 37 deletions

File tree

lib/matplotlib/_constrained_layout.py

Lines changed: 11 additions & 9 deletions

lib/matplotlib/figure.py

Lines changed: 57 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -662,26 +662,27 @@ def get_window_extent(self, *args, **kwargs):
662662
"""
663663
return self.bbox
664664

665-
def suptitle(self, t, **kwargs):
665+
def _suplabels(self, t, info, **kwargs):
666666
"""
667-
Add a centered title to the figure.
667+
Add a centered {name} to the figure.
668668
669669
Parameters
670670
----------
671671
t : str
672-
The title text.
672+
The {name} text.
673673
674-
x : float, default 0.5
674+
x : float, default {x0}
675675
The x location of the text in figure coordinates.
676676
677-
y : float, default 0.98
677+
y : float, default {y0}
678678
The y location of the text in figure coordinates.
679679
680-
horizontalalignment, ha : {'center', 'left', right'}, default: 'center'
680+
horizontalalignment, ha : 'center', 'left', 'right', \
681+
default: {ha}
681682
The horizontal alignment of the text relative to (*x*, *y*).
682683
683-
verticalalignment, va : {'top', 'center', 'bottom', 'baseline'}, \
684-
default: 'top'
684+
verticalalignment, va : 'top', 'center', 'bottom', 'baseline', \
685+
default: {va}
685686
The vertical alignment of the text relative to (*x*, *y*).
686687
687688
fontsize, size : default: :rc:`figure.titlesize`
@@ -695,7 +696,7 @@ def suptitle(self, t, **kwargs):
695696
Returns
696697
-------
697698
text
698-
The `.Text` instance of the title.
699+
The `.Text` instance of the {name}.
699700
700701
Other Parameters
701702
----------------
@@ -708,19 +709,20 @@ def suptitle(self, t, **kwargs):
708709
**kwargs
709710
Additional kwargs are `matplotlib.text.Text` properties.
710711
711-
Examples
712-
--------
713-
>>> fig.suptitle('This is the figure title', fontsize=12)
714712
"""
713+
715714
manual_position = ('x' in kwargs or 'y' in kwargs)
715+
suplab = getattr(self, info['name'])
716716

717-
x = kwargs.pop('x', 0.5)
718-
y = kwargs.pop('y', 0.98)
717+
x = kwargs.pop('x', info['x0'])
718+
y = kwargs.pop('y', info['y0'])
719719

720720
if 'horizontalalignment' not in kwargs and 'ha' not in kwargs:
721-
kwargs['horizontalalignment'] = 'center'
721+
kwargs['horizontalalignment'] = info['ha']
722722
if 'verticalalignment' not in kwargs and 'va' not in kwargs:
723-
kwargs['verticalalignment'] = 'top'
723+
kwargs['verticalalignment'] = info['va']
724+
if 'rotation' not in kwargs:
725+
kwargs['rotation'] = info['rotation']
724726

725727
if 'fontproperties' not in kwargs:
726728
if 'fontsize' not in kwargs and 'size' not in kwargs:
@@ -729,17 +731,47 @@ def suptitle(self, t, **kwargs):
729731
kwargs['weight'] = mpl.rcParams['figure.titleweight']
730732

731733
sup = self.text(x, y, t, **kwargs)
732-
if self._suptitle is not None:
733-
self._suptitle.set_text(t)
734-
self._suptitle.set_position((x, y))
735-
self._suptitle.update_from(sup)
734+
if suplab is not None:
735+
suplab.set_text(t)
736+
suplab.set_position((x, y))
737+
suplab.update_from(sup)
736738
sup.remove()
737739
else:
738-
self._suptitle = sup
740+
suplab = sup
739741
if manual_position:
740-
self._suptitle.set_in_layout(False)
742+
suplab.set_in_layout(False)
743+
setattr(self, info['name'], suplab)
741744
self.stale = True
742-
return self._suptitle
745+
return suplab
746+
747+
748+
def suptitle(self, t, **kwargs):
749+
# docstring from _suplabels...
750+
info = {'name': '_suptitle', 'x0': 0.5, 'y0': 0.98,
751+
'ha': 'center', 'va': 'top', 'rotation': 0}
752+
return self._suplabels(t, info, **kwargs)
753+
754+
suptitle.__doc__ = _suplabels.__doc__.format(
755+
x0=0.5, y0=0.98, name='suptitle', ha='center', va='top')
756+
757+
def supxlabel(self, t, **kwargs):
758+
# docstring from _suplabels...
759+
info = {'name': '_supxlabel', 'x0': 0.5, 'y0': 0.01,
760+
'ha': 'center', 'va': 'bottom', 'rotation': 0}
761+
return self._suplabels(t, info, **kwargs)
762+
763+
supxlabel.__doc__ = _suplabels.__doc__.format(
764+
x0=0.5, y0=0.01, name='supxlabel', ha='center', va='bottom')
765+
766+
def supylabel(self, t, **kwargs):
767+
# docstring from _suplabels...
768+
info = {'name': '_supylabel', 'x0': 0.02, 'y0': 0.5,
769+
'ha': 'left', 'va': 'center', 'rotation': 90}
770+
return self._suplabels(t, info, **kwargs)
771+
772+
supylabel.__doc__ = _suplabels.__doc__.format(
773+
x0=0.02, y0=0.5, name='supylabel', ha='left', va='center')
774+
743775

744776
def set_canvas(self, canvas):
745777
"""
@@ -1603,6 +1635,8 @@ def clf(self, keep_observers=False):
16031635
if not keep_observers:
16041636
self._axobservers = cbook.CallbackRegistry()
16051637
self._suptitle = None
1638+
self._supxlabel = None
1639+
self._supylabel = None
16061640
if self.get_constrained_layout():
16071641
layoutgrid.nonetree(self._layoutgrid)
16081642
self.stale = True

lib/matplotlib/tight_layout.py

Lines changed: 16 additions & 5 deletions

0 commit comments

Comments
 (0)