10

Based on this code directly from plotly's tut page:

https://plot.ly/python/dropdowns/

Now, what if I want to change not just the chart type but rather the data source and its chart type? Is it possible?

EDIT:

I've played with this settings:

data1 = go.Surface(z=df.values.tolist(), colorscale='Viridis')
data2 = go.Heatmap(z=df.values.tolist())

buttons=list([   
            dict(
                args=[data1],
                label='3D Surface',
                method='restyle'
            ),
            dict(
                args=[data2],
                label='Heatmap',
                method='restyle'
            )             
        ])

However, the graphs are shown, but overlayed. And when I click any item in the dropdown menu, the graph is completely gone.

2
  • 1
    Doesn't the restyle method described on that page give an example how to change the data with py.iplot(dict(data=data, layout=layout), ...)? NB: you should include the code you have tried with embedded in your question. It is not enough to refer to an external site. Commented Sep 9, 2017 at 17:01
  • Just added some lines of my code... Commented Sep 10, 2017 at 9:50

1 Answer 1

19

I've found a solution myself, that is actually based on the tut:

import plotly
import plotly.graph_objs as go 
from datetime import datetime
import pandas_datareader as web

df = web.DataReader("aapl", 'google',
                    datetime(2015, 1, 1),
                    datetime(2016, 7, 1))

trace_high = go.Bar(    x=df.index,
                        y=df.High,
                        name='High')

trace_low = go.Scatter(x=df.index,
                       y=df.Low,
                       name='Low',
                       line=dict(color='#F06A6A'))

data = [trace_high, trace_low]


updatemenus = list([
    dict(active=-1,
         buttons=list([   
            dict(label = 'High',
                 method = 'update',
                 args = [{'visible': [True, False]},
                         {'title': 'Yahoo High'}]),

            dict(label = 'Low',
                 method = 'update',
                 args = [{'visible': [False, True]},
                         {'title': 'Yahoo Low'}])
        ]),
    )
])

layout = dict(title='Yahoo', showlegend=False,
              updatemenus=updatemenus)

fig = dict(data=data, layout=layout)

plotly.offline.plot(fig, auto_open=False, show_link=False)
Sign up to request clarification or add additional context in comments.

1 Comment

this does not work for me. A plot is generated but nothing happens when I try to select different plot (i.e., Low or High).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.