diff --git a/doc/users/next_whats_new/get_suptitle.rst b/doc/users/next_whats_new/get_suptitle.rst new file mode 100644 index 000000000000..b03ad10b1b4c --- /dev/null +++ b/doc/users/next_whats_new/get_suptitle.rst @@ -0,0 +1,4 @@ +``Figure.get_suptitle()``, ``Figure.get_supxlabel()``, ``Figure.get_supylabel()`` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +These methods return the strings set by ``Figure.suptitle()``, ``Figure.supxlabel()`` +and ``Figure.supylabel()`` respectively. diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index 7fdcc8cb6627..970bf957d4bf 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -388,6 +388,11 @@ def suptitle(self, t, **kwargs): 'size': 'figure.titlesize', 'weight': 'figure.titleweight'} return self._suplabels(t, info, **kwargs) + def get_suptitle(self): + """Return the suptitle as string or an empty string if not set.""" + text_obj = self._suptitle + return "" if text_obj is None else text_obj.get_text() + @_docstring.Substitution(x0=0.5, y0=0.01, name='supxlabel', ha='center', va='bottom', rc='label') @_docstring.copy(_suplabels) @@ -398,6 +403,11 @@ def supxlabel(self, t, **kwargs): 'size': 'figure.labelsize', 'weight': 'figure.labelweight'} return self._suplabels(t, info, **kwargs) + def get_supxlabel(self): + """Return the supxlabel as string or an empty string if not set.""" + text_obj = self._supxlabel + return "" if text_obj is None else text_obj.get_text() + @_docstring.Substitution(x0=0.02, y0=0.5, name='supylabel', ha='left', va='center', rc='label') @_docstring.copy(_suplabels) @@ -409,6 +419,11 @@ def supylabel(self, t, **kwargs): 'weight': 'figure.labelweight'} return self._suplabels(t, info, **kwargs) + def get_supylabel(self): + """Return the supylabel as string or an empty string if not set.""" + text_obj = self._supylabel + return "" if text_obj is None else text_obj.get_text() + def get_edgecolor(self): """Get the edge color of the Figure rectangle.""" return self.patch.get_edgecolor() diff --git a/lib/matplotlib/figure.pyi b/lib/matplotlib/figure.pyi index ee21892f32ac..f4c31506a2e1 100644 --- a/lib/matplotlib/figure.pyi +++ b/lib/matplotlib/figure.pyi @@ -90,8 +90,11 @@ class FigureBase(Artist): def get_children(self) -> list[Artist]: ... def contains(self, mouseevent: MouseEvent) -> tuple[bool, dict[Any, Any]]: ... def suptitle(self, t: str, **kwargs) -> Text: ... + def get_suptitle(self) -> str: ... def supxlabel(self, t: str, **kwargs) -> Text: ... + def get_supxlabel(self) -> str: ... def supylabel(self, t: str, **kwargs) -> Text: ... + def get_supylabel(self) -> str: ... def get_edgecolor(self) -> ColorType: ... def get_facecolor(self) -> ColorType: ... def get_frameon(self) -> bool: ... diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index 4188ca878fed..d8f137ddd61a 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -302,6 +302,19 @@ def test_suptitle_subfigures(): assert sf2.get_facecolor() == (1.0, 1.0, 1.0, 1.0) +def test_get_suptitle_supxlabel_supylabel(): + fig, ax = plt.subplots() + assert fig.get_suptitle() == "" + assert fig.get_supxlabel() == "" + assert fig.get_supylabel() == "" + fig.suptitle('suptitle') + assert fig.get_suptitle() == 'suptitle' + fig.supxlabel('supxlabel') + assert fig.get_supxlabel() == 'supxlabel' + fig.supylabel('supylabel') + assert fig.get_supylabel() == 'supylabel' + + @image_comparison(['alpha_background'], # only test png and svg. The PDF output appears correct, # but Ghostscript does not preserve the background color.