-
-
Notifications
You must be signed in to change notification settings - Fork 8k
Use set_window_title rather than set_label to set title of webagg figure #29338
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use set_window_title rather than set_label to set title of webagg figure #29338
Conversation
What's the behavior with respect to the label? I assume the label is used as a fallback if the window title is not set?
If that's correct, the behavoir should be documented, with the comment that it results in backward compatibility for previous users of set_label
, but set_window_title
is preferred over set_label
.
What's the behavior with respect to the label? I assume the label is used as a fallback if the window title is not set?
Unfortunately it is not that simple. If the window title
is not set then the default such as "Figure 1"
is used instead from here:
matplotlib/lib/matplotlib/backend_bases.py
Line 2612 in f2717a5
regardless of the backend used.
This can be overridden by passing a label
to the Figure
constructor, but any subsequent use of Figure.set_label
does not update the title
. This behaviour is surprising but it is, for good or bad, the same as for other backends such as qtagg
.
To demonstrate this use
import matplotlib as mpl mpl.use("qtagg") #mpl.use('webagg') import matplotlib.pyplot as plt # 1. Set title using set_window_title fig1, ax1 = plt.subplots(figsize=(3, 2)) fig1.canvas.manager.set_window_title('Using set_window_title') # 2. Set title using label passed to Figure constructor fig2, ax2 = plt.subplots(figsize=(3, 2), label="Label in constructor") # 3. Using Figure.set_label has no effect on the window title fig3, ax3 = plt.subplots(figsize=(3, 2)) fig3.set_label("This is not used in the title") plt.show()
and the title outputs are the same for qtagg
and webagg
here:
Screenshot 2025年01月02日 at 14 47 19
Screenshot 2025年01月02日 at 14 46 26
So this PR brings webagg
in line with other backends. Whether Figure.set_label
should also update the title (for all backends) would I think be a separate issue for discussion.
Closes #29256.
Previously when using the
webagg
backend if you wanted to change the title above a figure you would usefigure.set_label('whatever')
and if you usedfigure.canvas.manager.set_window_title('whatever')
it would be ignored. This was inconsistent with other backends which use the latter.This PR changes the behaviour so that
figure.canvas.manager.set_window_title('whatever')
is used now forwebagg
, the same as the other backends.There is no test as there isn't currently any visual testing of the
webagg
backend. There is some draft work underway in #23540 for this. So here is a demonstration instead, using this code:the
Screenshot 2024年12月17日 at 17 01 44webagg
output is:which is consistent with the output produced by e.g. the
Screenshot 2024年12月17日 at 17 02 26qtagg
backend:PR checklist
WebAgg
backend #29256 " is in the body of the PR description to link the related issue