当选择非默认选项时,带有子图和下拉菜单的绘图图会隐藏第二个图

问题描述 投票:0回答:1

我正在尝试生成一个带有两个子图并具有下拉菜单的 Plotly 图形。默认选项工作正常(图1),但是当选择其他值时,第二个图就会消失(图2)。如何解决此问题,以便对于选择的所有选项,出现第二个图和图例?

Fig. 1

图。 1

Fig. 2

图。 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')
python plotly
1个回答
0
投票

您总共有 4 个迹线(第一个子图中在

value3, value4, value5
之间交替的三个迹线,以及在第二个子图中始终可见的
value2
的迹线),因此您需要将
[True]
添加到可见列表中强制 value2 显示在第二个子图中。

value3, value4, value5
的按钮应对应于
[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()

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.