I have a Plotly graph with an updatemenu dropdown box to select different data series. I would like to create a key shortcut to select the next sequential data series from the dropdown box instead of manually selecting it.
Here is the code I have so far:
import pandas as pd
import numpy as np
import plotly.graph_objects as go
# Generate random data
num_rows = 100
num_cols = 4
df_stocks = pd.DataFrame(np.random.randn(num_rows, num_cols), columns=['Stock A', 'Stock B', 'Stock C', 'Stock D'])
# Create plot
fig = go.Figure()
for column in df_stocks.columns.to_list():
fig.add_trace(
go.Scatter(
x = df_stocks.index,
y = df_stocks[column],
name = column
)
)
# Add update menu
fig.update_layout(
updatemenus=[go.layout.Updatemenu(
active=0,
buttons=list(
[dict(label = 'All',
method = 'update',
args = [{'visible': [True, True, True, True]},
{'showlegend':True}]),
dict(label = 'Stock A',
method = 'update',
args = [{'visible': [True, False, False, False]},
{'showlegend':True}]),
dict(label = 'Stock B',
method = 'update',
args = [{'visible': [False, True, False, False]},
{'showlegend':True}]),
dict(label = 'Stock C',
method = 'update',
args = [{'visible': [False, False, True, False]},
{'showlegend':True}]),
dict(label = 'Stock D',
method = 'update',
args = [{'visible': [False, False, False, True]},
{'showlegend':True}]),
])
)
])
fig.show()
How can I modify this code to add a keypress event callback to the graph that will select the next sequential data series from the updatemenu dropdown box when a key is pressed?