如何解决matplotlib中的溢出错误?

时间:2018-07-22 01:21:39

标签: python numpy matplotlib physics differential-equations

我正在用scipy.integrate的odeint包求解一组耦合的微分方程。

对于积分时间,我有:

 t=numpy.linspace(0,8e+9,5e+06)

其中5e + 06是时间步长。

然后我将这样的方程式绘制出来:

plt.xscale('symlog') #x axis logarithmic scale
plt.yscale('log',basey=2) #Y axis logarithmic scale
plt.gca().set_ylim(8, 100000) #Changing y axis ticks
ax = plt.gca()
ax.yaxis.set_major_formatter(matplotlib.ticker.ScalarFormatter())
ax.xaxis.set_major_formatter(matplotlib.ticker.ScalarFormatter())
plt.title("Example graph")
plt.xlabel("time (yr)")
plt.ylabel("quantity a")
plt.plot(t,a,"r-", label = 'Example graph')
plt.legend(loc='best')

其中a是时间相关变量。 (这只是很多图中的一张。)

但是,这些图看起来有些参差不齐,而不是像振荡的那样,我得到了这个错误:

OverflowError: Exceeded cell block limit (set 'agg.path.chunksize' rcparam)

我不太确定此错误的含义,我查看了其他答案,但不知道如何实现“ agg.path.chunksize”。

此外,集成+绘图大约需要7个小时,而且要花一些CPU处理时间,所以我真的不想实施任何会增加时间的事情。

如何克服此错误?

我试图减少时间步长,但是我却得到了这个错误:

Excess work done on this call (perhaps wrong Dfun type).
Run with full_output = 1 to get quantitative information.

1 个答案:

答案 0 :(得分:2)

如错误消息所提示,您可以set the chunksize to a larger value

plt.rcParams['agg.path.chunksize'] = 1000

但是,您可能也批判性地反映了为什么首先发生此错误。仅当您试图在图表上绘制大量数据时,才会发生这种情况。这意味着,如果尝试绘制200000000点,则渲染器可能会遇到问题,无法将它们全部保留在内存中。但是人们可能应该问自己,为什么必须画出这么多点呢?屏幕可能会在横向方向上显示大约2000个点,而在打印纸上可能会显示6000个点。通常来说,使用更多的点是没有意义的。

现在,如果您的微分方程式的解决方案需要大的点密度,则并不自动意味着您需要全部绘制它们。

例如可以每100个点绘制一次,

plt.plot(x[::100], y[::100])

最有可能甚至不会影响视觉图的外观。