我有不同的資料集:
Df1
X Y
1 1
2 5
3 14
4 36
5 90
Df2
X Y
1 1
2 5
3 21
4 38
5 67
Df3
X Y
1 1
2 5
3 10
4 50
5 78
我想確定一條適合該資料的線并將所有資料繪制在一個圖表中(如回歸)。在 x 軸上,我有時間;在 y 軸上,我有一個事件發生的頻率。關于如何確定線和繪制結果保持不同圖例的方法的任何幫助(使用 seaborn 或 matplotlib 都可以)將有所幫助。
到目前為止,我所做的是將三行繪制如下:
plot_df = pd.DataFrame(list(zip(dataset_list, x_lists, y_lists)),
columns =['Dataset', 'X', 'Y']).set_index('Dataset', inplace=False)
plot_df= plot_df.apply(pd.Series.explode).reset_index() # this step should transpose the resulting df and explode the values
# plot
fig, ax = plt.subplots(figsize=(10,8))
for name, group in plot_df.groupby('Dataset'):
group.plot(x = "X", y= "Y", ax=ax, label=name)
請注意,開頭的三個串列包含有關三個不同 df 的資訊。
uj5u.com熱心網友回復:
我建議使用linregressfrom,scipy.stats因為這提供了非常易讀的代碼。只需將邏輯添加到您的回圈中:
from scipy.stats import linregress
for name, group in plot_df.groupby('Dataset'):
group.plot(x = "X", y= "Y", ax=ax, label=name)
#fit a line to the data
fit = linregress(group.X, group.Y)
ax.plot(group.X, group.X * fit.slope fit.intercept, label=f'{name} fit')
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/429370.html
標籤:Python 麻木的 matplotlib scikit-学习
上一篇:遍歷numpy陣列和向量的列
