-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
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
[Bug]: Matplotlib don't take into account savefig.pad_inches when plt.plot(... transform=fig.dpi_scale_trans) #29224
Comments
This is because you are using a notebook and the inline backend is expanding the figure to fit around the line with the padding you found added on. See https://matplotlib.org/devdocs/users/explain/figure/figure_intro.html#notebooks-and-ides. I guess If instead you save to a file with
you get Note that, unlike your "expected outcome" above, here the line goes right to the corners. |
@rcomer, thanks for your answer! I tested a lot and found that matplotlib inline uses fig = plt.figure(facecolor='#ccc')
ax = fig.gca()
ax.set_axis_off()
line_1, = plt.plot([0, 1], [0, 1], transform=fig.transFigure, clip_on=False, lw=2, c='blue')
ofs = 0
# uncomment line below for workaround:
# ofs = plt.rcParams['savefig.pad_inches']
fig_w, fig_h = fig.get_size_inches()
line_2, = plt.plot([0+ofs,fig_w+ofs], [0+ofs,fig_h+ofs], transform=fig.dpi_scale_trans, clip_on=False, lw=2, c='black')
#plt.show()
plt.savefig('test.png', bbox_inches='tight') And test.png now is: So now I suggest this is definitely is a Matplotlib bug. |
Please see the pad_inches parameter of savefig. https://matplotlib.org/stable/api/_as_gen/matplotlib.figure.Figure.savefig.html#matplotlib.figure.Figure.savefig |
@jklymak, sorry, I don't know how this should resolve this problem. Please explain this in more detail if you could. I already use these pads in my code as a workaround. But I wonder, why fig.transFigure don't require to shift plotted line. |
Because bboxinches=tight changes the size of the figure and adds a pad around the visible artists. The line in figure space then gets stretched. |
The problem is that you are asking for two mutually exclusive things: the |
@rcomer, I'm just wondering now, why this default pad make |
@jklymak, Okay, but why is only one of the lines displaced relative to the lower left corner? I couldn't find anything about this in the Matplotlib documentation. |
@sindzicat if you are really keen, the code that makes the adjustment for |
@rcomer, many thanks! I tried to find this myself, but without success. I found a way to make code work: ax.plot([0, 6.4], [0, 4.8], transform=(fig.dpi_scale_trans+mpl.transforms.ScaledTranslation(0,0,fig.transFigure)), clip_on=False, c='blue') I've added |
In my opinion, if you want to mess with transfigure and trans_fig_dpi you should avoid using bbox_inches="tight" which changes the size of the figure on you. |
I should add that it's iPython that makes the inline backend use |
Bug summary
Hello! When I draw line from bottom left figure corner up to top right figure corner, I see then that there some figure paddings:
fig.get_size_inches()
gives me 6.4×4.8When I use
plt.plot
withtransform=fig.dpi_scale_trans
, I see that line starts from the left bottom corner:plt.rcParams['savefig.pad_inches']
gives me 0.1. When I add 0.1 to line_2 coordinates, line_2 will be placed the same as line_1:It seems to me this is a bug.
Code for reproduction
Actual outcome
Expected outcome
Additional information
No response
Operating system
Windows 10
Matplotlib Version
3.9.3
Matplotlib Backend
inline
Python version
3.12.5
Jupyter version
ms-toolsai.jupyter v2024.10.0
Installation
pip
The text was updated successfully, but these errors were encountered: