Repository navigation
Closed as not planned
Closed as not planned
Copy link
Labels
status: needs clarificationIssues that need more information to resolve.Issues that need more information to resolve.
Description
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:
- Feature request: savefig with separated pad_inches in each direction #11764
- [Bug]: figimage producing inconsistent results #23399
- [Bug]: Inconsistent rendering between backends when rendering Mathtext horizontal rule #23763
Code for reproduction
Run the following in Jupyter Lab to reproduce the attached images.
import matplotlib.pyplot
import base64, io
from enum import Enum
from ipywidgets import widgets
from fractions import Fraction
from itertools import chain
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)}
class Format(str, Enum):
NATIVE = "native"
PNG = "png"
SVG = "svg"
class PngImage:
def __init__(self, data):
self.data = data
def _repr_png_(self):
return base64.b64encode(self.data).decode()
class SvgImage:
def __init__(self, data):
self.data = data
def _repr_svg_(self):
return self.data.decode()
@widgets.interact
def show(output_to_format = Format, use_subplots: bool = True):
with matplotlib.style.context("bmh"):
if use_subplots:
_, ax = matplotlib.pyplot.subplots()
else:
fig = matplotlib.pyplot.figure()
xmin, ymin, dx, dy = 0.1, 0.1, 0.9, 0.9
ax = fig.add_axes((xmin, ymin, dx, dy))
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)
if output_to_format is Format.NATIVE:
matplotlib.pyplot.show()
else:
buf = io.BytesIO()
matplotlib.pyplot.savefig(buf, format=output_to_format)
if output_to_format is Format.PNG:
display(PngImage(buf.getvalue()))
elif output_to_format is Format.SVG:
display(SvgImage(buf.getvalue()))
else:
assert False, "should never be here"
matplotlib.pyplot.clf()
print(matplotlib.__version__)Actual outcome
| Output method | Using subplots |
Using 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
Metadata
Metadata
Assignees
Labels
status: needs clarificationIssues that need more information to resolve.Issues that need more information to resolve.




Activity
jklymak commentedon Oct 26, 2022
It looks like the widget is doing the equivalent of
matplotlib.pyplot.savefig(buf, format=output_to_format, bbox_inches='tight')which will clip the figure to whatever artists are in it. That is the choice the inline backend makes as well. I don't think this is a bug, or actionable on our part.posita commentedon Oct 26, 2022
Why would that yield a different result from, say
matplotlib.pyplot.tight_layout() ; matplotlib.pyplot.show()?posita commentedon Oct 26, 2022
@jklymak, how would one go about getting similar results for both
matplotlib.pyplot.show()andmatplotlib.pyplot.savefig(...)then? Or are you saying this is anipywidgetsissue and should be filed against that project?jklymak commentedon Oct 26, 2022
Because
bbox_inches='tight'clips the figure to the artists as draw,plt.tight_layoutadjusts the size of the axes so the artists fit in the figure, but doesn't change the size of the figure.To get similar results, you pass
bbox_inches='tight'tosavefig. I think there is also a configuration option to change ipython to not dobbox_inches='tight', but forget what it is.[-][Bug]: Rendering inconsistency between `plt.show()` and `fig.savefig(...)`[/-][+][Bug]: Rendering inconsistency between matplotlib-inline `plt.show()` and `fig.savefig(...)`[/+]QuLogic commentedon Oct 26, 2022
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.
posita commentedon Oct 26, 2022
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!
tacaswell commentedon Oct 26, 2022
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)
fig.savefig(buf, bbox_inches='tight'). The use ofbbox_inches='tight'is the key detail as it clips the figure size to exactly fit the artists in the Figure. The somewhat surprising behavior of this clipping is that in addition to making the Figure smaller (trimming whitespace), it also can make the figure effectively bigger.A minimal example to reproduce this behavior:
There is no chance that we will change the default behavior of
savefigto do this resizing (targeting fixed-sized figures is a major part of what we do) and I think it is also far too late for matplotlib-inline to change their default (the wave of bug reports of people who implicitly depended on this would be huge!).I think you option are either to pass
bbox_inches='tight'when you save or configure the inline backend to not pass it (I thinkwill work interactively and there is a way to put this in your profile configuration files).
https://stackoverflow.com/questions/37864735/matplotlib-and-ipython-notebook-displaying-exactly-the-figure-that-will-be-save
I took the liberty of updating the title to include matplotlib-inline.
posita commentedon Oct 29, 2022
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! 🙇
tacaswell commentedon Oct 29, 2022
@posita Your welcome!