如何绘制剧情python的导数?

时间:2018-10-23 20:49:00

标签: python

我通过以下功能使用plotly绘制数据:

data_t = []

for mac, dico_data in dict_info.items():
    data_t.append(go.Scatter( x= dico_data["time"], y= dico_data['val'], name=mac ))
    print (data_t)
data = data_t
offline.plot(data_t)

我需要使用图形中的一组数据点来找到导数并将其绘制出来。但是我不知道该怎么做? 这是我的数据的示例:

[Scatter({
    'name': '14:15:92:cc:00:00:00:01',
    'x': [707, 1212, 1616, 1818, 2020, 2121, 2323, 2424, 2525, 6969, 11009, 11716,
          12019, 16059, 16564, 19493, 20099, 23533, 23836, 25149, 29896, 43127,
          45147, 45753, 55045, 66761, 66862, 77467, 81204, 82921, 92718, 104434],
    'y': [1539071748.0, 1539071752.0, 1539071755.0, 1539071757.0, 1539071759.0,
          1539071760.0, 1539071764.0, 1539071765.0, 1539071768.0, 1539071872.0,
          1539071979.0, 1539071998.0, 1539072006.0, 1539072123.0, 1539072137.0,
          1539072226.0, 1539072250.0, 1539072386.0, 1539072398.0, 1539072450.0,
          1539072637.0, 1539073158.0, 1539073243.0, 1539073268.0, 1539073615.0,
          1539074097.0, 1539074101.0, 1539074533.0, 1539074691.0, 1539074763.0,
          1539075159.0, 1539075623.0]
})]
[Scatter({
    'name': '14:15:92:cc:00:00:00:01',
    'x': [707, 1212, 1616, 1818, 2020, 2121, 2323, 2424, 2525, 6969, 11009, 11716,
          12019, 16059, 16564, 19493, 20099, 23533, 23836, 25149, 29896, 43127,
          45147, 45753, 55045, 66761, 66862, 77467, 81204, 82921, 92718, 104434],
    'y': [1539071748.0, 1539071752.0, 1539071755.0, 1539071757.0, 1539071759.0,
          1539071760.0, 1539071764.0, 1539071765.0, 1539071768.0, 1539071872.0,
          1539071979.0, 1539071998.0, 1539072006.0, 1539072123.0, 1539072137.0,
          1539072226.0, 1539072250.0, 1539072386.0, 1539072398.0, 1539072450.0,
          1539072637.0, 1539073158.0, 1539073243.0, 1539073268.0, 1539073615.0,
          1539074097.0, 1539074101.0, 1539074533.0, 1539074691.0, 1539074763.0,
          1539075159.0, 1539075623.0]
})

1 个答案:

答案 0 :(得分:1)

您可以执行以下类似操作,以获取数据样本:

data = {
        'x': [1539071748.0, 1539071752.0, 1539071755.0, 1539071757.0, 1539071759.0,
          1539071760.0, 1539071764.0, 1539071765.0, 1539071768.0, 1539071872.0,
          1539071979.0, 1539071998.0, 1539072006.0, 1539072123.0, 1539072137.0,
          1539072226.0, 1539072250.0, 1539072386.0, 1539072398.0, 1539072450.0,
          1539072637.0, 1539073158.0, 1539073243.0, 1539073268.0, 1539073615.0,
          1539074097.0, 1539074101.0, 1539074533.0, 1539074691.0, 1539074763.0,
          1539075159.0, 1539075623.0],
        'y': [707, 1212, 1616, 1818, 2020, 2121, 2323, 2424, 2525, 6969, 11009, 11716,
              12019, 16059, 16564, 19493, 20099, 23533, 23836, 25149, 29896, 43127,
              45147, 45753, 55045, 66761, 66862, 77467, 81204, 82921, 92718, 104434]
        }

要计算您的导数(请注意,data['y_p']的大小为n-1,因此data['y_p'][i]实际上是(data['x'][i] + data['x'][i+1]) / 2的导数的近似值):

import numpy as np

data['y_p'] = np.diff(data['y']) / np.diff(data['x'])
data['x_p'] = (np.array(data['x'])[:-1] + np.array(data['x'])[1:]) / 2

然后绘制结果:

import matplotlib.pyplot as plt

plt.figure(1)
plt.plot(data['x'], data['y'], 'r')
plt.plot(data['x_p'], data['y_p'], 'b')
plt.show()