Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

PGF back-end fails on simple graph #1128

Closed
jholewinski opened this Issue · 6 comments

3 participants

Justin Holewinski Jens Hedegaard Nielsen Peter Würtz
Justin Holewinski

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.

Peter Würtz pwuertz was assigned
Jens Hedegaard Nielsen

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

Peter Würtz
Collaborator

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

Peter Würtz
Collaborator

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

Justin Holewinski

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.

Peter Würtz
Collaborator

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.

Peter Würtz
Collaborator

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.

Peter Würtz pwuertz closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.