savefig PGF-RuntimeError:无法获得不带渲染器的窗口范围 [英] savefig PGF - RuntimeError: Cannot get window extent w/o renderer
问题描述
我正在尝试将图形保存在pyplot中且页边距很小.
I'm trying to save a figure in pyplot with tight margins.
以下代码与PDF输出完美配合:
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')
但不是 PGF 输出
plt.savefig('test.pgf', bbox_inches='tight')
因为它返回 RuntimeError:无法获取不带渲染器的窗口范围
.
为什么会发生这种情况,是否有解决方法?
Why is this happening and is there a way to work around it?
Ubuntu 13.04 上的 matplotlib 1.3.0rc2
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
推荐答案
顺便说一句,有一种解决方法.通常,PGF/TikZ 图像的范围会自动调整,使其与绘图相匹配.为了保留 matplotlib 预期的图形大小,将这些行添加到输出中:
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}%
如果您从 PGF 输出中删除最顶部的这些行,您应该删除图形周围的任何额外空间.
If you remove these lines at the very top from the PGF output you should get rid of any additional space around the figure.
使用1.3版中的 plt.tight_layout()
或更好的 plt.figure(tight_layout = True)
是另一种方法(与PGF完美配合和PGF-> PDF),尽管有些不同.它会重新计算图形的布局,使其适合给定的图形尺寸.我通常更喜欢这种方法,因为它还能消除文本元素重叠等问题.
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.
这篇关于savefig PGF-RuntimeError:无法获得不带渲染器的窗口范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!