简体   繁体   中英

How to change plots of several datasets with Plotly button?

I have three separated datasets of stocks/indexes. I can plot them one by one with PlotLy. But I want to create a button or dropdown menu that will allow me to switch the datasets from it.

Every dataset looks like this:

            open    high    low     close   volume  
Date                            
2015-08-06  5146.63 5149.93 5035.41 5056.44 2290950000  
2006-08-16  2127.06 2149.54 2120.11 2149.54 2474760000  
1995-06-19  909.90  922.09  909.84  922.09  407000000   
2009-10-19  2162.41 2180.11 2150.42 2176.32 1970440000  
1997-05-23  1377.73 1389.75 1377.73 1389.72 539440000   

Below is my code. But I don't know where to put the three datasets that will substitute "df". I was thinking of something like a list of datasets maybe but I am not used to working with python. I would appreciate some help.

I want the button or the drop-down menu to change between df = [msft_data, spy_500, nasdaq]

def plot_interactive_stock_data(title):

fig = go.Figure()

fig_high = go.Scatter(x=df.index, y=df['high'], name="high ($)",
                     line_color='deepskyblue')

fig_low = go.Scatter(x=df.index, y=df['low'], name="low ($)",
                     line_color='green')

fig_open = go.Scatter(x=df.index, y=df['open'], name="open ($)",
                     line_color='maroon')

fig_close - go.Scatter(x=df.index, y=df['close'], name="close ($)",
                     line_color='orange')

fig_volume = go.Scatter(x=df.index, y=df['volume'], name="volume",
                     line_color='brown')

fig.update_layout(title_text='{}'.format(title),
              xaxis_rangeslider_visible=True)


data = [fig_high, fig_low, fig_open, fig_close, fig_volume]

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

             dict(label = 'S&P 500',
                 method = 'update',
                 args = {'visible':  [False, True, False]}),

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

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

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

plotly.offline.plot(fig, auto_open=False, show_link=False)

I have found an answer which is not perfect but still, the code does what I wanted it to do. I have used information from plotly and from stackoverflow . Switching between datasets is very slow though. So I would appreciate it if somebody can show me a better solution.

Below is the code for the plot function:

def plot_data(df):    
fig = go.Figure()
fig.add_trace(go.Scatter(
                x=df.index,
                y=df['open'],
                name="Open",
                line_color='blueviolet',
                opacity=0.8))

fig.add_trace(go.Scatter(
                x=df.index,
                y=df['high'],
                name="High",
                line_color='green',
                opacity=0.8))

fig.add_trace(go.Scatter(
                x=df.index,
                y=df['low'],
                name="Low",
                line_color='red',
                opacity=0.8))

fig.add_trace(go.Scatter(
                x=df.index,
                y=df['close'],
                name="Close",
                line_color='darkkhaki',
                opacity=0.8))

fig.add_trace(go.Scatter(
                x=df.index,
                y=df['volume'],
                name="Volume",
                line_color='darkgoldenrod',
                opacity=0.8))

fig.add_trace(go.Scatter(
                x=df.index,
                y=df['dividends'],
                name="Dividends",
                line_color='brown',
                opacity=0.8))

fig.add_trace(go.Scatter(
                x=df.index,
                y=df['stock_splits'],
                name="Stock splits",
                line_color='brown',
                opacity=0.8))

fig.update_layout(title_text='Explore data',
              xaxis_rangeslider_visible=True)   
fig.show()

This is the code for the drop-down menu:

@interact(control=widgets.Dropdown(
options=["msft_data",
         "spy_500",
         "nasdaq"
         ],
description='Datasets'))

def plot_df(control):
    plt.figure(figsize = (14,8), linewidth=3, frameon = False)
    data = eval(control)
    plot_data(data)

You can see the image of my work too:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM