PGF back-end fails on simple graph #1128

jholewinski opened this Issue Aug 21, 2012 · 6 comments

3 participants


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 
Traceback (most recent call last):
  File "", 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/", 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/", line 2048, in print_figure
  File "/home/jholewinski/projects/matplotlib/matplotlib-pgf/build/lib.linux-x86_64-2.7/matplotlib/", 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/", line 664, in print_pgf
    with, "w", encoding="utf-8") as fh:
  File "/usr/lib/python2.7/", line 881, in open
    file =, 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')


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

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

@pwuertz pwuertz was assigned Aug 21, 2012
Matplotlib Developers 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


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


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


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.


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.


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 Aug 21, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment