我正在尝试以较低的保证金在pyplot中保存一个数字。
以下代码与PDF输出完美配合:
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: Cannot get window extent w/o renderer
。
为什么会发生这种情况,有没有办法解决它?
Ubuntu 13.04上的matplotlib 1.3.0rc2
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
答案 0 :(得分:1)
顺便说一下,有一个解决方法。通常,PGF / TikZ图像的范围会自动调整,以便与绘图相匹配。为了保留matplotlib预期的图形大小,这些行将添加到输出中:
\pgfpathrectangle{\pgfpointorigin}{\pgfqpoint{8.000000in}{6.000000in}}%
\pgfusepath{use as bounding box}%
如果你从PGF输出的最顶部删除这些线,你应该摆脱图周围的任何额外空间。
在版本1.3中使用plt.tight_layout()
或更好的plt.figure(tight_layout=True)
是另一种方法(它与PGF和PGF-> PDF完美配合),尽管它有点不同。它会重新计算图形的布局,使其适合给定的图形尺寸。我通常更喜欢这种方法,因为它还消除了重叠文本元素等问题。