You haven't provided a working code snippet with a sample of your data, so I'm going to base my suggestion on my earlier answer Plotly: How to plot a regression line using plotly?. If your figure is limeted to two series as in your example you can:
1. retrieve x-values from one of the series using xVals = fig.data[0]['x']
, and
2. organize all points for your regression line and observations markers using a dict errors = {}
, and
3. populate that dict using:
for d in fig.data:
errors[d['mode']]=d['y']
4. Then you can add a line shape for distance between your line and markers (your errors) using:
for i, x in enumerate(xVals):
shapes.append(go.layout.Shape(type="line", [...])
Result:
Complete code:
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')
# retrieve x-values from one of the series
xVals = fig.data[0]['x']
errors = {} # container for prediction errors
# organize data for errors in a dict
for d in fig.data:
errors[d['mode']]=d['y']
shapes = [] # container for shapes
# make a line shape for each error == distance between each marker and line points
for i, x in enumerate(xVals):
shapes.append(go.layout.Shape(type="line",
x0=x,
y0=errors['markers'][i],
x1=x,
y1=errors['lines'][i],
line=dict(
#color=np.random.choice(colors,1)[0],
color = 'black',
width=1),
opacity=0.5,
layer="above")
)
# include shapes in layout
fig.update_layout(shapes=shapes)
fig.show()
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…