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
{{ message }}
Repository navigation
PGF backend does not clip image with specific bounding box #2586
When setting the bbox_inches in savefig() to a specific size the resulting pgf file sets the bounding box to the appropriate dimensions but the image is not clipped. It seems that clip option should be added to the \pgfusepath command.
Current commands cut from top of pgf file:
\begingroup%
\makeatletter%
\begin{pgfpicture}%
\pgfpathrectangle{\pgfpointorigin}{\pgfqpoint{2.281265in}{2.224689in}}%
\pgfusepath{use as bounding box}%
Correct \pgfusepath command?:
\pgfusepath{use as bounding box,clip}%
I looked at the pgf file and see the use of \pgfusepath{clip} throughout deeper in the file however it doesn't clip the image. Adding clip as I did above gives the correct behavior in my latex document.
i created the pgf with a python command like:
extent = ax.get_window_extent().transformed(fig.dpi_scale_trans.inverted())
fig.savefig('test.pgf', bbox_inches=extent)
Are you saying that parts of the figure are drawn outside the bounding box when you include the PGF code in LaTeX? Are you having any trouble with PDF produced by the PGF backend? Can you post some example code that illustrates the problem you are having?
I just tried savefig() sending it to a pdf file and it has the same issue. The bounding box looks to be positioned correctly though. Incidentally, if I run this in ipython notebook, the pdf works although the notebook might be using a different backend for pdf, although I'm not sure.
import numpy as np
import matplotlib as mpl
mpl.use('pgf')
import matplotlib.pyplot as plt
Hm, I think the actual problem is that I never implemented support for user defined extents in bbox_inches :). I did some changes for making bbox_inches='tight' work, but that seems to be a completely different mechanism.
Actually, the output isn't completely wrong. As you said, the size of the bounding box is right, the point of origin seems to be good, it appears to be some problem with the scaling. I had some problems with hardcoded dpi values in matplotlib earlier, maybe this is yet another of these cases.
Yep, set fig.savefig('test.pdf', bbox_inches=extent1, dpi=72.) as a workaround when working with the pgf backend. I'll try to find the hardcoded dpi value.
Yup, that worked for the pdf, but not the pgf as you mentioned.
If the first call of
\pgfusepath{use as bounding box}%
is changed to
\pgfusepath{use as bounding box, clip}%
then the pgf file works in my latex document. Looks like this happens at line 794 of backend_pgf.py
Ah ok, so in addition to the scaling error, "use as bounding box" doesn't seem to prevent pgf from drawing outside the boundaries. I'll include this in the fix for this issue.
Activity
pwuertz commentedon Nov 8, 2013
Are you saying that parts of the figure are drawn outside the bounding box when you include the PGF code in LaTeX? Are you having any trouble with PDF produced by the PGF backend? Can you post some example code that illustrates the problem you are having?
gibbycu commentedon Nov 8, 2013
I just tried savefig() sending it to a pdf file and it has the same issue. The bounding box looks to be positioned correctly though. Incidentally, if I run this in ipython notebook, the pdf works although the notebook might be using a different backend for pdf, although I'm not sure.
import numpy as np
import matplotlib as mpl
mpl.use('pgf')
import matplotlib.pyplot as plt
fig = plt.figure(1)
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
plt.tight_layout()
extent1 = ax1.get_window_extent().transformed(fig.dpi_scale_trans.inverted())
fig.savefig('test.pgf', bbox_inches=extent1)
fig.savefig('test.pdf', bbox_inches=extent1)
plt.show()
gibbycu commentedon Nov 8, 2013
Sorry about closed/open I'm new to github
pwuertz commentedon Nov 8, 2013
Hm, I think the actual problem is that I never implemented support for user defined extents in
bbox_inches:). I did some changes for makingbbox_inches='tight'work, but that seems to be a completely different mechanism.pwuertz commentedon Nov 8, 2013
Actually, the output isn't completely wrong. As you said, the size of the bounding box is right, the point of origin seems to be good, it appears to be some problem with the scaling. I had some problems with hardcoded dpi values in matplotlib earlier, maybe this is yet another of these cases.
pwuertz commentedon Nov 8, 2013
Yep, set
fig.savefig('test.pdf', bbox_inches=extent1, dpi=72.)as a workaround when working with the pgf backend. I'll try to find the hardcoded dpi value.gibbycu commentedon Nov 8, 2013
Yup, that worked for the pdf, but not the pgf as you mentioned.
If the first call of
\pgfusepath{use as bounding box}%
is changed to
\pgfusepath{use as bounding box, clip}%
then the pgf file works in my latex document. Looks like this happens at line 794 of backend_pgf.py
pwuertz commentedon Nov 9, 2013
Ah ok, so in addition to the scaling error, "use as bounding box" doesn't seem to prevent pgf from drawing outside the boundaries. I'll include this in the fix for this issue.