Python matplotlib 给出更短的回归线
Python matplotlib giving shorter regression line
这是我目前的问题:有时,当我绘制散点图然后绘制回归线(使用相同的数据)时,使用 scipy.stats.linregress(y,X)
计算的回归线太短,如下所示:
正如我已经说过的,散点图和直线的数据是相同的,通常情况下,它是有效的。这是代码:
### "results" comes from a Mysql query and passed to an np.array
Data=np.array(results)
X=Data[:,1]
y=Data[:,0]
slope, intercept, r_value, p_value, std_err=linregress(y,X)
line = slope * X + intercept
plt.scatter(y, X, marker='o',color='#33ffe6',alpha=1,edgecolors='black',linewidths=0.5)
#### Plot
plt.plot(X, line, 'r', label="Regression Line", antialiased=True)
#### Label
plt.xlabel(labels[0])
plt.ylabel(labels[col])
#### scala
plt.xlim(y.min()-((y.max() / float(100)) * 5), y.max() + (y.max() / float(100)) * 5)
plt.ylim(X.min()-((X.max() / float(100)) * 5), X.max() + (X.max() / float(100)) * 5)
#### Legenda
plt.legend(loc="upper center", bbox_to_anchor=(0.5, 1.07), fancybox=True, shadow=True)
这是一个下拉框 link,其中包含包含数据的 .csv 文件:here。第一列是 y,第二列是 X。
问题是你的回归拟合了一条线到 X 作为 Y 的函数,而不是 Y 作为 X 的函数,所以当你构建你的线时你应该使用 Y 作为自变量而不是 X。因为X 和 Y 都没有排序,绘制一条线与定期采样的数组可能也是个好主意。我实际上可能会建议,为了避免所有这些混乱,正常地做事,只是交换你从中得到 X 和 Y 的列。
根据您的 csv 文件,我相信这段代码会生成您想要的图(减去轴标签)。
import pandas as pd
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
df = pd.read_csv(r'C:\Users\smith\Downloads\orders.csv')
Data=df.values
X=Data[:,1]
y=Data[:,0]
slope, intercept, r_value, p_value, std_err=stats.linregress(y,X)
yvals = np.linspace(y.min(),y.max())
line = slope * yvals + intercept # This is the critical change
plt.scatter(y, X, marker='o',color='#33ffe6',alpha=1,edgecolors='black',linewidths=0.5)
#### Plot
plt.plot(yvals, line, 'r', label="Regression Line", antialiased=True)
#### scala
plt.xlim(y.min()-((y.max() / float(100)) * 5), y.max() + (y.max() / float(100)) * 5)
plt.ylim(X.min()-((X.max() / float(100)) * 5), X.max() + (X.max() / float(100)) * 5)
#### Legenda
plt.legend(loc="upper center", bbox_to_anchor=(0.5, 1.07), fancybox=True, shadow=True)
这是我目前的问题:有时,当我绘制散点图然后绘制回归线(使用相同的数据)时,使用 scipy.stats.linregress(y,X)
计算的回归线太短,如下所示:
正如我已经说过的,散点图和直线的数据是相同的,通常情况下,它是有效的。这是代码:
### "results" comes from a Mysql query and passed to an np.array
Data=np.array(results)
X=Data[:,1]
y=Data[:,0]
slope, intercept, r_value, p_value, std_err=linregress(y,X)
line = slope * X + intercept
plt.scatter(y, X, marker='o',color='#33ffe6',alpha=1,edgecolors='black',linewidths=0.5)
#### Plot
plt.plot(X, line, 'r', label="Regression Line", antialiased=True)
#### Label
plt.xlabel(labels[0])
plt.ylabel(labels[col])
#### scala
plt.xlim(y.min()-((y.max() / float(100)) * 5), y.max() + (y.max() / float(100)) * 5)
plt.ylim(X.min()-((X.max() / float(100)) * 5), X.max() + (X.max() / float(100)) * 5)
#### Legenda
plt.legend(loc="upper center", bbox_to_anchor=(0.5, 1.07), fancybox=True, shadow=True)
这是一个下拉框 link,其中包含包含数据的 .csv 文件:here。第一列是 y,第二列是 X。
问题是你的回归拟合了一条线到 X 作为 Y 的函数,而不是 Y 作为 X 的函数,所以当你构建你的线时你应该使用 Y 作为自变量而不是 X。因为X 和 Y 都没有排序,绘制一条线与定期采样的数组可能也是个好主意。我实际上可能会建议,为了避免所有这些混乱,正常地做事,只是交换你从中得到 X 和 Y 的列。
根据您的 csv 文件,我相信这段代码会生成您想要的图(减去轴标签)。
import pandas as pd
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
df = pd.read_csv(r'C:\Users\smith\Downloads\orders.csv')
Data=df.values
X=Data[:,1]
y=Data[:,0]
slope, intercept, r_value, p_value, std_err=stats.linregress(y,X)
yvals = np.linspace(y.min(),y.max())
line = slope * yvals + intercept # This is the critical change
plt.scatter(y, X, marker='o',color='#33ffe6',alpha=1,edgecolors='black',linewidths=0.5)
#### Plot
plt.plot(yvals, line, 'r', label="Regression Line", antialiased=True)
#### scala
plt.xlim(y.min()-((y.max() / float(100)) * 5), y.max() + (y.max() / float(100)) * 5)
plt.ylim(X.min()-((X.max() / float(100)) * 5), X.max() + (X.max() / float(100)) * 5)
#### Legenda
plt.legend(loc="upper center", bbox_to_anchor=(0.5, 1.07), fancybox=True, shadow=True)