See Image Here I am trying to make Plotly graphs for anomaly detection in time series using Isolation Forest. The problem is: only the plot of the last iteration in for loop apprears. Please help.
import plotly.express as px
import plotly.graph_objs as go
from plotly.subplots import make_subplots
start = 0.01
stop = 0.26
step = 0.05
float_range_array = np.arange(start, stop, step)
float_range_list = list(float_range_array)
fig = make_subplots(
rows=len(float_range_list), cols=1)
for x1,i in enumerate(float_range_list):
iforest1 = create_model('pca', fraction = i)
iforest_results = assign_model(iforest1)
fig = px.line( iforest_results, x="timestamp", y="value",
title='Principal Component Analysis: Fraction={}'.format(round(i,2)),template =
'plotly',labels={"timestamp": "Stay Date","value": "Number of Bookings"})
outlier_dates = iforest_results[iforest_results['Anomaly'] == 1].index
outlier_dates1=iforest_results.iloc[outlier_dates]['timestamp']
y_values = [iforest_results.loc[i]['value'] for i in outlier_dates]
fig.add_trace(go.Scatter(x=outlier_dates1, y=y_values, mode = 'markers',
name = 'Anomaly', marker=dict(color='red',size=10)),row=x1+1,col=1)
fig.show()
create_model()
andassign_model()
fig = make_subplots(rows=len(float_range_list), cols=1)
then in loop overwrite it withfig = px.line()
. Changed to use variable namefig_
for figure created within loopfig_
tofig
within loop