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

依然范特西╮ 提交于 2019-12-11 07:42:39

问题


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.

Why is this happening and is there a way to work around it?

matplotlib 1.3.0rc2 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

回答1:


By the way, there is a workaround. Normally the extent of PGF/TikZ images is adjusted automatically so it matches the drawing. For preserving the figure size intended by matplotlib, these lines are added to the output:

\pgfpathrectangle{\pgfpointorigin}{\pgfqpoint{8.000000in}{6.000000in}}%
\pgfusepath{use as bounding box}%

If you remove these lines at the very top from the PGF output you should get rid of any additional space around the figure.

Using plt.tight_layout(), or better plt.figure(tight_layout=True) in version 1.3, is another way to do that (which works perfectly with PGF and PGF->PDF), although it is a bit different. It recalculates the figure's layout so it fits to the given figure size. I usually prefer that method because it also eliminates problems like overlapping text elements.



来源:https://stackoverflow.com/questions/18405652/savefig-pgf-runtimeerror-cannot-get-window-extent-w-o-renderer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!