Skip to content

Commit 2d0c3ba

Browse files
authored
Merge pull request matplotlib#25686 from timhoffm/get_suptitle
Add Figure methods get_suptitle(), get_subxlabel(), get_supylabel()
2 parents 81a2a69 + 0d3fda5 commit 2d0c3ba

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
``Figure.get_suptitle()``, ``Figure.get_supxlabel()``, ``Figure.get_supylabel()``
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
These methods return the strings set by ``Figure.suptitle()``, ``Figure.supxlabel()``
4+
and ``Figure.supylabel()`` respectively.

lib/matplotlib/figure.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,11 @@ def suptitle(self, t, **kwargs):
388388
'size': 'figure.titlesize', 'weight': 'figure.titleweight'}
389389
return self._suplabels(t, info, **kwargs)
390390

391+
def get_suptitle(self):
392+
"""Return the suptitle as string or an empty string if not set."""
393+
text_obj = self._suptitle
394+
return "" if text_obj is None else text_obj.get_text()
395+
391396
@_docstring.Substitution(x0=0.5, y0=0.01, name='supxlabel', ha='center',
392397
va='bottom', rc='label')
393398
@_docstring.copy(_suplabels)
@@ -398,6 +403,11 @@ def supxlabel(self, t, **kwargs):
398403
'size': 'figure.labelsize', 'weight': 'figure.labelweight'}
399404
return self._suplabels(t, info, **kwargs)
400405

406+
def get_supxlabel(self):
407+
"""Return the supxlabel as string or an empty string if not set."""
408+
text_obj = self._supxlabel
409+
return "" if text_obj is None else text_obj.get_text()
410+
401411
@_docstring.Substitution(x0=0.02, y0=0.5, name='supylabel', ha='left',
402412
va='center', rc='label')
403413
@_docstring.copy(_suplabels)
@@ -409,6 +419,11 @@ def supylabel(self, t, **kwargs):
409419
'weight': 'figure.labelweight'}
410420
return self._suplabels(t, info, **kwargs)
411421

422+
def get_supylabel(self):
423+
"""Return the supylabel as string or an empty string if not set."""
424+
text_obj = self._supylabel
425+
return "" if text_obj is None else text_obj.get_text()
426+
412427
def get_edgecolor(self):
413428
"""Get the edge color of the Figure rectangle."""
414429
return self.patch.get_edgecolor()

lib/matplotlib/figure.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,11 @@ class FigureBase(Artist):
9090
def get_children(self) -> list[Artist]: ...
9191
def contains(self, mouseevent: MouseEvent) -> tuple[bool, dict[Any, Any]]: ...
9292
def suptitle(self, t: str, **kwargs) -> Text: ...
93+
def get_suptitle(self) -> str: ...
9394
def supxlabel(self, t: str, **kwargs) -> Text: ...
95+
def get_supxlabel(self) -> str: ...
9496
def supylabel(self, t: str, **kwargs) -> Text: ...
97+
def get_supylabel(self) -> str: ...
9598
def get_edgecolor(self) -> ColorType: ...
9699
def get_facecolor(self) -> ColorType: ...
97100
def get_frameon(self) -> bool: ...

lib/matplotlib/tests/test_figure.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,19 @@ def test_suptitle_subfigures():
302302
assert sf2.get_facecolor() == (1.0, 1.0, 1.0, 1.0)
303303

304304

305+
def test_get_suptitle_supxlabel_supylabel():
306+
fig, ax = plt.subplots()
307+
assert fig.get_suptitle() == ""
308+
assert fig.get_supxlabel() == ""
309+
assert fig.get_supylabel() == ""
310+
fig.suptitle('suptitle')
311+
assert fig.get_suptitle() == 'suptitle'
312+
fig.supxlabel('supxlabel')
313+
assert fig.get_supxlabel() == 'supxlabel'
314+
fig.supylabel('supylabel')
315+
assert fig.get_supylabel() == 'supylabel'
316+
317+
305318
@image_comparison(['alpha_background'],
306319
# only test png and svg. The PDF output appears correct,
307320
# but Ghostscript does not preserve the background color.

0 commit comments

Comments
 (0)