Bug summary
After a WebAgg figure has been loaded on a display with a device pixel ratio not equal to 1, it permanently renders incorrectly on page load on displays with device pixel ratio equal to 1.
Code for reproduction
import matplotlib
import matplotlib.pyplot as plt
matplotlib.use("WebAgg")
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [4, 5, 6])
ax.set_title("Test Plot")
plt.show(block=True)
Actual outcome
- Open a WebAgg figure in a browser on a display with a device pixel ratio of 1 (this renders correctly)
- Move to a display with a device pixel ratio of 2 and refresh (this renders correctly)
- Move back to the original display and refresh (this breaks). Only the top-left quadrant of the figure renders and can only be fixed by restarting the server.
Final screenshot:

Expected outcome
I'd expect the final step to reset the device pixel ratio to 1 and render the figure at a correct size.
Additional information
In mpl.js, there is a condition that skips setting the device pixel ratio when it's equal to 1. This condition serves no functional purpose as far as I can tell, so I think it should be removed (happy to submit a PR if whoever reads this agrees). :)
Relevant line of code: https://github.com/matplotlib/matplotlib/blob/main/lib/matplotlib/backends/web_backend/js/mpl.js#L67
Operating system
MacOS
Matplotlib Version
3.10.8
Matplotlib Backend
WebAgg
Python version
3.13.14
Jupyter version
No response
Installation
pixi
Bug summary
After a WebAgg figure has been loaded on a display with a device pixel ratio not equal to 1, it permanently renders incorrectly on page load on displays with device pixel ratio equal to 1.
Code for reproduction
Actual outcome
Final screenshot:

Expected outcome
I'd expect the final step to reset the device pixel ratio to 1 and render the figure at a correct size.
Additional information
In
mpl.js, there is a condition that skips setting the device pixel ratio when it's equal to 1. This condition serves no functional purpose as far as I can tell, so I think it should be removed (happy to submit a PR if whoever reads this agrees). :)Relevant line of code: https://github.com/matplotlib/matplotlib/blob/main/lib/matplotlib/backends/web_backend/js/mpl.js#L67
Operating system
MacOS
Matplotlib Version
3.10.8
Matplotlib Backend
WebAgg
Python version
3.13.14
Jupyter version
No response
Installation
pixi