1

I have created a scatter plot with bokeh. 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:

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? I see how to generate a line of best fit, my need is for a polyline.

1 Answers1

3

As mentioned in the comments, graph.line() adds a line plot. Now, we just need an evenly spaced x-range over which we plot the fitted 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: enter image description here

Mr. T
  • 11,960
  • 10
  • 32
  • 54