【发布时间】: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()
有没有办法在情节中做到这一点?
【问题讨论】:
标签: python plotly plotly-dash polynomials plotly.js