【问题标题】:Best fit curve (polynomial) on scatter plot with bokeh散景散点图上的最佳拟合曲线(多项式)
【发布时间】:2022-01-25 23:06:10
【问题描述】:

我用散景创建了一个散点图。我想在数据上生成最佳拟合多项式曲线,并将曲线叠加在点云上。

我用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)

在散点图顶部显示这条折线的说明是什么?我知道如何生成a line of best fit,我需要一条折线。

【问题讨论】:

  • 您可以使用graph.line()在图表中添加一条线。
  • 它也适用于曲线吗?

标签: python bokeh


【解决方案1】:

如 cmets 中所述,graph.line() 添加了线图。现在,我们只需要一个均匀分布的 x 范围,在其上绘制拟合函数:

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)

示例输出:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-22
    • 2016-03-06
    • 1970-01-01
    • 2019-11-06
    相关资源
    最近更新 更多