【问题标题】:Bokeh plot regression lines on scatter plot散点图上的散景图回归线
【发布时间】:2019-02-09 06:36:34
【问题描述】:

我使用 Python 和 Bokeh 在同一个图表中生成了两个散点图,并添加了复选框以允许单独查看散点图。

如何使用 Bokeh 为两个散点图(带有方程)添加回归线?

output_file("Scatterplot.html")

#scatter plot
S0 = f.circle(A_area, A_price,
         fill_alpha=0.3, size=3, color='green')
S1 = f.circle(B_area, B_price,
         fill_alpha=0.3, size=3, color='blue')

#widget-checkbox
checkboxes = CheckboxGroup(labels=["A", "B"], active=[0, 1])
callback = CustomJS(code="""S0.visible = false; // same S0 passed in from args
                            S1.visible = false;
                            // cb_obj injected in by the callback
                            if (cb_obj.active.includes(0)){S0.visible = true;} // 0 index box is S0
                            if (cb_obj.active.includes(1)){S1.visible = true;}""",
                    args={'S0': S0, 'S1': S1})

checkboxes.js_on_click(callback)

【问题讨论】:

    标签: python regression bokeh scatter-plot


    【解决方案1】:

    你用 numpy 计算线拟合,然后在散景中绘制它:

    import numpy as np
    from bokeh.plotting import figure
    from bokeh.io import show
    
    #the data
    x=np.array([0,1,2,3,4,5,6,7,8])
    y=np.array([1,2,3,5,4,6,8,7,9])
    
    # determine best fit line
    par = np.polyfit(x, y, 1, full=True)
    slope=par[0][0]
    intercept=par[0][1]
    y_predicted = [slope*i + intercept  for i in x]
    
    # plot it
    fig=figure()
    fig.circle(x,y)
    fig.line(x,y_predicted,color='red',legend='y='+str(round(slope,2))+'x+'+str(round(intercept,2)))
    show(fig)
    

    【讨论】:

      【解决方案2】:

      Bokeh 在其documentation 中也有一个 Slope 类,用于绘制这样的回归线。您只需要截距和斜率即可。另外,我正在使用 scikit-learn 中的 LinearRegression(),但 Joris 的回答中的 np.polyfit 显然也可以。

      import numpy as np
      from sklearn.linear_model import LinearRegression
      from bokeh.models import Slope
      from bokeh.plotting import figure, show
      from bokeh.io import output_notebook
      output_notebook()
      
      # Data
      x=np.array([0,1,2,3,4,5,6,7,8])
      y=np.array([1,2,3,5,4,6,8,7,9])
      
      # Make and fit a linear regression model
      model = LinearRegression().fit(x.reshape(-1, 1), y)
      # x values need to be in a two-dimensional array, so use .reshape(-1, 1)
      
      # Find the slope and intercept from the model
      slope = model.coef_[0] # Takes the first element of the array
      intercept = model.intercept_
      
      # Make the regression line
      regression_line = Slope(gradient=slope, y_intercept=intercept, line_color="red")
      
      # Plot the data and regression line
      fig=figure()
      fig.circle(x, y)
      fig.add_layout(regression_line)
      show(fig)
      

      【讨论】:

      • 如果您希望一条线在整个绘图范围内运行,而不是在 x 范围结束的地方结束,这是一个比接受的答案更好的选择
      猜你喜欢
      • 2021-02-06
      • 2021-01-28
      • 1970-01-01
      • 2012-06-27
      • 2016-05-30
      • 1970-01-01
      • 1970-01-01
      • 2022-12-11
      • 2019-10-22
      相关资源
      最近更新 更多