Hi
I am working with Bar chart with Slider. The developed code working good but initially bar chart not showing as expected. It shows stacked bar chart instead of basic bar chart for the active step which was set. once moved the slider back and forth, the graph refreshes well. please help me where I am missing. thank you!
Sample Data:
Time,City,TempChange
09:00,Delhi,0.93
09:00,Masco,0.14
09:00,London,0.02
10:00,Delhi,0.52
10:00,Masco,0.29
10:00,London,0.49
11:00,Delhi,0.93
11:00,Masco,0.31
11:00,London,0.91
Code:
import pandas as pd
import plotly.graph_objs as go
from plotly.subplots import make_subplots
df = pd.read_csv('data.csv')
fig = make_subplots(rows=1, cols=1)
step_list = df.Time.unique() # get all the uniq times for slider steps
active_step = len(step_list) - 1 # make latest time active
for step in step_list:
df2 = (df[ df['Time'] == step ]).sort_values('TempChange')
fig.add_trace( go.Bar(x = df2['City'], y = df2['TempChange'], marker = dict(color = df2['TempChange'])))
fig.data[active_step].visible = True
steps = []
for i in range(len(fig.data)):
step = dict( method="update", label= step_list[i], args=[{"visible": [False] * len(fig.data)} ])
step["args"][0]["visible"][i] = True # Toggle i'th trace to "visible"
steps.append(step)
sliders = [ dict(active=active_step, pad={"t": 50}, currentvalue={"prefix": "Active: "}, steps=steps )]
fig.update_layout(sliders=sliders)
import dash
import dash_core_components as dcc
import dash_html_components as html
app = dash.Dash()
app.layout = html.Div([dcc.Graph(figure=fig)])
app.run_server(debug=True)
Initial Chart:
Expected Chart: