I have three separated datasets of stocks. I can plot them one by one with PlotLy. But I want to create a button or dropdown menu that will allow me to switch the datasets from it.
Below is my code. But I don’t know where to put the three datasets that will substitute “df”. I was thinking of something like a list of datasets maybe but I am not used to working with python. I would appreciate some help.
I want the button or the drop-down menu to change between df = [msft_data, spy_500, nasdaq]
def plot_interactive_stock_data(df, title):
fig = go.Figure()
fig_high = go.Scatter(x=df.index, y=df['high'], name="high ($)",
line_color='deepskyblue')
fig_low = go.Scatter(x=df.index, y=df['low'], name="low ($)",
line_color='green')
fig_open = go.Scatter(x=df.index, y=df['open'], name="open ($)",
line_color='maroon')
fig_close - go.Scatter(x=df.index, y=df['close'], name="close ($)",
line_color='orange')
fig_volume = go.Scatter(x=df.index, y=df['volume'], name="volume",
line_color='brown')
fig.update_layout(title_text='{}'.format(title),
xaxis_rangeslider_visible=True)
data = [fig_high, fig_low, fig_open, fig_close, fig_volume]
updatemenus = list([
dict(active = -1,
buttons = list([
dict(label = 'Microsoft',
method = 'update',
args = {'visible': [True, False, False]}),
dict(label = 'S&P 500',
method = 'update',
args = {'visible': [False, True, False]}),
dict(label = 'Nasdaq',
method = 'update',
args = {'visible': [False, False, True]})
]),
)
])
layout = dict(title=title, showlegend = False,
updatemenus = updatemenus)
fig = dict(data=data, layout=layout)
plotly.offline.plot(fig, auto_open=False, show_link=False)