You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It's probably an issue that almost everybody who uses matplotlib would have encountered. If you generate a figure - which often contains axis labels and legends - and save it with default settings, you'll get a cropped image.
If I generate the same plot in a jupyter notebook, without using tight_layout option, I see that all the elements of the plot are contained within the figure boundaries (shown in red).
This figure is generated in the output cell of a jupyter notebook(!).
Then why it is not saved as it is? Why the saved image is by default different from the image jupyter notebook?
In my opinion this is a very fundamental issue with matplotlib.
Would't it make the lives of the users easier, if by default, all the elements are contained in the saved figure without the need of any workarounds?
(Thanks to stackoverflow), we know few workarounds but each has a caveat of its own..
Workaround #1: from within matplotlib: use of tight_layout option.
It works for simple figures. However, in my experience, it is not reliable option in the case of more complex, multi-panel figures. tight_layout often fails with errors such as these:
UserWarning: This figure includes Axes that are not compatible with tight_layout, so results might be incorrect.
UserWarning: tight_layout not applied: number of rows in subplot specifications must be multiples of one another.
Workaround #2: from outside of matplotlib: save the image in SVG format and then convert to png. For example using --export-area-drawing option in the inkscape command line UI or "resize to page" option in the inkscape's GUI.
However, in this case you have to depend on external softwares which are difficult to add as dependencies in python packages (currently conda only hosts Windows version of inkscape).
So my question is..
Why doesn't matplotlib save the whole figure by default?
Matplotlib version
Operating system: Ubuntu 18.04
Matplotlib version: 3.1.2 (this is issue reproducible in previous version too.)
Matplot does save "the whole figure". The concept is, like in a lot of other software, to define a figure like an empty sheet of fixed size. If elements that are later added overflow the borders of the sheet, it they will be cropped.
The reason this is useful is that the output will be deterministic. If you create a matplotlib figure, e.g. via plt.figure(figsize=(5,4)), you can be sure that is has 5 by 4 inches in size.
For the common case of elements overflowing, matplotlib provides 4 ways to handle those:
Manually make sure that everything fits onto the figure. E.g. adjust the subplot parameters with fig.subplots_adjust
Use fig.tight_layout() to let the subplot parameters be adjusted automatically.
Use constrained_layout to let an algorithm find the best positions of the elements within the figure (in most cases this will give similar results as tight_layout).
Use bbox_inches='tight' argument to savefig - this option is only available when saving the figure - to let the figure expand (or crop) to contain all the artists within. This is the only of those options that actually changes the figure size.
Notes:
Option No. 4 is what you observe in the jupyter notebook. Because you are using IPykernel's inline backend, the figure you get in the output cell is produced with the bbox_inches='tight' option.
`tight_layout* as well as constrained_layout can be set via rcParams. So if you find yourself in the situation that you always want to apply those, you can do so by changing your rc file.
savefig
saves cropped images by default.Bug summary
It's probably an issue that almost everybody who uses matplotlib would have encountered. If you generate a figure - which often contains axis labels and legends - and save it with default settings, you'll get a cropped image.
It's a duplicate of my question on stackoverflow.
Code for reproduction
Actual outcome
Expected outcome
If I generate the same plot in a jupyter notebook, without using
tight_layout
option, I see that all the elements of the plot are contained within the figure boundaries (shown in red).This figure is generated in the output cell of a jupyter notebook(!).
Then why it is not saved as it is? Why the saved image is by default different from the image jupyter notebook?
In my opinion this is a very fundamental issue with matplotlib.
Would't it make the lives of the users easier, if by default, all the elements are contained in the saved figure without the need of any workarounds?
(Thanks to stackoverflow), we know few workarounds but each has a caveat of its own..
Workaround #1: from within matplotlib: use of
tight_layout
option.It works for simple figures.
However, in my experience, it is not reliable option in the case of more complex, multi-panel figures.
tight_layout
often fails with errors such as these:Workaround #2: from outside of matplotlib: save the image in SVG format and then convert to png. For example using
--export-area-drawing
option in the inkscape command line UI or "resize to page" option in the inkscape's GUI.However, in this case you have to depend on external softwares which are difficult to add as dependencies in python packages (currently conda only hosts Windows version of inkscape).
So my question is..
Why doesn't matplotlib save the whole figure by default?
Matplotlib version
print(matplotlib.get_backend())
):The text was updated successfully, but these errors were encountered: