Plotly:如何使用plotly绘制回归线?

问题描述 投票:1回答:1

我有一个数据帧df,其列为pm1和pm25。我想显示这两个信号之间的相关性的图表(带有Plotly)。到目前为止,我已经设法显示了散点图,但是我没有设法画出信号之间的相关性的拟合线。到目前为止,我已经尝试过:

denominator=df.pm1**2-df.pm1.mean()*df.pm1.sum()
print('denominator',denominator)
m=(df.pm1.dot(df.pm25)-df.pm25.mean()*df.pm1.sum())/denominator
b=(df.pm25.mean()*df.pm1.dot(df.pm1)-df.pm1.mean()*df.pm1.dot(df.pm25))/denominator
y_pred=m*df.pm1+b


lineOfBestFit = go.Scattergl(
    x=df.pm1,
    y=y_pred,
    name='Line of best fit',
    line=dict(
        color='red',
    )
)

data = [dataPoints, lineOfBestFit]
figure = go.Figure(data=data)

figure.show()

图:

enter image description here

如何使lineOfBestFit正确绘制?

python dataframe graph plotly figure
1个回答
1
投票

对于回归分析,我喜欢使用statsmodels.api。我还喜欢在熊猫数据框中组织数据和回归结果。这是一种以一种干净有序的方式来做您想要的事情的方法:

图:

enter image description here

代码:

import plotly.graph_objects as go
import statsmodels.api as sm
import pandas as pd
import numpy as np
import datetime

# data
np.random.seed(123)
numdays=20

X = (np.random.randint(low=-20, high=20, size=numdays).cumsum()+100).tolist()
Y = (np.random.randint(low=-20, high=20, size=numdays).cumsum()+100).tolist()

df = pd.DataFrame({'X': X, 'Y':Y})

# regression
df['bestfit'] = sm.OLS(df['Y'],sm.add_constant(df['X'])).fit().fittedvalues

# plotly figure setup
fig=go.Figure()
fig.add_trace(go.Scatter(name='X vs Y', x=df['X'], y=df['Y'].values, mode='markers'))
fig.add_trace(go.Scatter(name='line of best fit', x=X, y=df['bestfit'], mode='lines'))


# plotly figure layout
fig.update_layout(xaxis_title = 'X', yaxis_title = 'Y')

fig.show()
© www.soinside.com 2019 - 2024. All rights reserved.