Skip to content
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

PGF backend does not clip image with specific bounding box #2586

Closed
gibbycu opened this issue Nov 8, 2013 · 8 comments
Closed

PGF backend does not clip image with specific bounding box #2586

gibbycu opened this issue Nov 8, 2013 · 8 comments
Assignees

Comments

@gibbycu
Copy link

gibbycu commented Nov 8, 2013

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)

@ghost ghost assigned pwuertz Nov 8, 2013
@pwuertz
Copy link
Contributor

pwuertz commented 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
Copy link
Author

gibbycu commented 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 gibbycu closed this as completed Nov 8, 2013
@gibbycu gibbycu reopened this Nov 8, 2013
@gibbycu
Copy link
Author

gibbycu commented Nov 8, 2013

Sorry about closed/open I'm new to github

@pwuertz
Copy link
Contributor

pwuertz commented 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 making bbox_inches='tight' work, but that seems to be a completely different mechanism.

@pwuertz
Copy link
Contributor

pwuertz commented 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
Copy link
Contributor

pwuertz commented 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
Copy link
Author

gibbycu commented 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
Copy link
Contributor

pwuertz commented 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants