查找单个序列的R2和斜率

时间:2018-09-20 08:10:33

标签: python python-3.x pandas matplotlib scipy

我在以绿色突出显示的列中有数据,我需要使用Python转到以黄色突出显示的列和绘图。我知道如何在使用matplotlib在python中绘制数据或使用scipy找出斜率和R2时,必须找出整列的斜率和R2。但是,从所附图像可以看出,对于四个不同的国家,我需要四个不同的斜率和四个不同的R2和四个不同的地块。enter image description here

数据以绿色突出显示。解决方案以黄色突出显示,并显示四个图。我尝试在线研究许多论坛,但发现很难找到解决方案。感谢您的阅读。

PS:我在x轴和y轴上使用了随机数据。

编辑:

我一直用于计算斜率的代码是:

import numpy, scipy,pandas as pd, matplotlib
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
from scipy.stats import linregress
import scipy.stats
import copy
df=pd.read_excel("I:/Python/Excel.xlsx")
df.head();

xyDataPairs = df[['x-axis', 'y-axis']].values.tolist();
slope = linregress(x, y)[0];  # slope in units of y / x

print('best slope=', slope)

1 个答案:

答案 0 :(得分:0)

这应该可以解决问题

df.groupby('Country', as_index=True)['x-axis','y-axis'].apply(linregress)

编辑

要按子图按类别绘制数据,seaborn具有直观的语法。

import seaborn as sn
sns.set(style="ticks", color_codes=True)

sns.catplot(x="x-axis", y="y-axis",
            col="Country", aspect=.6,
             data=df);
plt.show()
相关问题