I am trying to generate a Plotly figure with two sub plots and has dropdown menu. The default option works fine (Fig. 1) but when other values are selected the second plot disappears (Fig. 2). How to fix this issue such that for all options selected the second plot and legend appears?
Fig. 1
Fig. 2
MWE
import pandas as pd
import numpy as np
import plotly.graph_objects as go
from plotly.subplots import make_subplots
np.random.seed(42)
data = {
'Value1': np.random.randint(1, 1000, 100),
'Value2': np.random.randint(1, 1000, 100),
'Value3': np.random.randint(1, 1000, 100),
'Value4': np.random.randint(1, 1000, 100),
'Value5': np.random.randint(1, 1000, 100),
'Value6': np.random.randint(1, 1000, 100),
}
df = pd.DataFrame(data)
fig = make_subplots(rows = 2, cols = 1, vertical_spacing = 0.1, shared_xaxes = True)
for r in df.columns[2:-1]:
fig.add_trace(go.Scatter(x = df['Value1'], y = df[r], mode ='markers', marker_symbol = 'circle', visible = False), row = 1, col = 1, secondary_y = False)
fig.data[0].visible = True
dropdown_buttons = [{'label': column, 'method': 'update', 'args': [{'visible': [col == column for col in df.columns[2:-1]]}]} for column in df.columns[2:-1]]
fig.add_trace(go.Scatter(x = df['Value1'], y = df['Value2'], mode = 'markers', visible = True), row = 2, col = 1, secondary_y = False)
fig.update_xaxes(title_text = 'Value1', row = 2, col = 1)
fig.update_layout(updatemenus=[{'buttons': dropdown_buttons, 'direction': 'down', 'showactive': False}],
template = 'plotly')
fig.write_html('plot.html')
4
You have four traces total (three traces that alternate between value3, value4, value5
in the first subplot, and one trace for value2
that is always visible in the second subplot), so you need to add [True]
to your visible list to force the value2 to show in the second subplot.
The buttons for value3, value4, value5
should correspond to [True, False, False, True], [False, True, False, True], [False, False, True, True]
import pandas as pd
import numpy as np
import plotly.graph_objects as go
from plotly.subplots import make_subplots
np.random.seed(42)
data = {
'Value1': np.random.randint(1, 1000, 100),
'Value2': np.random.randint(1, 1000, 100),
'Value3': np.random.randint(1, 1000, 100),
'Value4': np.random.randint(1, 1000, 100),
'Value5': np.random.randint(1, 1000, 100),
'Value6': np.random.randint(1, 1000, 100),
}
df = pd.DataFrame(data)
fig = make_subplots(rows = 2, cols = 1, vertical_spacing = 0.1, shared_xaxes = True)
for r in df.columns[2:-1]:
fig.add_trace(go.Scatter(x = df['Value1'], y = df[r], name=r, mode ='markers', marker_symbol = 'circle', visible = False,), row = 1, col = 1, secondary_y = False)
fig.data[0].visible = True
fig.add_trace(go.Scatter(x = df['Value1'], y = df['Value2'], name='Value2', mode = 'markers', visible = False,), row = 2, col = 1, secondary_y = False)
## make the last trace visible when the plot loads initially
fig.data[-1].visible = True
## hardcode the visible list to have an additional [True]
dropdown_buttons = [{'label': column, 'method': 'update', 'args': [{'visible': [col == column for col in df.columns[2:-1]] + [True]}]} for column in df.columns[2:-1]]
fig.update_xaxes(title_text = 'Value1', row = 2, col = 1)
fig.update_layout(updatemenus=[{'buttons': dropdown_buttons, 'direction': 'down', 'showactive': False}],
template = 'plotly')
fig.show()