scipy cumtrapz()除以零或模数

时间:2017-08-10 20:21:53

标签: python csv matplotlib scipy

我已使用pandas从带有matplotlib.pyplot的.csv中提取数据。我的数据日期/时间格式是每5分钟一次,但我需要整合超过1天的时间才能为每天制作一个数据点。

我一直收到这个错误:

Traceback (most recent call last):
  File "integrating.py", line 25, in <module>
    plt.plot(times, integ)
  File "/usr/local/lib/python3.6/site-packages/matplotlib/pyplot.py", line 3317, in plot
    ret = ax.plot(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/matplotlib/__init__.py", line 1898, in inner
    return func(ax, *args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/matplotlib/axes/_axes.py", line 1406, in plot
    for line in self._get_lines(*args, **kwargs):
  File "/usr/local/lib/python3.6/site-packages/matplotlib/axes/_base.py", line 407, in _grab_next_args
    for seg in self._plot_args(remaining, kwargs):
  File "/usr/local/lib/python3.6/site-packages/matplotlib/axes/_base.py", line 395, in _plot_args
    seg = func(x[:, j % ncx], y[:, j % ncy], kw, kwargs)
ZeroDivisionError: integer division or modulo by zero

我怀疑是格式化问题,因为当我使用下面的数组时它会起作用。我已经尝试将datas转换为数组,但我不确定它是否值得进一步追求。我也知道这部分数据不包含任何零,所以我不确切地知道它为什么告诉我这个。

非常感谢任何帮助。我真的只是想让它工作,即使我需要采取另一个方向。谢谢!

从csv中提取数据后的格式为:

     Data
367                   3
368                   4
369                   5
370                   5
371                   6
372                   7
373                   8
374                   8
375                   9
376                  10

代码:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import scipy
from scipy import integrate

file = pd.read_csv("Compiled.csv",parse_dates=['Date/Time'])
times = file.loc[367:432, ['Date/Time']]
datas = file.loc[367:432, ['Data']]

#times = [1,2,3,4,5,6,7,8,9]
#datas = [1,2,3,4,5,0,3,2,1]

plt.plot(times, scipy.integrate.cumtrapz(datas, x=None))
plt.show()

解决方案:

times = datas['Date/Time'].loc[367:532]
integ = scipy.integrate.cumtrapz(datas['Data'].loc[366:532].values, x=None)
plt.plot(times, integ)

1 个答案:

答案 0 :(得分:0)

应该将值作为数组提供给cumtrapz函数

integ = scipy.integrate.cumtrapz(datas['Data'].values, x=None)

由于integ的值小于datas,因此您的绘图需要考虑

plt.plot(times.loc[1:], scipy.integrate.cumtrapz(datas, x=None))

此功能仅在times包含数字数据或datetime个对象时才有效。