回归线在seaborn / matplotlib图表中表现奇怪

时间:2018-04-26 06:35:54

标签: python matplotlib seaborn

有没有更好的方法在seaborn / matplotlib中沿着散点图绘制函数?我只是做regplot,但由于某种原因,回归线正在做...那。

奖金问题:我有没有办法让seaborn告诉我它用于线路的功能是什么?

我真的需要一条线和一个实际的功能,这些点的最佳方式。

由于

a = { 4:44, 8:167, 16:479, 32:1239, 64: 2991, 128: 7012, 256: 15997, 512: 35922, 1024: 79590, 2048: 174699, 4096:380155,
8192: 821857, 16384: 1766870, 32768: 3779259, 65536: 8050043, 131072:17983451, 262144: 36133203, 524288: 76198683,
1048576:160262930, 2097152: 336255735, 4194304: 703966918, 8388608: 1470850124, 16777216: 3067526261, 33554432: 6386708998}



df = pd.DataFrame({'n':list(a.keys()), 'operations':list(a.values())})
f, ax = plt.subplots(figsize=(6, 6))
ax.set(xscale="log", yscale="log")

ax= sns.regplot('n', 'operations', df, ci=None)

plt.show()

enter image description here

1 个答案:

答案 0 :(得分:0)

仅显示您的回归线未通过原点(0,0)。

您可以通过numpy的

检索第一度多项式拟合的斜率和偏移量
import numpy as np
k = np.polyfit(df['n'], df['operations'], 1)

并将其绘制成

plt.plot(df['n'], df['n']*k[0]+k[1])

然而,这将导致(几乎)相同的图片。 但是使用polyfit,您有机会对数据点进行加权。 如果你给出原点,例如1000和其余1,你得到一条线(0,0)。