我正在绘制三组大约20个时间序列。每个时间序列都有180万个点,我分别绘制它们。为了减少执行时间(因为我发现fig()函数非常耗时),我只打开一个图形,绘制然后清理下一个时间序列。就像是:

import matplotlib.pyplot as plt

fig = plt.figure() # open a figure for plotting
for v in range(nfigs):
    # here I build the x and y vectors from the raw data
    # and also the figname string
    plt.plot(x, y, linewidth=0.3)
    plt.title('a title')
    plt.xlabel('x-label')
    plt.ylabel('y-label')
    plt.grid()
    plt.draw()
    plt.savefig(figname)
    plt.clf()
plt.close() # close the figure after the job is done

除了所有数据集中的所有图之外,所有图都保存正常,这给了我以下错误(为保护隐私而编辑了文件路径):
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "~\AppData\Local\Continuum\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 540, in runfile
    execfile(filename, namespace)
  File "batch.py", line 142, in <module>
    main(fn)
  File "batch.py", line 94, in main
    plt.savefig(figname+'_'+nn+'.png')
  File "~\AppData\Local\Continuum\Anaconda\lib\site-packages\matplotlib\pyplot.py", line 561, in savefig
    return fig.savefig(*args, **kwargs)
  File "~\AppData\Local\Continuum\Anaconda\lib\site-packages\matplotlib\figure.py", line 1421, in savefig
    self.canvas.print_figure(*args, **kwargs)
  File "~\AppData\Local\Continuum\Anaconda\lib\site-packages\matplotlib\backends\backend_qt4agg.py", line 167, in print_figure
    FigureCanvasAgg.print_figure(self, *args, **kwargs)
  File "~\AppData\Local\Continuum\Anaconda\lib\site-packages\matplotlib\backend_bases.py", line 2220, in print_figure
    **kwargs)
  File "~\AppData\Local\Continuum\Anaconda\lib\site-packages\matplotlib\backends\backend_agg.py", line 505, in print_png
    FigureCanvasAgg.draw(self)
  File "~\AppData\Local\Continuum\Anaconda\lib\site-packages\matplotlib\backends\backend_agg.py", line 451, in draw
    self.figure.draw(self.renderer)
  File "~\AppData\Local\Continuum\Anaconda\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "~\AppData\Local\Continuum\Anaconda\lib\site-packages\matplotlib\figure.py", line 1034, in draw
    func(*args)
  File "~\AppData\Local\Continuum\Anaconda\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "~\AppData\Local\Continuum\Anaconda\lib\site-packages\matplotlib\axes.py", line 2086, in draw
    a.draw(renderer)
  File "~\AppData\Local\Continuum\Anaconda\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "~\AppData\Local\Continuum\Anaconda\lib\site-packages\matplotlib\lines.py", line 562, in draw
    drawFunc(renderer, gc, tpath, affine.frozen())
  File "~\AppData\Local\Continuum\Anaconda\lib\site-packages\matplotlib\lines.py", line 938, in _draw_lines
    self._lineFunc(renderer, gc, path, trans)
  File "~\AppData\Local\Continuum\Anaconda\lib\site-packages\matplotlib\lines.py", line 978, in _draw_solid
    renderer.draw_path(gc, path, trans)
  File "~\AppData\Local\Continuum\Anaconda\lib\site-packages\matplotlib\backends\backend_agg.py", line 145, in draw_path
    self._renderer.draw_path(gc, path, transform, rgbFace)
OverflowError: Allocated too many blocks

触发此错误的时间序列绝对没有什么特别的,我使用简单的show()而不是savefig()进行了检查。它既不是第一个也不是最后一个(在大约60个时间序列中),但是足以破坏我的脚本。非常感谢您的帮助!

最佳答案

您可以尝试在agg.path.chunksize文件中或在运行时在matplotlibrc中更改matplotlib.rcParams(10000-100000)的值。它使渲染器以块而不是整个路径的形式绘制路径。但是根据文档,这可能会导致伪像。有关更多信息,请参见http://matplotlib.org/1.3.1/users/customizing.html上的示例matplotlibrc文件。

关于python-2.7 - Matplotlib OverflowError : Allocated too many blocks,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20330475/

10-12 17:50