I need to create two subplots with a dropdown menu and title for each graph. (side-by-side comparison). In addition, I 'd like to have a shared y-axis.
As for now, I have only one dropdown menu that change both graphs.
The code is following: (note that a df consists of 2 columns and 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')
