Skip to content

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

Closed

Description

@gibbycu

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)

Activity

  1. ghost assigned on Nov 8, 2013
  2. pwuertz commented on Nov 8, 2013

    @pwuertz
    Contributor

    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?

  3. gibbycu commented on Nov 8, 2013

    @gibbycu
    Author

    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()

  4. reopened this on Nov 8, 2013
  5. gibbycu commented on Nov 8, 2013

    @gibbycu
    Author

    Sorry about closed/open I'm new to github

  6. pwuertz commented on Nov 8, 2013

    @pwuertz
    Contributor

    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.

  7. pwuertz commented on Nov 8, 2013

    @pwuertz
    Contributor

    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.

  8. pwuertz commented on Nov 8, 2013

    @pwuertz
    Contributor

    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.

  9. gibbycu commented on Nov 8, 2013

    @gibbycu
    Author

    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

  10. pwuertz commented on Nov 9, 2013

    @pwuertz
    Contributor

    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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions