Plotly:如何使用 DASH 回调将多项式拟合线添加到 plotly go.scatter 图?

Posted

技术标签:

【中文标题】Plotly:如何使用 DASH 回调将多项式拟合线添加到 plotly go.scatter 图?【英文标题】:Plotly: How to add polynomial fit line to plotly go.scatter figure using a DASH callback? 【发布时间】:2020-11-25 06:55:17 【问题描述】:

我想将多项式曲线添加到使用回调呈现的散点图中。

以下是我的回调函数,它返回散点图。

@app.callback(Output('price-graph', 'figure'),
              [
                 Input('select', 'value')
              ]
             )
def update_price(sub):

    if sub:

        fig1 = go.Figure(

            data=[go.Scatter(

                            x=dff['Count'],
                            y=dff['Rent'],
                            mode='markers'

                            )
                  ],

            layout=go.Layout(

                title='',

                xaxis=dict(
                    tickfont=dict(family='Rockwell', color='crimson', size=14)
                ),

                yaxis=dict(

                    showticklabels = True

                ),

            )
        )

        return fig1

结果图:

我可以使用 sklearn.preprocessing 添加一条 polyfit 线。

from sklearn.preprocessing import PolynomialFeatures 
from sklearn.linear_model import LinearRegression
from sklearn.pipeline import make_pipeline


dff = df.groupby(['Rent']).size().reset_index(name='Count')

fig = plt.figure(figsize=(15,8)) 

x = dff['Count']
y = dff['Rent']

model = make_pipeline(PolynomialFeatures(4), LinearRegression())
model.fit(np.array(x).reshape(-1, 1), y)
x_reg = np.arange(90)
y_reg = model.predict(x_reg.reshape(-1, 1))

plt.scatter(x, y)
plt.plot(x_reg, y_reg)
plt.xlim(0,100)
plt.xlabel('Number of rental units leased')
plt.ylim(10,50)
plt.show()

有没有办法在情节中做到这一点?

【问题讨论】:

【参考方案1】:

您尚未指定如何使用 DASH。在此示例中,我在 JupyterLab 中使用 JupyterDASH(是的,这太棒了!)。

下图由下面的代码 sn-p 生成。 sn-p 使用回调函数更改设置多项式特征数量的参数nFeatures in:

 model = make_pipeline(PolynomialFeatures(nFeatures), LinearRegression())
 model.fit(np.array(x).reshape(-1, 1), y)

我正在使用dcc.Slider 来更改值。

nFeatures = 1 的默认设置

使用 nFeatures = 3 的滑块选择设置

完整代码:

import numpy as np
import plotly.express as px
import plotly.graph_objects as go
from jupyter_dash import JupyterDash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output

from sklearn.preprocessing import PolynomialFeatures 
from sklearn.linear_model import LinearRegression
from sklearn.pipeline import make_pipeline

from IPython.core.debugger import set_trace

# Load Data
df = px.data.tips()
# Build App
app = JupyterDash(__name__)
app.layout = html.Div([
    html.H1("ScikitLearn: Polynomial features"),
    dcc.Graph(id='graph'),
    html.Label([
        "Set number of features",
        dcc.Slider(id='PolyFeat',
    min=1,
    max=6,
    marks=i: ''.format(i) for i in range(10),
    value=1,
) 
    ]),
])

# Define callback to update graph
@app.callback(
    Output('graph', 'figure'),
    [Input("PolyFeat", "value")]
)

def update_figure(nFeatures):
    
    global model

    # data
    df = px.data.tips()
    x=df['total_bill']
    y=df['tip']

    # model
    model = make_pipeline(PolynomialFeatures(nFeatures), LinearRegression())
    model.fit(np.array(x).reshape(-1, 1), y)
    x_reg = x.values
    y_reg = model.predict(x_reg.reshape(-1, 1))
    df['model']=y_reg

    # figure setup and trace for observations
    fig = go.Figure()
    fig.add_traces(go.Scatter(x=df['total_bill'], y=df['tip'], mode='markers', name = 'observations'))

    # trace for polynomial model
    df=df.sort_values(by=['model'])
    fig.add_traces(go.Scatter(x=df['total_bill'], y=df['model'], mode='lines', name = 'model'))
    
    # figure layout adjustments
    fig.update_layout(yaxis=dict(range=[0,12]))
    fig.update_layout(xaxis=dict(range=[0,60]))
    print(df['model'].tail())
    return(fig)

# Run app and display result inline in the notebook
app.enable_dev_tools(dev_tools_hot_reload =True)
app.run_server(mode='inline', port = 8070, dev_tools_ui=True, #debug=True,
              dev_tools_hot_reload =True, threaded=True)

【讨论】:

以上是关于Plotly:如何使用 DASH 回调将多项式拟合线添加到 plotly go.scatter 图?的主要内容,如果未能解决你的问题,请参考以下文章

Plotly Dash 表回调

回调错误更新 plot-div.children (Plotly Dash)

Plotly-Dash 实时图表:回调失败

Plotly Dash:下拉组件回调可见性错误

Plotly Dash dcc.Interval 在一段时间后失败:回调错误更新 graph.figure

Plotly Dash 回调错误更新输出图