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

savefig PGF - RuntimeError: Cannot get window extent w/o renderer #2342

Closed
gozzilli opened this issue Aug 24, 2013 · 2 comments · Fixed by #2391
Closed

savefig PGF - RuntimeError: Cannot get window extent w/o renderer #2342

gozzilli opened this issue Aug 24, 2013 · 2 comments · Fixed by #2391
Assignees

Comments

@gozzilli
Copy link

I'm trying to save a figure in pyplot with tight margins.

The following code works perfectly with a PDF output:

from matplotlib import pyplot as plt
plt.plot(1)
plt.savefig('test.pdf', bbox_inches='tight')

But not with PGF output

plt.savefig('test.pgf', bbox_inches='tight')

as it returns RuntimeError: Cannot get window extent w/o renderer.

I have been suggested that this may be a bug -- see question on Stackoverflow.

matplotlib 1.3.0 on Ubuntu 13.04

python -c "from matplotlib import pyplot as plt; plt.plot(1); plt.savefig('test.pgf', bbox_inches='tight');"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "PYTHONPATH/matplotlib-1.3.0rc2-py2.7-linux-x86_64.egg/matplotlib/pyplot.py", line 561, in savefig
    return fig.savefig(*args, **kwargs)
  File "PYTHONPATH/matplotlib-1.3.0rc2-py2.7-linux-x86_64.egg/matplotlib/figure.py", line 1410, in savefig
    self.canvas.print_figure(*args, **kwargs)
  File "PYTHONPATH/matplotlib-1.3.0rc2-py2.7-linux-x86_64.egg/matplotlib/backends/backend_qt4agg.py", line 161, in print_figure
    FigureCanvasAgg.print_figure(self, *args, **kwargs)
  File "PYTHONPATH/matplotlib-1.3.0rc2-py2.7-linux-x86_64.egg/matplotlib/backend_bases.py", line 2169, in print_figure
    bbox_inches = self.figure.get_tightbbox(renderer)
  File "PYTHONPATH/matplotlib-1.3.0rc2-py2.7-linux-x86_64.egg/matplotlib/figure.py", line 1551, in get_tightbbox
    bb.append(ax.get_tightbbox(renderer))
  File "PYTHONPATH/matplotlib-1.3.0rc2-py2.7-linux-x86_64.egg/matplotlib/axes.py", line 9153, in get_tightbbox
    bb_xaxis = self.xaxis.get_tightbbox(renderer)
  File "PYTHONPATH/matplotlib-1.3.0rc2-py2.7-linux-x86_64.egg/matplotlib/axis.py", line 1055, in get_tightbbox
    renderer)
  File "PYTHONPATH/matplotlib-1.3.0rc2-py2.7-linux-x86_64.egg/matplotlib/axis.py", line 1038, in _get_tick_bboxes
    extent = tick.label1.get_window_extent(renderer)
  File "PYTHONPATH/matplotlib-1.3.0rc2-py2.7-linux-x86_64.egg/matplotlib/text.py", line 751, in get_window_extent
    raise RuntimeError('Cannot get window extent w/o renderer')
RuntimeError: Cannot get window extent w/o renderer
@mdboom
Copy link
Member

mdboom commented Aug 26, 2013

@pwuertz: As the PGF guy, I thought I'd bring this to your attention.

@ghost ghost assigned pwuertz Aug 26, 2013
@pwuertz
Copy link
Contributor

pwuertz commented Aug 26, 2013

There is indeed a problem I couldn't solve. The way the tight bbox mechanism currently works is to draw a figure to a dummy stream as some kind of initialization. Then it grabs the active Renderer in order to determine the text element sizes and thus the figure size. So the backend must support writing to a stream,.

I implemented stream support in backend_pgf for the ".pdf" and ".png" output, so tight bbox works here. Pgf output on the other hand may consist of multiple files if a figure includes raster images. There is no meaningful way of writing this to a stream, so I denied streams as a target if the output type is ".pgf". This is why tight bbox fails here.

To fix this one could eliminate that dummy save-figure procedure in the tight bbox routine and use the get_renderer method that is also used by tight_layout. At least if I understood the tight bbox mechanism correctly? If so, I could give it a try.

pwuertz added a commit that referenced this issue Sep 10, 2013
 support tight_bbox for pgf output, fixes #2342
pwuertz added a commit that referenced this issue Sep 10, 2013
support tight_bbox for pgf output, fixes #2342 (v1.3.x)
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

Successfully merging a pull request may close this issue.

3 participants