简体   繁体   English

散景 plot 散景上的最佳拟合曲线(多项式)

[英]Best fit curve (polynomial) on scatter plot with bokeh

I have created a scatter plot with bokeh.我用散景创建了散点图 plot。 I want to generate a best fit polynomial curve on the data, and superimpose the curve on the cloud of points.我想在数据上生成最佳拟合多项式曲线,并将曲线叠加在点云上。

I have generated a 2nd degree polyline with polyfit :我用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)

What is the instruction for showing this polyline on top of the scatter plot?在散点图 plot 上显示这条折线的指令是什么? I see how to generate a line of best fit , my need is for a polyline.我看到了如何生成一条最合适的线,我需要一条折线。

As mentioned in the comments, graph.line() adds a line plot.如评论中所述, graph.line()添加了一行 plot。 Now, we just need an evenly spaced x-range over which we plot the fitted function:现在,我们只需要一个均匀分布的 x 范围,在该范围上我们 plot 拟合 function:

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)

Sample output:样品 output: 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM