0

So I have stock prices of 5 companies. What I am trying to do is use a dropdown menu to select the company and create a line plot using data for the company selected from the dropdown menu. The code I have so far is as below :

import pandas as pd

intel = pd.read_csv(r'C:\Users\PC\Desktop\INTEL.csv')
nvidia = pd.read_csv(r'C:\Users\PC\Desktop\NVIDIA.csv')
amd = pd.read_csv(r'C:\Users\PC\Desktop\AMD.csv')
gigabyte = pd.read_csv(r'C:\Users\PC\Desktop\GIGABYTE.csv')
msi= pd.read_csv(r'C:\Users\PC\Desktop\MSI.csv')


stocks= widgets.Dropdown(
description='Stocks:   ',
options=['','Intel','Nvidia','AMD','Gigabyte','MSI']
)

name = stock.value


def response(name):
if name=='Intel' :
    Intel_chart = go.Line(x=Intel['Date'],y=Intel['Close'])
    Intel_data = [Intel_chart]
    iplot(Intel_data)

elif name=='Nvidia' :
    Nvidia_chart = go.Line(x=Nvidia['Date'],y=Nvidia['Close'])
    Nvidia_data = [Nvidia_chart]
    iplot(Nvidia_data)

elif name=='AMD' :
    AMD_chart = go.Line(x=AMD['Date'],y=AMD['Close'])
    AMD_data = [AMD_chart]
    iplot(AMD_data)

elif name=='Gigabyte' :
    Gigabyte_chart = go.Line(x=Gigabyte['Date'],y=Gigabyte['Close'])
    Gigabyte_data = [Gigabyte_chart]
    iplot(Gigabyte_data)

elif name=='MSI' :
    MSI_chart = go.Line(x=MSI['Date'],y=MSI['Close'])
    MSI_data = [MSI_chart]
    iplot(MSI_data)

However, the plots are not showing up when I choose the a value from the dropdown menu. What am i doing wrong? I have used Plotly before to plot the charts but I am new to ipywidgets and so any and all help will be appreciated.

Thanks in advance

Stevi G
  • 257
  • 1
  • 4
  • 13

2 Answers2

1

At the end of your code you need to make the dropdown widget call the response function when it's value changes.

stocks.observe(response, type='change', names=['value'])
ac24
  • 5,325
  • 1
  • 16
  • 31
  • Thanks man, but that didnt seem to do the trick. If you could edit the code that would be much better – Stevi G Apr 18 '19 at 17:27
0

Plotly has a widget mode which works well and I use it sometimes, together with Voila for dashboarding (https://github.com/QuantStack/voila) and with other ipywidgets including BQplot. (I prefer some Plotly plot types and I prefer the interactively of BQplot).

This code has not been tested but it could be something like this.

import ipywidgets as widgets
import plotly.graph_objs as go


data = [go.Scatter(
    x = Intel['date'],
    y = Intel['close'],
    mode = 'lines',
    name = 'Data',
    line = dict(
        color = ('rgb(205, 12, 24)'),
        width = 4)
)]

plotlyFig = go.Figure(data=data)
plotlyFig['layout'].update(height=800,
                           width=800,
                           title='',
                           xaxis=dict(
                               title='x Axis',
                               titlefont=dict(
                                   family='Arial',
                                   size=18,
                                   color='#7f7f7f'
                               )
                           ),
                           yaxis=dict(
                               title='y Axis',
                               titlefont=dict(
                                   family='Arial',
                                   size=18,
                                   color='#7f7f7f'
                               )
                           )
                           )

PlotlyWidget = go.FigureWidget(plotlyFig)   # This creates a Plotly Ipywidget.
PlotlyWidgetData = PlotlyWidget.data[0]        

def response(change):
    name = stocks.value

    if name=='Intel' :
        PlotlyWidgetData.x = Intel['date']
        PlotlyWidgetData.y = Intel['close']
    elif name=='Nvidia' :
        PlotlyWidgetData.x = Nvidia['date']
        PlotlyWidgetData.y = Nvidia['close']
    elif name=='AMD' :
        PlotlyWidgetData.x = AMD['date']
        PlotlyWidgetData.y = AMD['close']
    elif name=='Gigabyte' :
        PlotlyWidgetData.x = Gigabyte['date']
        PlotlyWidgetData.y = Gigabyte['close']
    elif name=='Asus' :
        PlotlyWidgetData.x = Asus['date']
        PlotlyWidgetData.y = Asus['close']
    elif name=='MSI' :
        PlotlyWidgetData.x = MSI['date']
        PlotlyWidgetData.y = MSI['close']
    elif name=='EVGA' :
        PlotlyWidgetData.x = EVGA['date']
        PlotlyWidgetData.y = EVGA['close']  

stocks = widgets.Dropdown(
    description='Stocks:   ',
    options=['','Intel','Nvidia','AMD','Gigabyte','Asus','MSI','EVGA'])

stocks.observe(response, type='change', names=['value'])

stocks
DougR
  • 3,196
  • 1
  • 28
  • 29
  • Sorry for the late response. Anyways I tried it but it doesnt seem to work. I get the dropdown menu but the figure doesnt plot. – Stevi G Apr 26 '19 at 16:53
  • Could you provide your attempt with the data (or cut doen version ). It would be easier for me to debug it with actual data in whatever format you use. – DougR Apr 28 '19 at 11:21
  • Well my attempt is the same as the one with the code in the original post. I am taking the data directly from yahoo finance and storing it in dataframes. However, for your convinence I have converted all the dataframes to csv files and uploaded them [HERE](https://ufile.io/6xnsjl4w) so for you to see. Also would like to point out that in the case of your above provided answer, even the initial figure wasnt being plotted even though it should be plotted. I used copy and paste to test your solution as is without making any changes. Thanks :) – Stevi G Apr 29 '19 at 09:19
  • try this: ```stocks.observe(response)```. That may work. But I can't test it because I don't have time to create the data dictionaries. I will have a look at it for you if you provide a near working example with sample data defined in the Python. – DougR May 01 '19 at 09:23
  • Tried it but it didnt work. I updated my original post with the data dictionaries. I have already uploaded the data so all that you have to do is change the path to the csv files :). Thanks – Stevi G May 02 '19 at 13:46