gpt4 book ai didi

python - 如何绘制Python绘图的导数?

转载 作者:行者123 更新时间:2023-12-03 08:58:36 25 4
gpt4 key购买 nike

我使用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]
})

最佳答案

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

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()

关于python - 如何绘制Python绘图的导数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52957623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com