我的数据集:
我有以下数据集,当使用Plotly绘图时,它会产生以下结果(简化):
我的任务:
我想要创建一个信封函数,从上述数据集中提取局部最大值和最小值,并绘制一条包络曲线。大概是这样的:
我已经尝试接近解决方案提供的这里和信封提供的这里。然而,在这种方法中,没有一个对我有用。由于一些奇怪的原因,散点图正在生成下面的结果 (用于本地分钟),这不是我所需要的。
这里是我的第一个情节的代码:
import plotly.express as px
import plotly.graph_objects as go
fig= go.Figure()
fig.add_traces(go.Scatter(x= mem_df['time'], y=mem_df['volatge']))
fig.update_layout(xaxis_title = r'$\text{Time } T \text{ in s} $',
yaxis_title = r'$\text{Deflection angle } \text{ in radians}$')
fig.update_layout(title= r'$\text{Deflection angles versus time for the damping sheet}$')
fig.show()
任何帮助,在这种情况下,将不胜感激!提前谢谢你!
发布于 2022-05-16 15:51:28
问题是你的数据含有一点噪音。一种可能的方法是找到合适的曲线拟合,然后在拟合上找到局部最大值和最小值。scipy
来帮忙:
import plotly.express as px
import plotly.graph_objects as go
import pandas as pd
import numpy as np
from scipy.optimize import curve_fit
from scipy.signal import argrelextrema
mem_df = pd.read_csv("path/to/file")
x = mem_df['time'].values
y = mem_df['deflection'].values
# dampened oscillator
func = lambda x, xi, k, phi, a, b: a * np.sin(k * x - phi) * np.exp(-xi * x) + b
popt, pcov = curve_fit(func, x, y)
fig= go.Figure()
fig.add_traces(go.Scatter(x=x, y=y, name="data"))
# plot the curve fit
xx = np.linspace(0, 20, 150)
yy = func(xx, *popt)
fig.add_traces(go.Scatter(x=xx, y=yy, name="fit"))
idx_max = argrelextrema(yy, np.greater)
idx_min = argrelextrema(yy, np.less)
fig.add_traces(go.Scatter(x=xx[idx_max], y=yy[idx_max], name="top"))
fig.add_traces(go.Scatter(x=xx[idx_min], y=yy[idx_min], name="bottom"))
fig.update_layout(xaxis_title = r'$\text{Time } T \text{ in s} $',
yaxis_title = r'$\text{Deflection angle } \text{ in radians}$')
fig.update_layout(title= r'$\text{Deflection angles versus time for the damping sheet}$')
fig.show()
https://stackoverflow.com/questions/72261594
复制相似问题