具有散景的散点图上的最佳拟合曲线(多项式)
Best fit curve (polynomial) on scatter plot with bokeh
我创建了散景散点图。我想在数据上生成最佳拟合多项式曲线,并将曲线叠加在点云上。
我用 polyfit
:
生成了一条二次折线
import numpy as np
from bokeh.plotting import figure, output_file, show
model2 = np.poly1d(np.polyfit(df['Dist'],df['Speed'], 2)
polyline = np.linspace(1,16000,900)
graph = figure(title = "Speed function of flight distance")
graph.scatter(df['Dist'],df['Speed'])
show(graph)
在散点图上显示这条折线的说明是什么?我看到了如何生成 ,我需要的是折线。
如评论中所述,graph.line()
添加了一个线图。现在,我们只需要一个均匀分布的 x-range,我们在其上绘制拟合函数:
import numpy as np
from bokeh.plotting import figure, output_file, show
#data generation
import pandas as pd
np.random.seed(123)
dist = np.sort(np.random.choice(range(100), 20, replace=False))
speed = 0.3 * dist ** 2 - 2.7 * dist - 1 + np.random.randint(-10, 10, dist.size)
df = pd.DataFrame({'Dist': dist, 'Speed': speed})
model2 = np.poly1d(np.polyfit(df['Dist'], df['Speed'], 2))
x_fit = np.linspace(df['Dist'].min(), df['Dist'].max(), 100)
graph = figure(title = "Speed function of flight distance")
graph.scatter(df['Dist'],df['Speed'])
graph.line(x_fit, model2(x_fit))
show(graph)
示例输出:
我创建了散景散点图。我想在数据上生成最佳拟合多项式曲线,并将曲线叠加在点云上。
我用 polyfit
:
import numpy as np
from bokeh.plotting import figure, output_file, show
model2 = np.poly1d(np.polyfit(df['Dist'],df['Speed'], 2)
polyline = np.linspace(1,16000,900)
graph = figure(title = "Speed function of flight distance")
graph.scatter(df['Dist'],df['Speed'])
show(graph)
在散点图上显示这条折线的说明是什么?我看到了如何生成
如评论中所述,graph.line()
添加了一个线图。现在,我们只需要一个均匀分布的 x-range,我们在其上绘制拟合函数:
import numpy as np
from bokeh.plotting import figure, output_file, show
#data generation
import pandas as pd
np.random.seed(123)
dist = np.sort(np.random.choice(range(100), 20, replace=False))
speed = 0.3 * dist ** 2 - 2.7 * dist - 1 + np.random.randint(-10, 10, dist.size)
df = pd.DataFrame({'Dist': dist, 'Speed': speed})
model2 = np.poly1d(np.polyfit(df['Dist'], df['Speed'], 2))
x_fit = np.linspace(df['Dist'].min(), df['Dist'].max(), 100)
graph = figure(title = "Speed function of flight distance")
graph.scatter(df['Dist'],df['Speed'])
graph.line(x_fit, model2(x_fit))
show(graph)
示例输出: