Matplotlib OverflowError:分配了太多块

时间:2013-12-02 14:19:14

标签: python-2.7 python-3.x matplotlib plot

我正在策划三组约20个时间序列。每个时间序列有180万点,我正在分别绘制它们。为了减少执行时间(因为我发现figure()函数非常耗时),我只打开一个数字,绘图然后清理下一个时间序列。类似的东西:

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个时间序列)但它足以打破我的剧本。对此的帮助非常感谢!

2 个答案:

答案 0 :(得分:9)

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

答案 1 :(得分:6)

当设置matplotlib.pyplot.rcParams['agg.path.chunksize'] = 20000时,我的问题就解决了。 png数字很好。