【发布时间】:2021-03-13 07:22:29
【问题描述】:
我需要为每个图表创建两个带有下拉菜单和标题的子图。 (并排比较)。另外,我想要一个共享的 y 轴。
就目前而言,我只有一个下拉菜单可以更改两个图表。
代码如下:(注意 df 由 2 列和 datetimeindex 组成)。
import plotly.offline as py
import plotly.graph_objs as go
from plotly.offline import init_notebook_mode, iplot, plot
from plotly import tools
labels = ["Vol", "R"]
fig = tools.make_subplots(rows=1, cols=2)
trace1 = go.Scatter(x=df.index,
y=df['Stock1'].rolling(window=12).std(),
mode='lines'
)
trace2 = go.Scatter(x=df.index,
y=df['Stock1'],
mode='lines'
)
fig.append_trace(trace1, 1, 1)
fig.append_trace(trace2, 1, 1)
trace1 = go.Scatter(x=df.index,
y=df['Stock2'].rolling(window=12).std(),
mode='lines'
)
trace2 = go.Scatter(x=df.index,
y=df['Stock2'],
mode='lines'
)
fig.append_trace(trace1, 1, 2)
fig.append_trace(trace2, 1, 2)
# Create buttons for drop down menu
buttons = []
for i, label in enumerate(labels):
visibility = [i==j for j in range(len(labels))]
button = dict(
label = label,
method = 'update',
args = [{'visible': visibility},
{'title': label}])
buttons.append(button)
updatemenus = list([
dict(active=-1,
x=-0.15,
buttons=buttons
)
])
fig['layout']['title'] = 'Title'
fig['layout']['showlegend'] = False
fig['layout']['updatemenus'] = updatemenus
iplot(fig, filename='dropdown')
【问题讨论】:
标签: button drop-down-menu plotly trace subplot