X Tutup
Skip to content

Fix remove method for figure title and xy-labels#31075

Merged
QuLogic merged 1 commit intomatplotlib:mainfrom
rcomer:suptitle-remove
Feb 4, 2026
Merged

Fix remove method for figure title and xy-labels#31075
QuLogic merged 1 commit intomatplotlib:mainfrom
rcomer:suptitle-remove

Conversation

@rcomer
Copy link
Copy Markdown
Member

@rcomer rcomer commented Feb 4, 2026

PR summary

Fixes #31073

Currently the remove method for suptitle and supx/ylabel is set within fig.text and only removes them from the list of texts. We need to additionally reset the attributes to None, consistent with their instantiation.

I have run the code from the issue and the output is consistent with having never set the suptitle.

PR checklist

@rcomer rcomer added this to the v3.10.9 milestone Feb 4, 2026
@rcomer rcomer added the PR: bugfix Pull requests that fix identified bugs label Feb 4, 2026
@rcomer rcomer marked this pull request as draft February 4, 2026 10:49
@rcomer
Copy link
Copy Markdown
Member Author

rcomer commented Feb 4, 2026

I broke the pickling. I know very little about pickling so will need some guidance here.

Or anyone feel free to PR an alternative solution.

@QuLogic
Copy link
Copy Markdown
Member

QuLogic commented Feb 4, 2026

Oops, I wrote almost the same thing, but it also suffers from pickling problems. It looks like that can be fixed by using a partial method:

diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py
index 56634b2f3d..df347f876d 100644
--- a/lib/matplotlib/figure.py
+++ b/lib/matplotlib/figure.py
@@ -376,17 +376,16 @@ default: %(va)s
         else:
             suplab = self.text(x, y, t, **kwargs)
             setattr(self, info['name'], suplab)
-            orig_remove_method = suplab._remove_method
-
-            # Ensure sublabel is properly removed from Figure internals.
-            def _remove(label):
-                orig_remove_method(label)
-                setattr(self, info['name'], None)
-            suplab._remove_method = _remove
+            suplab._remove_method = functools.partial(self._remove_suplabel,
+                                                      name=info['name'])
         suplab._autopos = autopos
         self.stale = True
         return suplab
 
+    def _remove_suplabel(self, label, name):
+        self.texts.remove(label)
+        setattr(self, name, None)
+
     @_docstring.Substitution(x0=0.5, y0=0.98, name='super title', ha='center',
                              va='top', rc='title')
     @_docstring.copy(_suplabels)

Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
@rcomer
Copy link
Copy Markdown
Member Author

rcomer commented Feb 4, 2026

Circle-CI emailed me about successful builds on this PR. Never seen that before.

@rcomer rcomer marked this pull request as ready for review February 4, 2026 11:41
@timhoffm
Copy link
Copy Markdown
Member

timhoffm commented Feb 4, 2026

Circle-CI emailed me about successful builds on this PR. Never seen that before.

Also got some yesterday. This seems to be a new "feature".

@jklymak
Copy link
Copy Markdown
Member

jklymak commented Feb 4, 2026

maybe @QuLogic could merge, but if not in the next day or so, anyone including @rcomer can merge

@QuLogic QuLogic merged commit 9abdac2 into matplotlib:main Feb 4, 2026
38 of 40 checks passed
meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Feb 4, 2026
@rcomer rcomer deleted the suptitle-remove branch February 4, 2026 21:35
@QuLogic
Copy link
Copy Markdown
Member

QuLogic commented Feb 4, 2026

Circle-CI emailed me about successful builds on this PR. Never seen that before.

There seems to be new settings for your own work notifications, which defaults to on:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR: bugfix Pull requests that fix identified bugs topic: figures and subfigures

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Crash when Removing Suptitle in a Figure with Constrained Layout

4 participants

X Tutup