Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python savefig PGF-运行时错误:无法获取不带渲染器的窗口范围_Python_Matplotlib_Pgf - Fatal编程技术网

Python savefig PGF-运行时错误:无法获取不带渲染器的窗口范围

Python savefig PGF-运行时错误:无法获取不带渲染器的窗口范围,python,matplotlib,pgf,Python,Matplotlib,Pgf,我正试图在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时:无法获取不带渲染器的窗口范围 为什么会发生这种情况,有没有办法解决 Ubuntu

我正试图在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时:无法获取不带渲染器的窗口范围

为什么会发生这种情况,有没有办法解决

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
python-c“从matplotlib导入pyplot作为plt;plt.plot(1);plt.savefig('test.pgf',bbox_inches='tight');”
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
savefig中第561行的文件“PYTHONPATH/matplotlib-1.3.0rc2-py2.7-linux-x86_64.egg/matplotlib/pyplot.py”
返回图savefig(*args,**kwargs)
savefig中的文件“PYTHONPATH/matplotlib-1.3.0rc2-py2.7-linux-x86_64.egg/matplotlib/figure.py”,第1410行
self.canvas.print_图(*args,**kwargs)
文件“PYTHONPATH/matplotlib-1.3.0rc2-py2.7-linux-x86_64.egg/matplotlib/backends/backend_qt4agg.py”,第161行,打印图
图canvasagg.打印图(self,*args,**kwargs)
打印图中的文件“PYTHONPATH/matplotlib-1.3.0rc2-py2.7-linux-x86_64.egg/matplotlib/backend_base.py”,第2169行
bbox_inches=self.figure.get_tightbox(渲染器)
文件“PYTHONPATH/matplotlib-1.3.0rc2-py2.7-linux-x86_64.egg/matplotlib/figure.py”,第1551行,在get_Tightbox中
追加(ax.get_-tightbox(渲染器))
文件“PYTHONPATH/matplotlib-1.3.0rc2-py2.7-linux-x86_64.egg/matplotlib/axes.py”,第9153行,在get_Tightbox中
bb_xaxis=self.xaxis.get_tightbox(渲染器)
文件“PYTHONPATH/matplotlib-1.3.0rc2-py2.7-linux-x86_64.egg/matplotlib/axis.py”,第1055行,在get_Tightbox中
渲染器)
文件“PYTHONPATH/matplotlib-1.3.0rc2-py2.7-linux-x86_64.egg/matplotlib/axis.py”,第1038行,在“获取”框中
范围=勾选.label1.get\u window\u范围(渲染器)
文件“PYTHONPATH/matplotlib-1.3.0rc2-py2.7-linux-x86_64.egg/matplotlib/text.py”,第751行,在获取窗口中
raise RUNTIMERROR('无法获取没有呈现程序的窗口范围')
运行时错误:无法获取不带渲染器的窗口范围

顺便说一句,这里有一个解决办法。通常,PGF/TikZ图像的范围会自动调整,以便与图形匹配。为了保留matplotlib所需的地物大小,这些线将添加到输出中:

\pgfpathrectangle{\pgfpointorigin}{\pgfqpoint{8.000000in}{6.000000in}}%
\pgfusepath{use as bounding box}%
如果从PGF输出中删除最顶部的这些行,则应该去掉图形周围的任何额外空间


在1.3版中使用
plt.tight\u layout()
,或者更好的
plt.figure(tight\u layout=True)
是实现这一点的另一种方法(它与PGF和PGF->PDF完美结合),尽管有点不同。它会重新计算地物的布局,使其适合给定的地物大小。我通常更喜欢这种方法,因为它还消除了诸如重叠文本元素之类的问题。

顺便说一句,有一种解决方法。通常,PGF/TikZ图像的范围会自动调整,以便与图形匹配。为了保留matplotlib所需的地物大小,这些线将添加到输出中:

\pgfpathrectangle{\pgfpointorigin}{\pgfqpoint{8.000000in}{6.000000in}}%
\pgfusepath{use as bounding box}%
如果从PGF输出中删除最顶部的这些行,则应该去掉图形周围的任何额外空间


在1.3版中使用
plt.tight\u layout()
,或者更好的
plt.figure(tight\u layout=True)
是实现这一点的另一种方法(它与PGF和PGF->PDF完美结合),尽管有点不同。它会重新计算地物的布局,使其适合给定的地物大小。我通常更喜欢这种方法,因为它还消除了诸如重叠文本元素之类的问题。

最终版本也会出现这种情况吗?如果是这样的话,请报告这是一个错误。哦,我不知道它出来了。是的,1.3.0决赛也是如此。我现在就报告它。修复了上游,应该包含在下一个错误修复版本中。这在最终版本中也会发生吗?如果是这样的话,请报告这是一个错误。哦,我不知道它出来了。是的,1.3.0决赛也是如此。我现在就报告它。fixed upstream,应该包含在下一个bug修复版本中。我尝试将
plt.figure(tight\u layout=True)
添加到我的代码中,但它没有任何作用-我应该在哪里添加它?就在savefig()之前?
plt.figure
创建一个新的地物,当您开始打印而不创建地物时,matplotlib会隐式执行此操作。在向地物添加任何绘图之前,您必须执行该调用。
plt.tight\u layout()
另一方面,最好放在
savefig
之前,它似乎没有任何作用-我得到的只是这个(似乎不是很紧)紧布局的默认填充不是零。尝试
plt.tight\u layout(pad=0)
plt.figure(tight\u layout={“pad”:0})
。添加该代码仍然不会更改我上面链接的图形。我尝试将
plt.figure(tight\u layout=True)
添加到我的代码中,但它没有任何作用-我应该在哪里添加它?就在savefig()之前?
plt.figure
创建一个新的地物,当您开始打印而不创建地物时,matplotlib会隐式执行此操作。在向地物添加任何绘图之前,您必须执行该调用。
plt.tight\u layout()
另一方面,最好放在
savefig
之前,它似乎没有任何作用-我得到的只是这个(似乎不是很紧)紧布局的默认填充不是零。尝试
plt.tight\u布局(pad=0)
plt.figure(tight\u布局={“pad”:0})
。添加该代码仍然不会更改我链接到上面的图形。