-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[Bug]: Rendering inconsistency between matplotlib-inline plt.show()
and fig.savefig(...)
#24281
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
Comments
It looks like the widget is doing the equivalent of |
Why would that yield a different result from, say |
@jklymak, how would one go about getting similar results for both |
Because
To get similar results, you pass |
plt.show()
and fig.savefig(...)
plt.show()
and fig.savefig(...)
ipywidgets uses the inline backend to display plots. That defaults to a tight bbox. It can be disabled by overriding their save options. Going to close as not our bug. |
Thanks for the insight! That actually led me to finding jupyter/notebook#2640, including jupyter/notebook#2640 (comment) , which offers a way to bring parity. UPDATE: Hah! Beat me to it, @QuLogic! |
To be very clear here, there are a bunch of moving parts:
that have become a bit tangled. I just want to make sure we are not talking past each other. The inline backend (which used to be part of IPython and is now its own package) partially replicates the desktop interactive backends by dropping a static png into the output cells of the notebook when it would have normally poped up a GUI window. The png is embeded in the notebook as a data URL with the base64 encoded binary and the png data is rendered using (effectively) A minimal example to reproduce this behavior: import matplotlib.pyplot
from fractions import Fraction
import matplotlib.pyplot as plt
data = {
3: Fraction(1, 216),
4: Fraction(1, 72),
5: Fraction(1, 36),
6: Fraction(5, 108),
7: Fraction(5, 72),
8: Fraction(7, 72),
9: Fraction(25, 216),
10: Fraction(1, 8),
11: Fraction(1, 8),
12: Fraction(25, 216),
13: Fraction(7, 72),
14: Fraction(5, 72),
15: Fraction(5, 108),
16: Fraction(1, 36),
17: Fraction(1, 72),
18: Fraction(1, 216),
}
fig, ax = matplotlib.pyplot.subplots(figsize=(4, 4))
unique_outcomes = sorted(data.keys())
ax.set_title(
"Super duper duper long title that will probably exceed the width of the pie chart",
fontdict={
"fontweight": "bold",
},
)
outcomes, values = zip(*((outcome, float(value)) for outcome, value in data.items()))
ax.pie(outcomes, values)
# this will clip the title, but will be the size you asked for
fig.savefig('test1.png')
# this will include the full title, but it is bigger than you asked for and not square
fig.savefig('test2.png', bbox_inches='tight') There is no chance that we will change the default behavior of I think you option are either to pass
will work interactively and there is a way to put this in your profile configuration files). I took the liberty of updating the title to include matplotlib-inline. |
Thanks @tacaswell! That is a great breakdown and very helpful not only for me in understanding the layers, but hopefully for folks developing JupyterLite or anyone else who stumbles across this issue. I really appreciate you taking the time to lay it all out in such a way that even a stranger to these technologies (like me) can easily understand. It am especially grateful for the layout of possible options and explanations of why some were off the table. I feel much more aware of things now. Many, many thanks again! 🙇 |
@posita Your welcome! |
Uh oh!
There was an error while loading. Please reload this page.
Bug summary
This originally came up in jupyterlite/jupyterlite#850, but I'm pretty sure the root cause is here. I'll reproduce the most helpful comment from that thread here. The following is a pretty good demonstration of how
savefig
clips (e.g.) the title of a pie chart.Possibly related issues:
Code for reproduction
Run the following in Jupyter Lab to reproduce the attached images.
Actual outcome
subplots
add_axes
show()
savefig(..., format="png")
savefig(..., format="svg")
Expected outcome
I expected all backends to produce at least similar results (i.e., not ones where titles were clipped or missing entirely).
Additional information
No response
Operating system
No response
Matplotlib Version
3.5.2, 3.6.1
Matplotlib Backend
module://matplotlib_inline.backend_inline
Python version
3.10.6
Jupyter version
Lab 3.4.8
Installation
No response
The text was updated successfully, but these errors were encountered: