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 back-end fails on simple graph #1128

Closed
jholewinski opened this issue Aug 21, 2012 · 6 comments
Closed

PGF back-end fails on simple graph #1128

jholewinski opened this issue Aug 21, 2012 · 6 comments
Assignees
Milestone

Comments

@jholewinski
Copy link

I'm not sure if this is related to the other Unicode/codec issue, but I'm trying out the pgf backend and I'm seeing what appears to be an issue with Unicode/codecs using matplotlib git master and Python 2.7 (Ubuntu 12.04):

jholewinski@rapture [tests]$ python t1.py 
Traceback (most recent call last):
  File "t1.py", line 14, in <module>
    fig.savefig('ti.pgf', bbox_inches='tight')
  File "/home/jholewinski/projects/matplotlib/matplotlib-pgf/build/lib.linux-x86_64-2.7/matplotlib/figure.py", line 1262, in savefig
    self.canvas.print_figure(*args, **kwargs)
  File "/home/jholewinski/projects/matplotlib/matplotlib-pgf/build/lib.linux-x86_64-2.7/matplotlib/backend_bases.py", line 2048, in print_figure
    **kwargs)
  File "/home/jholewinski/projects/matplotlib/matplotlib-pgf/build/lib.linux-x86_64-2.7/matplotlib/backend_bases.py", line 1848, in print_pgf
    return pgf.print_pgf(*args, **kwargs)
  File "/home/jholewinski/projects/matplotlib/matplotlib-pgf/build/lib.linux-x86_64-2.7/matplotlib/backends/backend_pgf.py", line 664, in print_pgf
    with codecs.open(filename, "w", encoding="utf-8") as fh:
  File "/usr/lib/python2.7/codecs.py", line 881, in open
    file = __builtin__.open(filename, mode, buffering)
TypeError: coercing to Unicode: need string or buffer, _io.BytesIO found

The graph script is very basic:

import matplotlib as mpl
import matplotlib.pyplot as plt

fig = plt.figure()

sub = fig.add_subplot(1, 1, 1)

sub.plot([1, 2, 3], [2, 3, 4], label='Foo', markersize=2, marker='x')

sub.grid()
sub.set_axisbelow(True)
sub.legend(loc=2)

fig.savefig('t1.pgf', bbox_inches='tight')

I just built the latest git master with "python setup.py build" and used it directly from the build/ directory. Interestingly, the tests included with matplotlib do seem to generate graphs.

@ghost ghost assigned pwuertz Aug 21, 2012
@jenshnielsen
Copy link
Member

I can confirm this. It seems to be related to the use of bbox_inches='tight'. Removing this and the plot works as expected

@pwuertz
Copy link
Contributor

pwuertz commented Aug 21, 2012

If you add the bbox_inches='tight' keyword, the filename given to the backend isn't a string but a _io.BytesIO object that the backend apparently is supposed to write to.

Sorry, but when I implemented the method I thought filename refers to the name of a file.. going to fix this..

@pwuertz
Copy link
Contributor

pwuertz commented Aug 21, 2012

This should be fixed in the latest commit in #1124

Saving the figure prints a warning that the bbox_inches option is not implemented, which is true because I have no idea what that option is and haven't seen any remark about it in backend_template :). Tight_layout however works nicely if that is an option for you. I'll try to figure out what bbox_inches does..

@jholewinski
Copy link
Author

Thanks for the fix. bbox_inches isn't too important for me with the pgf backend since the result will be embedded directly into TeX files. For other formats like PDF, it gets rid of extraneous white-space around the figure.

@pwuertz
Copy link
Contributor

pwuertz commented Aug 21, 2012

Ok apparently bbox_inches='tight' causes the figure to be printed to an in-memory buffer twice instead of saving to a file. The first run is for figuring out experimentally what the figure size is. You can use pyplot.tight_layout to do the opposite, changing the layout of the figure so it exactly uses the size you defined. For pixel perfect alignment you need to globally switch (pyplot.switch_backend or mpl.use) to the backend you are using for savefig until a better solution is found.

@pwuertz
Copy link
Contributor

pwuertz commented Aug 21, 2012

Ahh help, I did a terrible mistake. Since PGF is a text file, images that occur in figures must be saved to additional files, in the same directory, based on the same filename. print_pgf cannot support saving to buffers or streams. The pdf and png options should be fine however.
Luckily bbox_inches announces its use of the print function with the dryrun kwarg, so the latest fix just ignores the save at all instead of raising an exception.

@pwuertz pwuertz closed this as completed Aug 21, 2012
mdboom added a commit that referenced this issue Aug 22, 2012
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

3 participants