Date/Datetime not working properly in Plotly
I'm trying to create a stock portfolio dashboard using Dash and Plotly. I was able to successfully plot a line graph using the go.Scatter method and am now unsuccessfully trying to plot a similar graph with a callback function. Unfortunately, the x-axis does not seem to take kindly to the datetime format of my x-axis values but neither does it accept it when I recast the values as strings. I'm using exactly the same dataframe as the one that works when creating a simple line graph without the callback function. Here's a sample of my data:
Date | Value | Asset |
---|---|---|
2021-01 | 1.00 | a |
2021-02 | 1.10 | a |
2021-03 | 1.05 | a |
2021-04 | 1.12 | a |
And here's my code:
app = dash.Dash()
date_options = []
for date in df['Date'].unique():
date_options.append({'label':str(date),'value':date})
app.layout = html.Div([
dcc.Graph(id='graph'),
dcc.Dropdown(id='year-picker',options=date_options,value=df['Date'].min())
])
@app.callback(Output('graph', 'figure'),
[Input('date-picker', 'value')])
def update_figure(selected_date):
filtered_df = df[df['Date'] >= selected_date]
traces = []
for Asset in filtered_df['Asset'].unique():
df_by_asset = filtered_df[filtered_df['Asset'] == Asset]
traces.append(go.Scatter(
x=df_by_asset['Date'],
y=df_by_asset['Value'],
mode='markers',
marker={'size': 15},
name=Asset
))
return {
'data': traces,
'layout': go.Layout(
xaxis={'title': 'Date'},
yaxis={'title': 'Portfolio Value'},
hovermode='closest'
)
}
if __name__ == '__main__':
app.run_server()
I would really appreciate any help :)
from Recent Questions - Stack Overflow https://ift.tt/3zGxloL
https://ift.tt/eA8V8J
Comments
Post a Comment