Skip to content

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

Merged

Conversation

ianthomas23
Copy link
Member

Closes #29256.

Previously when using the webagg backend if you wanted to change the title above a figure you would use figure.set_label('whatever') and if you used figure.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 for webagg, 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:

import matplotlib as mpl
mpl.use('webagg')
import matplotlib.pyplot as plt

fig1, ax1 = plt.subplots(figsize=(3, 2))
ax1.plot([1, 3, 2, 4])
fig1.canvas.manager.set_window_title('My line plot')

fig2, ax2 = plt.subplots(figsize=(3, 2))
ax2.contourf([[1, 2], [3, 4]])
fig2.canvas.manager.set_window_title('My contour plot')

fig3, ax3 = plt.subplots(figsize=(3, 2))
ax3.bar(x=[0, 1], height=[1, 2])

plt.show()

the webagg output is:

Screenshot 2024-12-17 at 17 01 44

which is consistent with the output produced by e.g. the qtagg backend:

Screenshot 2024-12-17 at 17 02 26

PR checklist

@timhoffm
Copy link
Member

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.

@ianthomas23
Copy link
Member Author

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:

self.set_window_title(f"Figure {num:d}")

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.

@timhoffm timhoffm added this to the v3.11.0 milestone Jan 7, 2025
@timhoffm timhoffm merged commit 02263ba into matplotlib:main Jan 7, 2025
35 of 38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: canvas.manager.set_window_title does not work to figure with WebAgg backend
3 participants