def print_dataframe(df, n_rows=10, n_columns=3):
    missing_val_str = '...'
    config = helper.generate_config()

    formatted_df = df.iloc[:n_rows, :n_columns]
    formatted_df = formatted_df.applymap('{:.3f}'.format)

    if len(df.columns) > n_columns:
        formatted_df[missing_val_str] = [missing_val_str]*len(formatted_df.index)
    if len(df.index) > n_rows:
        formatted_df.loc[missing_val_str] = [missing_val_str]*len(formatted_df.columns)

    trace = go.Table(
        type='table',
        columnwidth=[1, 3],
        header={
            'values': [''] + list(formatted_df.columns.values),
            'line': {'color': helper.color_scheme['df_line']},
            'fill': {'color': helper.color_scheme['df_header']},
            'font': {'size': 13}},
        cells={
            'values': formatted_df.reset_index().values.T,
            'line': {'color': helper.color_scheme['df_line']},
            'fill': {'color': [helper.color_scheme['df_header'], helper.color_scheme['df_value']]},
            'font': {'size': 13}})

    offline_py.iplot([trace], config=config)
Пример #2
0
def plot_re_x(n):
    
    def g(a):
        fn, tp = a
        return go.Scatter(
            x = X,
            y = [sin(fn, tp, n, x) for x in X],
            mode = 'lines+markers',
            name = fn + ' (' + tp + ')'
        )
    
    data = list(map(g, product(['sin1_ltr', 'sin1_rtl', 'sin2'], ['float', 'double', 'longdouble'])))
    
    layout = go.Layout(
        title='Błąd względny (%d wyrazów)' % (n,),
        width=960,
        height=720,
        #hovermode='closest',
        xaxis=dict(title='x',),
        yaxis=dict(title='Błąd względny', type='log', autorange=True),
    )
    
    fig = go.Figure(data=data, layout=layout)
    
    pl.iplot(fig, show_link=False)
 def resistancePlot(self):
     """This just allows grouping by index (or die, etc)"""
     scatter = [go.Scatter(
             x = [float(line['area']) for line in self.data if line['index']==die],
             y = [1/float(line['resistance']) for line in self.data if line['index']==die],
             mode = 'markers',
             name = 'Data'
         ) for die in np.unique([row['index'] for row in self.data])]
     
     # linear fit based on all data
     x = [float(line['area']) for line in self.data]
     y = [1/float(line['resistance']) for line in self.data]
     xfit = np.unique([0]+x)
     m, b = self.fitData()
     yfit = [m*xi+b for xi in xfit]
     print("m = {}  b = {}  R_sub = {}".format(m,b,1/b))
     fit = go.Scatter(
             x = xfit,
             y = yfit,
             mode = 'lines',
             name = 'Fit'
         )
     fig = dict(data = scatter+[fit], 
                layout = {'title':'',
                         'xaxis':{'title':'Junction Area [um^2]'},
                         'yaxis':{'title':'1/Room Temperature Resistance [$1/\Omega$]'}
                         }
               )
     iplot(fig)
def plot_signal_returns(prices, signal_return_list, titles):
    config = helper.generate_config()
    layout = go.Layout(
        yaxis2={
            'title': 'Signal Returns',
            'titlefont': {'color': helper.color_scheme['y_axis_2_text_color']},
            'tickfont': {'color': helper.color_scheme['y_axis_2_text_color']},
            'overlaying': 'y',
            'side': 'right'})
    colors = Color(helper.color_scheme['low_value'])\
        .range_to(Color(helper.color_scheme['high_value']), len(signal_return_list))

    stock_trace = _generate_stock_trace(prices)
    for (signal_return, signal, lookahead_days), color, title in zip(signal_return_list, colors, titles):
        non_zero_signals = signal_return[signal_return != 0]
        signal_return_trace = go.Scatter(
                x=non_zero_signals.index,
                y=non_zero_signals,
                name='{} Day Lookahead'.format(lookahead_days),
                line={'color': str(color)},
                yaxis='y2')

        buy_annotations = _generate_buy_annotations(prices, signal)
        sell_annotations = _generate_sell_annotations(prices, signal)
        layout['title'] = title
        layout['annotations'] = buy_annotations + sell_annotations

        offline_py.iplot({'data': [stock_trace, signal_return_trace], 'layout': layout}, config=config)
Пример #5
0
def fibplot(xsr,ysr,xsi,ysi):
    frames = []
    for i in range(len(xsr)+2):
        frames.append({'data': [{'x': xsr[:i], 'y': ysr[:i]}, {'x':xsi[:i], 'y':ysi[:i]}]})

    figure = {
        'data': [
            {
                'x': xsr,
                'y': ysr,
                'mode':'lines',
                'name': 'Recursive Fib'
            }, {
                'x': xsi,
                'y': ysi,
                'mode':'lines',
                'name': 'Imperative Fib'
            }
        ],
        'layout': {'title': 'Fibonacci',
                   'updatemenus': [{
                       'buttons': [
                           {'args': [None],
                            'label': 'Play',
                            'method': 'animate'}
                   ],
                   'pad': {'r': 10, 't': 87},
                   'showactive': False,
                   'type': 'buttons'
                    }]},
        'frames': frames,
    }

    iplot(figure, filename='Fibonacci')
def plot_signal_to_normal_histograms(signal_list, title, subplot_titles):
    assert len(signal_list) == len(subplot_titles)

    signal_series_list = [signal.stack() for signal in signal_list]
    all_values = pd.concat(signal_series_list)
    x_range = [all_values.min(), all_values.max()]
    y_range = [0, 1500]
    config = helper.generate_config()

    fig = py.tools.make_subplots(rows=1, cols=len(signal_series_list), subplot_titles=subplot_titles, print_grid=False)
    fig['layout'].update(title=title)

    for series_i, signal_series in enumerate(signal_series_list, 1):
        filtered_series = signal_series[signal_series != 0].dropna()
        filtered_series_trace = go.Histogram(
            x=filtered_series,
            marker={'color': helper.color_scheme['low_value']},
            name='Signal Return Distribution',
            showlegend=False)
        normal_trace = go.Histogram(
            x=np.random.normal(np.mean(filtered_series), np.std(filtered_series), len(filtered_series)),
            marker={'color': helper.color_scheme['shadow']},
            name='Normal Distribution',
            showlegend=False)
        fig.append_trace(filtered_series_trace, 1, series_i)
        fig.append_trace(normal_trace, 1, series_i)
        fig['layout']['xaxis{}'.format(series_i)].update(range=x_range)
        fig['layout']['yaxis{}'.format(series_i)].update(range=y_range)

    # Show legened
    fig['data'][0]['showlegend'] = True
    fig['data'][1]['showlegend'] = True

    offline_py.iplot(fig, config=config)
Пример #7
0
def old_show(explanation, selector=None, index_map=None):
    from plotly.offline import iplot, init_notebook_mode

    init_notebook_mode(connected=True)
    # if not show.imported:
    #     show.imported = True

    if isinstance(selector, str):
        if index_map is None:
            print(
                "If selector is a string, a list or dictionary index_map must be passed."
            )
        if isinstance(index_map, list):
            selector = index_map.index(selector)
        elif isinstance(index_map, dict):
            selector = index_map[selector]
        else:
            print("Not supported index_feature_map type. Use list or dictionary.")
            return None
    elif isinstance(selector, int):
        selector = selector
    elif selector is None:
        selector = None
    else:
        print("Argument 'selector' must be an int, string, or None.")
        return None

    fig = explanation.visualize(selector)
    if fig is not None:
        iplot(fig)
    else:
        print("No overall graph for this explanation. Pass in a selector.")
Пример #8
0
def display(m,wireframe=True,smooth=True,data=None):
    """ The display function shows an interactive presentation of the Manifold, m, inside
        a Jupyter Notebook. wireframe=True means that a wireframe view of the mesh is
        superimposed on the 3D model. If smooth=True, the mesh is rendered with vertex
        normals. Otherwise, the mesh is rendered with face normals. If data=None, the
        mesh is shown in a light grey color. If data contains an array of scalar values
        per vertex, these are mapped to colors used to color the mesh."""
    xyz = array([ p for p in m.positions()])
    m_tri = gel.Manifold(m)
    gel.triangulate(m_tri)
    ijk = array([[ idx for idx in m_tri.circulate_face(f,'v')] for f in m_tri.faces()])
    mesh = go.Mesh3d(x=xyz[:,0],y=xyz[:,1],z=xyz[:,2],
            i=ijk[:,0],j=ijk[:,1],k=ijk[:,2],color='#dddddd',flatshading=not smooth)
    if data is not None:
        mesh['intensity'] = data
    mesh_data = [mesh]
    if wireframe:
        pos = m.positions()
        xyze = []
        for h in m.halfedges():
            if h < m.opposite_halfedge(h):
                p0 = pos[m.incident_vertex(m.opposite_halfedge(h))]
                p1 = pos[m.incident_vertex(h)]
                xyze.append(array(p0))
                xyze.append(array(p1))
                xyze.append(array([None, None, None]))
        xyze = array(xyze)
        trace1=go.Scatter3d(x=xyze[:,0],y=xyze[:,1],z=xyze[:,2],
                   mode='lines',
                   line=go.Line(color='rgb(125,0,0)', width=1),
                   hoverinfo='none')
        mesh_data += [trace1]
    lyt = go.Layout(scene=go.Scene(aspectmode='data'))
    fig = go.Figure(data=mesh_data,layout=lyt)
    py.iplot(fig)
Пример #9
0
    def render(self, df, encoding, output):
        if encoding.x is None or encoding.y is None:
            with output:
                print("\n\n\nPlease select an X and Y axis.")
                return

        try:
            data = self._get_data(df, encoding)
        except InvalidEncodingError as err:
            with output:
                print("\n\n\n{}".format(err))
                return

        type_x_axis = self._get_type_axis(encoding.logarithmic_x_axis)
        type_y_axis = self._get_type_axis(encoding.logarithmic_y_axis)

        layout = Layout(
            xaxis=dict(type=type_x_axis, rangemode="tozero", title=encoding.x),
            yaxis=dict(type=type_y_axis, rangemode="tozero", title=encoding.y),
        )

        with output:
            try:
                fig = Figure(data=Data(data), layout=layout)
                iplot(fig, show_link=False)
            except TypeError:
                print(
                    "\n\n\nPlease select another set of X and Y axis, because the type of the current axis do\n"
                    "not support aggregation over it."
                )
Пример #10
0
 def show(self,save=False):
     imagestr=None
     data = self.traces
     fig = go.Figure(data = data, layout = go.Layout(**self.layout))
     if save:
         imagestr = 'avg'
     py.iplot(fig, image = imagestr)
Пример #11
0
def pretty_table(df, outfile=None):
    """
    Display pandas dataframe as a nicely-formated HTML

    Parameters
    ----------
    outfile: filepath str
        If provided, output to an HTML file at provided location

    Example
    -------
    import pandas as pd
    
    animals = pd.DataFrame([
              ['cat',10, 'housepet'],
              ['dog',20,'housepet'],
              ['fish',5,'housepet'],
              ['cat',20,'zooanimal'],
              ['dog',50,'zooanimal'],
              ['fish',20,'zooanimal'],], columns=['animal','value','group'])

    pretty_table(animals)

    """
    
    table = FF.create_table(df)
    ol.iplot(table, show_link=False)

    # write figure to HTML file
    if outfile:
        print('Exporting copy of figure to %s...' % outfile)
        ol.plot(table, auto_open=False, filename=outfile)
Пример #12
0
def dist_plot(df, 
              groupby=None,
              val=None,
              bin_size=1, 
              title=None,
              show_hist=True,
              show_kde=True, 
              show_rug=True, 
              show_legend=True, 
              figsize=None,
              outfile=None,
              xlabel=None,
              ylabel=None):
    
    if groupby is None:
        fig = FF.create_distplot([df[c] for c in df.columns], 
                                     df.columns.values.tolist(), 
                                     bin_size=bin_size,
                                     show_rug=show_rug,
                                     show_curve=show_kde)
    else:
        groups = sorted(df[groupby].unique().tolist(), reverse=True)
        data = []
        if val is None:
            val = df.columns.drop(groupby)[0]  # choose first non-groupby column
            
        for group in groups:
            mask = df[groupby] == group
            data.append(df.loc[mask, val])
            
        fig = FF.create_distplot(data, 
                                 groups, 
                                 bin_size=bin_size,
                                 show_hist=show_hist,
                                 show_rug=show_rug,
                                 show_curve=show_kde)
        
    fig['layout'].update(showlegend=show_legend)
    
    if title:
        fig['layout'].update(title=title)

    if xlabel:
        fig['layout'].update(xaxis=go.XAxis(title=xlabel))

    if ylabel:
        fig['layout'].update(yaxis=go.YAxis(title=ylabel))


        
    if figsize and len(figsize) == 2:
        fig['layout'].update(width=figsize[0])
        fig['layout'].update(height=figsize[1])
        
    ol.iplot(fig, show_link=False)
    
    # write figure to HTML file
    if outfile:
        print('Exporting copy of figure to %s...' % outfile)
        ol.plot(fig, auto_open=False, filename=outfile)
Пример #13
0
 def resistancePlot(self):
     """Old version"""
     x = [1/float(line['area']) for line in self.data]
     y = [float(line['resistance']) for line in self.data]
     scatter = go.Scatter(
             x = x,
             y = y,
             mode = 'markers',
             name = 'Data'
         )
     xfit = np.unique(x)
     m, b = self.fitData()
     yfit = [m*xi+b for xi in xfit]
     fit = go.Scatter(
             x = xfit,
             y = yfit,
             mode = 'lines',
             name = 'Fit'
         )
     fig = dict(data = [scatter,fit], 
                layout = {'title':'',
                         'xaxis':{'title':'1/JJ Area [1/um^2]'},
                         'yaxis':{'title':'Room Temperature Resistance [\Omega]'}
                         }
               )
     iplot(fig)
def plot_stock(prices, title):
    config = helper.generate_config()
    layout = go.Layout(title=title)

    stock_trace = _generate_stock_trace(prices)

    offline_py.iplot({'data': [stock_trace], 'layout': layout}, config=config)
def plot_returns(returns, title):
    config = helper.generate_config()
    layout = go.Layout(title=title)

    traces = _generate_traces([
        ('Returns', returns, helper.color_scheme['major_line'])])

    offline_py.iplot({'data': traces, 'layout': layout}, config=config)
Пример #16
0
def save_plot(fig, file_name=None):
    try:
        from plotly.offline import iplot, init_notebook_mode
        init_notebook_mode(connected=True)
        iplot(fig, filename=file_name)
    except:
        from plotly.offline import plot
        plot(fig, auto_open=False, filename=file_name)
def plot_resampled_prices(df_resampled, df, title):
    config = helper.generate_config()
    layout = go.Layout(title=title)

    traces = _generate_traces([
        ('Monthly Close', df_resampled, helper.color_scheme['major_line']),
        ('Close', df, helper.color_scheme['minor_line'])])

    offline_py.iplot({'data': traces, 'layout': layout}, config=config)
Пример #18
0
    def plot_loss_reward(total_losses, total_rewards):

    figure = tools.make_subplots(rows=1, cols=2, subplot_titles=('loss', 'reward'), print_grid=False)
    figure.append_trace(Scatter(y=total_losses, mode='lines', line=dict(color='skyblue')), 1, 1)
    figure.append_trace(Scatter(y=total_rewards, mode='lines', line=dict(color='orange')), 1, 2)
    figure['layout']['xaxis1'].update(title='epoch')
    figure['layout']['xaxis2'].update(title='epoch')
    figure['layout'].update(height=400, width=900, showlegend=False)
    iplot(figure)
Пример #19
0
 def _plot(self, grobby, filename):
     global notebook_mode, notebook_mode_init
     if notebook_mode:
         if not notebook_mode_init:
             ply.init_notebook_mode()
             notebook_mode_init = True
         ply.iplot(grobby)
     else:
         ply.plot(grobby, filename=filename)
def plot_shifted_returns(df_shited, df, title):
    config = helper.generate_config()
    layout = go.Layout(title=title)

    traces = _generate_traces([
        ('Shifted Returns', df_shited, helper.color_scheme['major_line']),
        ('Returns', df, helper.color_scheme['minor_line'])])

    offline_py.iplot({'data': traces, 'layout': layout}, config=config)
def plot_scatter(xvals, yvals):
  data = [
    go.Scatter(
      x=xvals,
      y=yvals,
      mode = 'markers'
    )
  ]
  py.iplot(data)
Пример #22
0
def plot_cost_and_trace(X, Y, trace, low=-10, high=10, size=100):
    gr = calculate_cost_grid(X, Y, low, high, size)
    lsp = np.linspace(low, high, size)

    contour = go.Contour(z = np.log(gr), x = lsp, y = lsp, colorscale='Jet', contours={'coloring': 'heatmap'})

    descent = go.Scatter(x = trace[:, 1], y = trace[:, 0], mode='markers')

    iplot(go.Figure(data=[contour, descent], layout=go.Layout(hovermode='closest', width=600, height=600)))
Пример #23
0
def plot_color(color):
    import plotly.graph_objs as go
    from plotly import offline

    layout = go.Layout(width=50, height=50, margin=dict(l=0, r=0, t=0, b=0))
    fig = go.Figure(
        data=[go.Scatter(x=[1, 2], y=[2, 2], marker=dict(color=color), fill='tozeroy')],
        layout=layout
    )
    offline.iplot(fig, link_text='', show_link=False)
def plot_by_metro(df, trend_name, plot=True):
    #generates graph for metro volume/percentages
    df = cutoff(metro_analyses(df))
    colors = ['#194769','#A0C1B8', '#E4D183', '#F2855E']
    plot_config = {'kwargs': {'colors': colors}, 
                   'df': df}
    if not plot:
        return plot_config

    fig = pf.plot_by_metro(df=df, trend=trend_name, colors=colors)
    iplot(fig, filename ='stacked-bar')
def plot_by_income(df, trend_name, plot=True):
    #generates graph for income volume/percentages
    df = cutoff(income_analyses(df))
    colors = ['rgb(0, 0, 102)', 'rgb(127, 166, 238)']
    plot_config = {'kwargs': {'colors': colors}, 
                   'df': df}
    if not plot:
        return plot_config

    fig = pf.plot_by_income(df=df, trend=trend_name, colors=colors)
    iplot(fig, filename ='stacked-bar')
def plot_signal(price, signal, title):
    config = helper.generate_config()
    buy_annotations = _generate_buy_annotations(price, signal)
    sell_annotations = _generate_sell_annotations(price, signal)
    layout = go.Layout(
        title=title,
        annotations=buy_annotations + sell_annotations)

    stock_trace = _generate_stock_trace(price)

    offline_py.iplot({'data': [stock_trace], 'layout': layout}, config=config)
def plot_by_grade(df, trend_name, plot=True):
    #generates graph for grade volume/percentages
    df = cutoff(grade_analyses(df))
    colors = ['#FDA403','#FFD6A0', '#404B69', '#7FA99B']
    plot_config = {'kwargs': {'colors': colors}, 
                   'df': df}
    if not plot:
        return plot_config

    fig = pf.plot_by_grade(df=df, trend=trend_name, colors=colors)
    iplot(fig, filename ='stacked-bar')
def percent_by_subject(df, trend_name, plot=True):
    #generates graph for subject percentages
    analysis_df = cutoff(subject_analyses(df))
    df = edit_cols(df, analysis_df)
    df = bar_switch(df)
    colors = ['#84B9EF','#FBE4C9', '#FF5D5D', '#952E4B' , '#FFFF9D', '#F38181', '#F12D2D', '#660000' ]
    plot_config = {'kwargs': {'colors': colors}, 
                   'df': df}
    if not plot:
        return plot_config

    fig = pf.percent_by_subject(df=df, trend=trend_name, colors=colors)
    iplot(fig, filename ='stacked-bar')
def rfAvgAcc(rfModel, XTest, yTest):
    preds = []
    avgPred = []
    df = []

    for i,tree in enumerate(rfModel.estimators_):
        predTree = tree.predict(XTest)
        accTree  = round(metrics.accuracy_score(yTest, predTree),2)
        preds.append(accTree)
        if i==0:
            df = predTree
        else:
            df = np.vstack((df,predTree))

    for j in np.arange(df.shape[0]):
        j=j+1
        mv = []
        for i in np.arange(df.shape[1]):
            (values,counts) = np.unique(df[:j,i],return_counts=True)
            ind=np.argmax(counts)
            mv.append(values[ind].astype(int))
        avgPred.append(metrics.accuracy_score(yTest, mv))

    trace = go.Scatter(
        y=avgPred,
        x=np.arange(df.shape[0]),
        mode='markers+lines',
        name = "Ensemble accuracy trend"
    )

    layout = go.Layout(
        title = "Ensemble accuracy over increasing number of trees",
        xaxis = dict(title = "Number of trees", nticks = 15),
        yaxis = dict(title = "Accuracy"),
        showlegend=False,
        autosize=False,
        width=1000,
        height=500,
        margin=Margin(
            l=70,
            r=50,
            b=100,
            t=50,
            pad=4
        ),
    )

    data = [trace]

    fig = dict(data=data, layout=layout)
    iplot(fig)
Пример #30
0
 def show(self):
     annotations = []
     pitch = self.pitch
     center_die = self.center_die
     wafer_diameter = self.wafer_diameter
     sputter_diameter = self.sputter_diameter
     trace = go.Heatmap(
             z = self.dieMapData,
             x = [c for c in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'],
             # y = [str(self.ndie-i)+'a' for i in range(self.ndie)]
         )
     if self.title is None: 
         self.title = 'Wafer Die Map'
     fig = dict(data = [trace], 
                layout = {'title': self.title,
                          'width':500,
                          'height':500,
                          'autosize':False,
                          'xaxis': {'ticks':'', 'side':'top'},
                          'yaxis':{'autorange':'reversed'},
                          'annotations':annotations,
                          'shapes': [
                             # outer circle
                             {
                                 'type': 'circle',
                                 'xref': 'x',
                                 'yref': 'y',
                                 'x0': center_die-wafer_diameter/2./pitch+1,
                                 'y0': center_die-wafer_diameter/2./pitch+1,
                                 'x1': center_die+wafer_diameter/2./pitch+1,
                                 'y1': center_die+wafer_diameter/2./pitch+1,
                                 'line': {
                                     'color': 'rgba(50, 171, 96, 1)',
                                 },
                             },
                             # middle circle
                             {
                                 'type': 'circle',
                                 'xref': 'x',
                                 'yref': 'y',
                                 'x0': center_die-sputter_diameter/2./pitch+1,
                                 'y0': center_die-sputter_diameter/2./pitch+1,
                                 'x1': center_die+sputter_diameter/2./pitch+1,
                                 'y1': center_die+sputter_diameter/2./pitch+1,
                                 'line': {
                                     'color': 'rgba(171, 50, 96, 1)',
                                 },
                             },]
                         }
               )
     iplot(fig)
Пример #31
0
plt.xticks(rotation=90)

# # Investigate Global Happiness Ranking

# In[ ]:

data = dict(type='choropleth',
            locations=df['Country'],
            locationmode='country names',
            z=df['Happiness Rank'],
            text=df['Country'],
            colorbar={'title': 'Happiness'})
layout = dict(title='Global Happiness',
              geo=dict(showframe=False, projection={'type': 'Mercator'}))
choromap3 = go.Figure(data=[data], layout=layout)
iplot(choromap3)

# # Setting up Linear Model to Predict Happiness

# In[ ]:

y = df['Happiness Score']
X = df.drop(['Happiness Score', 'Happiness Rank', 'Country', 'Region'], axis=1)

# In[ ]:

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X,
                                                    y,
                                                    test_size=0.3,
Пример #32
0
    def plot(self, symbol=None, engine='plotly', notebook=False):
        if engine == 'plotly':
            if type(symbol) == str:
                df = pd.DataFrame(self.latest_bar_dict[symbol])
                df.set_index('date', inplace=True)
                df.index = pd.DatetimeIndex(df.index)
                p_symbol = go.Scatter(x=df.index,
                                      y=df.close,
                                      xaxis='x3',
                                      yaxis='y3',
                                      name=symbol)
                p_volume = go.Bar(x=df.index,
                                  y=df['volume'],
                                  xaxis='x3',
                                  yaxis='y5',
                                  opacity=0.5,
                                  name='volume')
                self.data.append(p_symbol)
                self.data.append(p_volume)

            if type(symbol) == list:
                for i in symbol:
                    df = pd.DataFrame(self.latest_bar_dict[i])
                    df.set_index('date', inplace=True)
                    df.index = pd.DatetimeIndex(df.index)
                    p_symbol = go.Scatter(x=df.index,
                                          y=df.close,
                                          xaxis='x3',
                                          yaxis='y3',
                                          name=i)
                    p_volume = go.Bar(x=df.index,
                                      y=df['volume'],
                                      xaxis='x3',
                                      yaxis='y5',
                                      opacity=0.5,
                                      name=i + 'volume')
                    self.data.append(p_symbol)
                    self.data.append(p_volume)

            for i in self.holdings:
                p_holdings = go.Scatter(x=self.holdings.index,
                                        y=self.holdings[i],
                                        xaxis='x2',
                                        yaxis='y2',
                                        name=i)
                self.data.append(p_holdings)

            p_returns = go.Scatter(x=self.enquity_curve.index,
                                   y=self.enquity_curve.returns,
                                   xaxis='x4',
                                   yaxis='y4',
                                   name='returns')
            self.data.append(p_returns)

            layout = go.Layout(xaxis2=dict(
                domain=[0, 1],
                anchor='y2',
            ),
                               xaxis3=dict(domain=[0, 1], anchor='y3'),
                               xaxis4=dict(domain=[0, 1], anchor='y4'),
                               yaxis2=dict(domain=[0, 0.2], ),
                               yaxis3=dict(domain=[0.2, 0.8]),
                               yaxis4=dict(domain=[0.8, 1], ),
                               yaxis5=dict(
                                   domain=[0.2, 0.8],
                                   side='right',
                                   range=[0, 10000000],
                                   overlaying='y3',
                                   tickvals=[0, 1000000, 2000000, 2500000],
                                   showgrid=False))
            fig = go.Figure(data=self.data, layout=layout)
            if notebook:
                import plotly
                plotly.offline.init_notebook_mode()
                py.iplot(fig, filename='testplot', validate=False)
            else:
                py.plot(fig, filename='testplot', validate=False)
data = [
    go.Scatter(x=bos_den.Weekend_Date, y=bos_den.Average_Price, name='Denver'),
    go.Scatter(x=bos_lax.Weekend_Date,
               y=bos_lax.Average_Price,
               name='Los Angeles'),
    go.Scatter(x=bos_sfo.Weekend_Date,
               y=bos_sfo.Average_Price,
               name='San Francisco'),
    go.Scatter(x=bos_iah.Weekend_Date, y=bos_iah.Average_Price,
               name='Houston'),
    go.Scatter(x=bos_phx.Weekend_Date, y=bos_phx.Average_Price, name='Phoenix')
]

layout = go.Layout(
    title="Weekend Return Trip Prices for major US cities from Boston",
    xaxis=dict(title="Weekend dates"),
    yaxis=dict(title="Return Ticket price in USD"),
)

figure = go.Figure(data=data, layout=layout)

offline.iplot(
    figure,
    filename='Weekend Return Trip Prices for major US cities from Boston')

# - From this graph I can plan my weekend trips to each of these cities and save on cost.
# - For instance I will plan my trip to Denver before 28 May 2017 to be able to save on the high prices at any later date.
# - Similarly for San Francisco 11 June 2017 is the cheapest date to travel.

# In[ ]:
stacked = py.get_figure("rmuir", 241)
for d in stacked['data']:
    xVals = d['y']
    yVals = d['x']
    d.update({'orientation' : 'h',
             'x' : xVals,
             'y' : yVals})
    fig.append_trace(d, row = 1, col = 3)
    
fig['layout']['xaxis2'].update({'tickformat' : '%',
                               'hoverformat' : '%'})

fig['layout'].update({'barmode' : 'stack',
                      'height' : 1000})

pyo.iplot(fig)


py.image.save_as(fig, r"C:\Users\Rytch\Google Drive\Financial\Passive Income\Online courses\Plotly\Course Content\Lessons\(06) Barcharts\Notebooks\images\Barcharts (11) - Creating our first Dashboard (2)\pyo.iplot-0.png") 
 #

# ## How many meteorites were found each year in each weight category?
# 
# Let's add the traces for the number of meteorites in each weight category which were found each year. We'll get this data from my website:

# In[5]:

sizes = pd.read_csv("http://richard-muir.com/data/public/csv/MeteoriteLandingsbyWeightPerYear.csv", index_col = 0)
sizes.head()

Пример #35
0
def plot_data_preds_scaled_conv1d(model,
                                  stock,
                                  dfs,
                                  scaled_ts,
                                  scaled_fs,
                                  train_test='all',
                                  train_frac=0.85):
    if train_test == 'all':
        # vertical line should be on the first testing set point
        train_size = int(train_frac * dfs[stock].shape[0])
        print(train_size)
        feats = scaled_fs[stock]
        for_pred = feats.reshape(feats.shape[0], feats.shape[1], 1)
        preds = model.predict(for_pred).ravel()
        print(max([max(scaled_ts[stock].ravel()), max(preds)]))
        layout = {
            'shapes': [{
                'type':
                'rect',
                # stupid hack to deal with pandas issue
                'x0':
                dfs[stock].iloc[train_size:train_size +
                                1].index[0].date().strftime('%Y-%m-%d'),
                'y0':
                1.1 * min([min(scaled_ts[stock].ravel()),
                           min(preds)]),
                'x1':
                dfs[stock].iloc[-2:-1].index[0].date().strftime('%Y-%m-%d'),
                'y1':
                1.1 * max([max(scaled_ts[stock].ravel()),
                           max(preds)]),
                'line': {
                    'color': 'rgb(255, 0, 0)',
                    'width': 2,
                },
                'fillcolor':
                'rgba(128, 0, 128, 0.05)',
            }]
        }
        trace0 = go.Scatter(x=dfs[stock].index,
                            y=scaled_ts[stock].ravel(),
                            mode='lines+markers',
                            name='actual')
        trace1 = go.Scatter(x=dfs[stock].index,
                            y=preds,
                            mode='lines+markers',
                            name='predictions')
        f = iplot({'data': [trace0, trace1], 'layout': layout})
    elif train_test == 'train':
        train_size = int(train_frac * dfs[stock].shape[0])
        feats = scaled_fs[stock][:train_size]
        for_pred = feats.reshape(feats.shape[0], feats.shape[1], 1)
        trace0 = go.Scatter(x=dfs[stock].iloc[:train_size].index,
                            y=scaled_ts[stock].ravel(),
                            mode='lines+markers',
                            name='actual')
        trace1 = go.Scatter(x=dfs[stock].iloc[:train_size].index,
                            y=model.predict(for_pred).ravel(),
                            mode='lines+markers',
                            name='predictions')
        f = iplot([trace0, trace1])
    elif train_test == 'test':
        train_size = int(train_frac * dfs[stock].shape[0])
        feats = scaled_fs[stock][train_size:]
        for_pred = feats.reshape(feats.shape[0], feats.shape[1], 1)
        trace0 = go.Scatter(x=dfs[stock].iloc[train_size:].index,
                            y=scaled_ts[stock].ravel(),
                            mode='lines+markers',
                            name='actual')
        trace1 = go.Scatter(x=dfs[stock].iloc[train_size:].index,
                            y=model.predict(for_pred).ravel(),
                            mode='lines+markers',
                            name='predictions')
        f = iplot([trace0, trace1])
    else:
        print(
            'error!  You have to supply train_test as \'all\', \'train\', or \'test\''
        )
Пример #36
0
 def _ipython_display_(self):
     if not ExpressFigure.offline_initialized:
         init_notebook_mode()
         ExpressFigure.offline_initialized = True
     iplot(self, show_link=False, auto_play=False)
layout = dict(geo={'scope': 'usa'})

# Então usamos:
#
#     go.Figure(data = [data],layout = layout)
#
# para configurar o objeto que finalmente é transmitido para iplot ()

# In[14]:

choromap = go.Figure(data=[data], layout=layout)

# In[15]:

iplot(choromap)

# ### Dados reais: Mapa dos EUA Choropleth
#
# Agora vamos mostrar um exemplo com alguns dados reais, bem como algumas outras opções que podemos adicionar aos dicionários em dados e layout.

# In[16]:

df = pd.read_csv('2011_US_AGRI_Exports')
df.head()

# Agora criamos um dicionário de dados com alguns argumentos adicionais e argumentos de barras de cores:

# In[22]:

data = dict(type='choropleth',
Пример #38
0
# ## Making a barchart
#
# In the same way that we make a scatter trace by specifying <code>{'type' : 'scatter'}</code>, we can make a bar trace by specifying <code>{'type' : 'bar'}</code>.
#
# For this barchart, we'll set the x-values as the year (the index in meteorite DataFrame), and the y-values as the number of meteorites found in that year:

# In[5]:

numberOfMeteorites = {
    'type': 'bar',
    'x': meteorite.index,
    'y': meteorite['count']
}

pyo.iplot([numberOfMeteorites])

py.image.save_as([
    numberOfMeteorites
], r"C:\Users\Rytch\Google Drive\Financial\Passive Income\Online courses\Plotly\Course Content\Lessons\(06) Barcharts\Notebooks\images\Barcharts (02) - making our first barchart\pyo.iplot-0.png"
                 )
#

# #### What happens if we don't set our x- and y-values correctly; will the chart still plot?
#
# Yes, it will, however for a vertical bar chart, Plotly expects each vlaue to be of a distinct category. As you can see from the chart below, Plotly has treated each distinct value of <code>'count'</code> as a separate category. If your bar charts don't come out as you expect, try switching your x- and y-values first - a simple mistake can make your chart look very strange indeed!

# In[10]:

pyo.iplot([{'type': 'bar', 'y': meteorite.index, 'x': meteorite['count']}])
Пример #39
0
import plotly.plotly as py
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot

init_notebook_mode(connected=True)

data = [
    {
        'x': pivot_list[1],
        'y': pivot_list[2],
        'mode':'markers+text',
        'marker': {
            'size': list(map((lambda x: pow(x,0.6)*20), map(int, pivot_list[0]))),
        },
        'text': pivot_list[0],
        'textposition':'middle center'
    }
]

layout = dict(width = 800, height = 600, autosize= False, margin = go.layout.Margin(
l = 150,
r = 50,
b = 150,
t = 50,
pad = 4))

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

iplot(fig, filename='bubblechart-text')
cf.set_config_file(theme='ggplot')

ds['course_rating'].iplot(kind='hist',title='Course Rating ',bargap=0.2,
                                   xTitle='Rating', yTitle='Counts')


# In[90]:


cf.set_config_file(theme='pearl')
ds['course_difficulty'].iplot(kind='hist',title='Course Difficulty',
                             xTitle='Course Type',yTitle='Count')


# In[91]:


course_org = ds.groupby('course_organization').count().reset_index()

trace = go.Pie(labels = course_org['course_organization'], values =course_org['course_students_enrolled'] )
data = [trace]
fig = go.Figure(data = data)
iplot(fig)


# In[ ]:




Пример #41
0
def plot_plotly(
    dataf: pd.DataFrame,
    col_names=None,
    title="",
    labels=None,
    x_label="",
    y_label="",
    file_name="1.html",
    mode_line="lines",
    in_jnb=False,
    legend=None,
    multiaxis=False,
    automodeline=True,
    width=None,
    height=None,
    auto_open=True,
    annotations=None,
    y2_label="",
    linewiths=None,
    x_range=None,
    y_range=None,
    png_name=None,
):
    """
    Функция строит графики, используя библиотеку 'plotly'.

    Parameters:
    -----------
        dataf -- данные, для построения графика (тип pandas.DataFrame)

        col_names -- названия столбцов, которые необходимо отобразить
        (по умолчанию все столбцы)

        title -- название графика (по умолчанию '')

        labels -- легенда (передается списком, по умолчанию '')

        x_label -- название оси 'X' (по умолчанию '')

        y_label -- название оси 'Y' (по умолчанию '')

        file_name -- название файла (по умолчанию '', с сохранением в рабочую
        директорию)

        mode_line -- тип линий (лист, или одно значение)
        возможные значения:'line' - линия, 'markers' - маркеры

        in_jnb -- 'IN_JupyterNoteook' параметр определяет где будет
        выводиться график: в строке (для Jupyter Notebook),
        или в отдельный файл

        multiaxis -- параметр, показывающий необходимо ли,
        каждый график выводить на отдельной оси 'y' (только для двух линий!)

        automodeline -- параметр, включающий атоматическое определение
        типа линий (маркер, в случае отсутствия у значений графика более 40%
        от длины всего датафрейма)

        auto_open -- необходимо ли открыть график после создания.

    Returns:
    --------
        График в формате html, или график в Jupyter Notebook.

    """
    annotations = annotations or []
    linewiths = linewiths or [2]
    x_range = x_range or []
    y_range = y_range or []

    if not isinstance(dataf, pd.DataFrame):
        raise ValueError("`dataf` is not pd.DataFrame.")

    # проверяем названия столбцов (по умолчанию строим по всем столбцам)
    if not col_names:
        col_names = dataf.columns

    # если список легенды не был передан, генерим список
    # необходимой длины
    if not labels:
        labels = col_names

    # определяем тип линий
    # если параметр автоматического определения типа линий включен
    if automodeline:
        mode_line = []
        # для всех столбцов, если количество значений мень 60%, то маркер
        # иначе линия
        for colname in col_names:
            if len(dataf[colname].dropna()) / len(dataf) < 0.6:
                mode_line.append("markers")
            else:
                mode_line.append("lines")
    # если параметр атоматического определения типа линии выключен
    elif len(col_names) > 1 and len(mode_line) != len(col_names):
        mode_line = mode_line * len(col_names)

    # проверить, необходимо ли построить график на двух разных осях y
    if multiaxis:
        if len(col_names) > 2:
            raise ValueError("Required number of multiaxis is 2")

        yaxis = ["y", "y2"]

    else:
        yaxis = ["y"] * len(col_names)

    if len(linewiths) != len(col_names):
        linewiths = [2] * len(col_names)

    # помещаем в переменную 'data' все данные из dataf
    data = []
    for col, m_line, label, y_axis, linewith in zip(col_names, mode_line,
                                                    labels, yaxis, linewiths):
        data.append(
            go.Scatter(
                x=dataf.index,
                y=dataf[col],
                mode=m_line,
                line=dict(width=linewith),
                marker=dict(size=5),
                name=label,
                yaxis=y_axis,
            ))

    # параметры осей
    layout = dict(
        title=title,
        xaxis=dict(title=x_label),  # range=x_range),
        yaxis=dict(title=y_label),  # range=y_range),
        yaxis2=dict(overlaying="y", side="right", title=y2_label),
        annotations=annotations,
    )

    if x_range:
        layout["xaxis"]["range"] = x_range

    if y_range:
        layout["yaxis"]["range"] = y_range

    if width:
        layout["width"] = width

    if height:
        layout["height"] = height

    if legend:
        layout["legend"] = legend

    # создаем фигуру
    fig = go.Figure(data=data, layout=layout)
    fig.update_layout(legend_orientation="h")

    # проверяем, где необходимо построить график:
    # в строке, или сохранить в файл
    if in_jnb:
        # случае, если функция была вызвана из Jupyter
        # для отрисовки графика inline,
        # необходимо подклюить режим notebook
        init_notebook_mode(connected=True)
        iplot(fig, show_link=False, config={"scrollZoom": True})

    # если указано название имени картинок в формате `*.png`
    elif png_name:
        pio.write_image(fig, png_name, width=800, height=500)

    else:
        plot(fig,
             filename=file_name,
             auto_open=auto_open,
             config={"scrollZoom": True})
Пример #42
0
ax = sns.scatterplot(x='date',
                     y='recovered',
                     data=dataset,
                     color='blue',
                     label='recovered')
ax = sns.scatterplot(x='date',
                     y='dead',
                     data=dataset,
                     color='red',
                     label='dead')
plt.plot(dataset.date, dataset.confirmed, zorder=1, color='black')
plt.plot(dataset.date, dataset.recovered, zorder=1, color='blue')
plt.plot(dataset.date, dataset.dead, zorder=1, color='red')

abc = dataset.groupby('date')[['confirmed', 'recovered',
                               'dead']].sum().reset_index()
r_cm = (abc.recovered / abc.confirmed)
d_cm = (abc.dead / abc.confirmed)

#Prophet on confirmed
prop_con = dataset.iloc[:, [0, 6]]
prop_con.columns = ['ds', 'y']
m1 = Prophet()
m1.fit(prop_con)
future = m1.make_future_dataframe(periods=365)
forecast_con = m1.predict(future)
figure = plot_plotly(m1, forecast_con)
py.iplot(figure)
figure = m1.plot(forecast_con, xlabel='date', ylabel='confirmed')
figure = m1.plot_components(forecast_con)
Пример #43
0
# light times is a N vector of time
print('Light times: ')
print(lightTimes[0])

# clean up the kernels
spice.kclear()

# plot
plotly.offline.init_notebook_mode()
threeDPlot = go.Scatter3d(
    x=positions[:, 0],  # X coordinates
    y=positions[:, 1],  # Y coordinates
    z=positions[:, 2],  # Z coordinates
    name='Cassini',
    mode='lines',
    line=dict(width=3))

barycenter = go.Scatter3d(x=[0],
                          y=[0],
                          z=[0],
                          name='bc',
                          mode='marker',
                          marker=dict(size=10, color='orange'))

data = [threeDPlot, barycenter]

layout = go.Layout(title="SpiceyPy Cassini Position Example")

fig = dict(data=data, layout=layout)
py.iplot(fig)
Пример #44
0
def plot(pca_results, plot_counter):

    # Get results
    pca = pca_results['pca']
    var_explained = pca_results['var_explained']
    sample_metadata = pca_results['sample_metadata']
    color_by = pca_results.get('color_by')
    color_type = pca_results.get('color_type')
    color_column = pca_results['sample_metadata'][
        color_by] if color_by else None
    colors = [
        '#a6cee3', '#1f78b4', '#b2df8a', '#33a02c', '#fb9a99', '#e31a1c',
        '#fdbf6f', '#ff7f00', '#cab2d6', '#6a3d9a', '#ffff99', '#b15928'
    ]
    sample_titles = [
        '<b>{}</b><br>'.format(index) +
        '<br>'.join('<i>{key}</i>: {value}'.format(**locals())
                    for key, value in rowData.items())
        for index, rowData in sample_metadata.iterrows()
    ]

    if not color_by:
        marker = dict(size=15)
        trace = go.Scatter3d(x=pca.components_[0],
                             y=pca.components_[1],
                             z=pca.components_[2],
                             mode='markers',
                             hoverinfo='text',
                             text=sample_titles,
                             marker=marker)
        data = [trace]
    elif color_by and color_type == 'continuous':
        marker = dict(size=15,
                      color=color_column,
                      colorscale='Viridis',
                      showscale=True)
        trace = go.Scatter3d(x=pca.components_[0],
                             y=pca.components_[1],
                             z=pca.components_[2],
                             mode='markers',
                             hoverinfo='text',
                             text=sample_titles,
                             marker=marker)
        data = [trace]
    elif color_by and color_type == 'categorical':

        # Get unique categories
        unique_categories = color_column.unique()

        # Define empty list
        data = []

        # Loop through the unique categories
        for i, category in enumerate(unique_categories):

            # Get the color corresponding to the category

            # If signature
            if color_by == 'Sample Group':
                group_A, group_B = [
                    x.split(' vs ')
                    for x in pca_results['signature_metadata'].keys()
                ][0]
                if category == group_A:
                    category_color = 'blue'
                elif category == group_B:
                    category_color = 'red'
                else:
                    category_color = 'black'
            else:
                category_color = colors[i]

            # Get the indices of the samples corresponding to the category
            category_indices = [
                i for i, sample_category in enumerate(color_column)
                if sample_category == category
            ]

            # Create new trace
            trace = go.Scatter3d(
                x=pca.components_[0][category_indices],
                y=pca.components_[1][category_indices],
                z=pca.components_[2][category_indices],
                mode='markers',
                hoverinfo='text',
                text=[sample_titles[x] for x in category_indices],
                name=category,
                marker=dict(size=15, color=category_color))

            # Append trace to data list
            data.append(trace)

    colored = '' if str(color_by) == 'None' else 'Colored by {}'.format(
        color_by)
    layout = go.Layout(
        title='<b>PCA Analysis | Scatter Plot</b><br><i>{}</i>'.format(
            colored),
        hovermode='closest',
        margin=go.Margin(l=0, r=0, b=0, t=50),
        width=900,
        scene=dict(xaxis=dict(title=var_explained[0]),
                   yaxis=dict(title=var_explained[1]),
                   zaxis=dict(title=var_explained[2])))
    fig = go.Figure(data=data, layout=layout)

    if pca_results['plot_type'] == 'interactive':
        iplot(fig)
    else:
        s.static_plot(fig)
    display(
        Markdown(
            '** Figure ' + plot_counter() +
            ' | Principal Component Analysis results. ** The figure displays an interactive, three-dimensional scatter plot of the first three Principal Components (PCs) of the data. Each point represents an RNA-seq sample. Samples with similar gene expression profiles are closer in the three-dimensional space. If provided, sample groups are indicated using different colors, allowing for easier interpretation of the results. If you are experiencing issues visualizing the plot, please visit our <a href="https://amp.pharm.mssm.edu/biojupies/help#troubleshooting" target="_blank">Troubleshooting guide</a>'
            .format(**locals())))
Пример #45
0
# Choropleth maps- draw data local or global scale
#build date dic
data = dict(
    type='choropleth',  #type geoplot
    locations=['AZ', 'CA', 'NY'],  #list state abbrev codes
    locationmode='USA-states',  #can go to county level
    colorscale='Greens',
    text=['text 1', 'text 2', 'text 3'],  #labels for locations
    z=[1.0, 2.0, 3.0],  # value want to represent as a color, e.g. population
    colorbar={'title': 'Colorbar title here'})

layout = dict(geo={'scope': 'usa'})  # use states choropleth

choromap = go.figure(data=[data], layout=layout)
iplot(choromap)  #could also just do plot

df = pd.read_csv('2011_US_AGRI_Exports')
df.head()

data = dict(
    type='choropleth',
    colorscale='YlOrRd',
    locations=df['code'],
    locationmode='USA-states',
    z=df['total exports'],
    text=df['text'],
    marker=dict(
        line=dict(color='rgb(255,255,255)', width=2)),  #spacing between states
    colorbar={'title': 'Millions USD'})
Пример #46
0
def fc_plot(obj, title='', xTitle='Date', yTitle='', asFigure=False):
    """Plots actual, fitted, and predicted values from forecast class in plotly graph
    Args:
        obj: forecast object (dict for single series and dataframe for multiple series)
        title: plot title
        xTitle: xaxis title
        yTitle: y axis title
        asFigure: parameter to return as fig instead of the plot if set to True
        
    Returns:
        fig: plotly figure object
        
    
    """

    try:
        #handle dict (assumes it's forecast object)
        if isinstance(obj, dict):
            trace_actuals = go.Scatter(x=obj['x'].index,
                                       y=obj['x'].values,
                                       mode='lines',
                                       name='actual')
            trace_fitted = go.Scatter(x=obj['fitted'].index,
                                      y=obj['fitted'].values,
                                      mode='lines+markers',
                                      name='fitted',
                                      opacity=0.8)

            trace_predicted = go.Scatter(x=obj['predicted'].index,
                                         y=obj['predicted'].values,
                                         mode='lines',
                                         name='predicted')

            trace_lower = go.Scatter(x=obj['predicted'].index,
                                     y=obj['lower'],
                                     fill=None,
                                     mode='lines',
                                     name='Lower PI (' + str(obj['level']) +
                                     '%)',
                                     line=dict(color='lightgreen', ))
            trace_upper = go.Scatter(x=obj['predicted'].index,
                                     y=obj['upper'],
                                     fill='tonexty',
                                     mode='lines',
                                     name='Upper PI (' + str(obj['level']) +
                                     '%)',
                                     line=dict(color='lightgreen', ))

            data = [
                trace_actuals, trace_fitted, trace_predicted, trace_lower,
                trace_upper
            ]

            layout = dict(title=title,
                          yaxis=dict(title=yTitle),
                          xaxis=dict(title=xTitle,
                                     rangeslider=dict(),
                                     type='date'))
            fig = dict(data=data, layout=layout)

        elif isinstance(obj, pd.core.series.Series):

            trace_series = go.Scatter(x=obj.index,
                                      y=obj.values,
                                      mode='lines',
                                      name='series')
            data = [trace_actuals]

            layout = dict(title=title,
                          yaxis=dict(title=yTitle),
                          xaxis=dict(title=xTitle,
                                     rangeslider=dict(),
                                     type='date'))
            fig = dict(data=data, layout=layout)

        #separate logic if input is dataframe
        elif isinstance(obj, pd.core.frame.DataFrame):
            #return cufflinks figure and add rangeslider to layout
            fig = obj.iplot(kind='scatter',
                            title=title,
                            yTitle=yTitle,
                            xTitle=xTitle,
                            asFigure=True)
            fig['layout']['xaxis1']['rangeslider'] = dict()

        else:
            raise TypeError(
                'only accepted objects are dictionary or dataframe returned from forecast class'
            )
    except TypeError:
        print(
            'only accepted objects are dictionary or dataframe returned from forecast class'
        )

    if asFigure == True:
        return fig
    return iplot(fig, show_link=False)
Пример #47
0
                                        inplace=False)
data_city = pd.DataFrame({
    'city': data['city'].value_counts().index,
    'value': data['city'].value_counts().values
})
data = [{
    'x': data_city['city'][0:10].values,
    'y': data_city['value'][0:10].values,
    'mode': 'markers',
    'marker': {
        'sizemode': 'area',
        #   'sizeref': 'sizeref',
        'size': data_city['value'][0:10]
    }
}]
iplot(data)
City_State = pd.merge(data_city,
                      df1_filter,
                      how='left',
                      left_on='city',
                      right_on='city')
City_State = City_State.drop_duplicates(subset='city',
                                        keep='first',
                                        inplace=False)
count = City_State['value'].values
m = folium.Map(location=[28, 81], tiles="Mapbox Bright", zoom_start=4.5)
for i in range(0, 10):
    folium.Circle(
        location=[
            City_State.iloc[i]['latitude'], City_State.iloc[i]['longitude']
        ],
Пример #48
0
def acc_plot(obj,
             title='',
             xTitle='',
             yTitle='',
             mode='lines+markers',
             tablewidth=350,
             asFigure=False):
    """ plots accuracy measures
    Args:
        obj: accuracy object(dict for single series, dataframe for multiple series)
        title: plot title
        xTitle: xaxis title
        yTitle: y axis title
        mode: one of 'lines', 'markers', or 'lines+markers' (only applies to multiple series)
        tablewidth:sets table width for accuracy measures of single instance
        asFigure: parameter to return as fig instead of the plot if set to True
        
    Returns:
        fig: plotly figure object
        
    
    """

    try:
        #handle dict (assumes it's forecast object)
        if isinstance(obj, dict):
            #create dataframe from accuracy dictionary
            table_df = pd.DataFrame.from_dict(obj, orient='index', dtype=None)
            table_df.columns = ['Value']
            #round decimals
            table_df = table_df.round(5)
            fig = ff.create_table(table_df,
                                  index=True,
                                  index_title='Accuracy Measure')
            fig.layout.update({'width': tablewidth})

        #separate logic if input is dataframe
        elif isinstance(obj, pd.core.frame.DataFrame):
            #return cufflinks figure, normalize error measures, then plot them without a y axis
            normalized_df = obj.T.apply(lambda x: x / abs(x).max(), axis=0)
            fig = normalized_df.T.iplot(kind='scatter',
                                        title=title,
                                        xTitle=xTitle,
                                        yTitle=yTitle,
                                        mode=mode,
                                        asFigure=True)
            fig['layout']['yaxis1']['visible'] = False
            for error_measure in fig['data']:
                #get correct text values from original dataframe and round values to 4 decimals
                error_measure['text'] = np.around(
                    obj[error_measure['name']].values, decimals=4)
                error_measure['hoverinfo'] = 'text+name'

        else:
            raise TypeError(
                'only accepted objects are dictionary or dataframe returned from forecast class'
            )
    except TypeError:
        print(
            'only accepted objects are dictionary or dataframe returned from forecast class'
        )

    if asFigure == True:
        return fig
    return iplot(fig, show_link=False)
Пример #49
0
def per_contract(future_chain, field='Volume'):
    """Plot heatmap of the average Volume or Open Interest of the last 90 days. Display it by year for each contract.
    This should be useful to find out which contract are traded/not traded and where

    :param future_chain: object - Future Chain initialized with data
    :param field: str - Volume or OI
    :return: object - Plot Plotly chart
    """
    log = logging.getLogger(__name__)

    if field != 'Volume' and field != 'OI':
        raise Exception('Field provided must be str Volume or OI!')

    stem = future_chain.stem
    df = future_chain.chain
    data = future_chain.data
    # Average Volume
    df['Month'] = df.apply(lambda x: x['Ticker'][2:3], axis=1)
    df['Year'] = df.apply(lambda x: 2000 + int(x['Ticker'][-2:]), axis=1)
    # Add average volume of last 90 days
    df['AvgVol'] = 0
    for idx, row in df.iterrows():
        mdf = data[row['Ticker']]
        df.loc[idx, 'AvgVol'] = int(mdf[-90:][field].mean())
    # Transformation to HeatMap DF
    years = []
    for year in df['Year'].unique():
        ydf = df[df['Year'] == year][['Month', 'AvgVol']]
        ydf.set_index('Month', drop=True, inplace=True)
        ydf.index.names = [None]
        ydf.columns = [year]
        years.append(ydf)
    hdf = pd.concat(years, axis=1)
    # Index
    cdf = oci.ctrmth(stem, we_trade=False)
    cdf['Letter'] = cdf.apply(lambda x: oci.ym_maturity(x['CtrMth'])[0],
                              axis=1)
    lw = cdf.groupby('Letter').mean()
    lw['Index'] = lw.apply(lambda x: '{} (T: {})'.format(x.name, x['WeTrd']),
                           axis=1)
    rlw = lw.reindex(index=lw.index[::-1])
    # Plot
    rhdf = hdf.reindex(index=hdf.index[::-1])
    values = rhdf.values
    values = [[int(vi) if not math.isnan(vi) else float('NaN') for vi in v]
              for v in values]
    try:
        fig = pff.create_annotated_heatmap(values,
                                           x=list(rhdf.columns),
                                           y=list(rlw['Index']),
                                           colorscale='Jet',
                                           font_colors=['white'],
                                           hoverinfo='z')
        for i in range(len(fig.layout.annotations)):  # Make text size smaller
            fig.layout.annotations[i].font.size = 10
        fig.layout.title = '{} - {}'.format(stem, field)

        plo.iplot(fig)
    except ple.PlotlyError:
        log.error(
            'Most likely a problem with axis length: x: {} - y: {} - z: {}'.
            format(list(rhdf.columns), list(rlw['Index']), len(values)))
Пример #50
0
def telecom_churn_prediction(algorithm, training_x, testing_x, training_y,
                             testing_y, cols, cf, threshold_plot):

    #model
    algorithm.fit(training_x, training_y)
    predictions = algorithm.predict(testing_x)
    probabilities = algorithm.predict_proba(testing_x)
    #coeffs
    if cf == "coefficients":
        coefficients = pd.DataFrame(algorithm.coef_.ravel())
    elif cf == "features":
        coefficients = pd.DataFrame(algorithm.feature_importances_)

    column_df = pd.DataFrame(cols)
    coef_sumry = (pd.merge(coefficients,
                           column_df,
                           left_index=True,
                           right_index=True,
                           how="left"))
    coef_sumry.columns = ["coefficients", "features"]
    coef_sumry = coef_sumry.sort_values(by="coefficients", ascending=False)

    print(algorithm)
    print("\n Classification report : \n",
          classification_report(testing_y, predictions))
    print("Accuracy   Score : ", accuracy_score(testing_y, predictions))
    #confusion matrix
    conf_matrix = confusion_matrix(testing_y, predictions)
    #roc_auc_score
    model_roc_auc = roc_auc_score(testing_y, predictions)
    print("Area under curve : ", model_roc_auc, "\n")
    fpr, tpr, thresholds = roc_curve(testing_y, probabilities[:, 1])

    #plot confusion matrix
    trace1 = go.Heatmap(z=conf_matrix,
                        x=["Not churn", "Churn"],
                        y=["Not churn", "Churn"],
                        showscale=False,
                        colorscale="Picnic",
                        name="matrix")

    #plot roc curve
    trace2 = go.Scatter(x=fpr,
                        y=tpr,
                        name="Roc : " + str(model_roc_auc),
                        line=dict(color=('rgb(22, 96, 167)'), width=2))
    trace3 = go.Scatter(x=[0, 1],
                        y=[0, 1],
                        line=dict(color=('rgb(205, 12, 24)'),
                                  width=2,
                                  dash='dot'))

    #plot coeffs
    trace4 = go.Bar(x=coef_sumry["features"],
                    y=coef_sumry["coefficients"],
                    name="coefficients",
                    marker=dict(color=coef_sumry["coefficients"],
                                colorscale="Picnic",
                                line=dict(width=.6, color="black")))

    #subplots
    fig = tls.make_subplots(
        rows=2,
        cols=2,
        specs=[[{}, {}], [{
            'colspan': 2
        }, None]],
        subplot_titles=('Confusion Matrix',
                        'Receiver operating characteristic',
                        'Feature Importances'))

    fig.append_trace(trace1, 1, 1)
    fig.append_trace(trace2, 1, 2)
    fig.append_trace(trace3, 1, 2)
    fig.append_trace(trace4, 2, 1)

    fig['layout'].update(showlegend=False,
                         title="Model performance",
                         autosize=False,
                         height=900,
                         width=800,
                         plot_bgcolor='rgba(240,240,240, 0.95)',
                         paper_bgcolor='rgba(240,240,240, 0.95)',
                         margin=dict(b=195))
    fig["layout"]["xaxis2"].update(dict(title="false positive rate"))
    fig["layout"]["yaxis2"].update(dict(title="true positive rate"))
    fig["layout"]["xaxis3"].update(
        dict(showgrid=True, tickfont=dict(size=10), tickangle=90))
    py.iplot(fig)
Пример #51
0
def plot_data_preds_unscaled(model,
                             stock,
                             t_scalers,
                             scaled_ts,
                             scaled_fs,
                             targs,
                             dates,
                             datapoints=300,
                             train_frac=0.85,
                             future_days=5):
    dates = dates[stock]
    train_size = int(train_frac * dates.shape[0])

    for_preds = scaled_fs[stock].reshape(scaled_fs[stock].shape[0], 1,
                                         scaled_fs[stock].shape[1])
    preds = model.predict(for_preds).ravel()
    unscaled_preds = t_scalers[stock].reform_data(preds, orig=True)

    if datapoints == 'all':
        datapoints = dates.shape[0]

    layout = {
        'shapes': [
            {
                'type':
                'rect',
                # first line is just before first point of test set
                'x0':
                dates[train_size].date().strftime('%Y-%m-%d'),
                'y0':
                1.1 * min([
                    min(targs[stock][-datapoints:]),
                    min(unscaled_preds.ravel()[-datapoints:])
                ]),
                'x1':
                dates[-1].date().strftime('%Y-%m-%d'),
                'y1':
                1.1 * max([
                    max(targs[stock][-datapoints:]),
                    max(unscaled_preds.ravel()[-datapoints:])
                ]),
                'line': {
                    'color': 'rgb(255, 0, 0)',
                    'width': 2,
                },
                'fillcolor':
                'rgba(128, 0, 128, 0.05)',
            },
            {
                'type':
                'line',
                # first line is just before first point of test set
                'x0':
                dates[train_size + future_days].date().strftime('%Y-%m-%d'),
                'y0':
                1.1 * min([
                    min(targs[stock][-datapoints:]),
                    min(unscaled_preds.ravel()[-datapoints:])
                ]),
                'x1':
                dates[train_size + future_days].date().strftime('%Y-%m-%d'),
                'y1':
                1.1 * max([
                    max(targs[stock][-datapoints:]),
                    max(unscaled_preds.ravel()[-datapoints:])
                ]),
                'line': {
                    'color': 'rgb(0, 255, 0)',
                    'width': 2,
                }
            }
        ],
        'yaxis': {
            'title': 'GLD price'
        }
    }

    trace0 = go.Scatter(x=dates[-datapoints:],
                        y=targs[stock][-datapoints:],
                        mode='lines+markers',
                        name='actual')
    trace1 = go.Scatter(x=dates[-datapoints:],
                        y=unscaled_preds.ravel()[-datapoints:],
                        mode='lines+markers',
                        name='predictions')
    f = iplot({'data': [trace0, trace1], 'layout': layout})
Пример #52
0
def plotOneLine(x,y,mode='lines+markers',legend_name='',title=''):
    d=[go.Scatter(x=x,y=y,mode=mode,name=legend_name)]
    fig=go.Figure(data=d,layout=go.Layout(title=title))
    iplot(fig,show_link=False)
Пример #53
0
        'symbol': 0,
        'size': 8
    },
    mode="markers+lines",
    text=['Year: ' + str(i) for i in list(range(2002, 2018))],  # hover text
    name='Suicide rate in Females')

plotdata = go.Data([trace, trace1, trace2])  # Process the plots

layout = go.Layout(title="Age-adjusted Suicide Rates in United States",
                   xaxis={'title': 'Year'},
                   yaxis={'title': 'Suicide rate'})  # design layout

figure = go.Figure(data=plotdata,
                   layout=layout)  # combine data and layout code
iplot(figure)

data1 = pd.read_csv(
    "https://raw.githubusercontent.com/plotly/datasets/master/2014_world_gdp_with_codes.csv"
)  #read csv for plot2

plotdata = go.Choropleth(
    locations=data1['CODE'],
    z=data1['GDP (BILLIONS)'],
    text=data1['COUNTRY'],
    autocolorscale=False,
    colorscale='Picnic',
    showscale=True,
)  # Process the plots
layout = go.Layout(title="GDP of countries"  # giving title to plot layout
                   )
Пример #54
0
def Plot_Streamflow(StreamDic, Rainfall=None, colors=ColorsDefault, **kwargs):
    '''Function to plot streamflow data with dates axis and 
    rainfall
    Parameters:
        - Dates: pandas index dates format.
        - StreamDic: Dictionary with the data and plot properties:
            ej: {'Q1':{data:np.array(data), 
                'dates': pd.Series.index,
                'color': 'rgb(30,114,36)', 
                'lw': 4, 'ls': '--'}}
    Optional:
        - Rainfall: Dictionary with rainfall data with the same 
            structure as StreamDic.'''
    #Data definition
    cont = 0
    data = []
    for k in StreamDic.keys():
        #Set del trace
        try:
            setColor = StreamDic[k]['color']
        except:
            setColor = np.random.choice(list(ColorsDefault.keys()), 1)
            setColor = ColorsDefault[setColor[0]]
        try:
            setWeight = StreamDic[k]['lw']
        except:
            setWeight = kwargs.get('lw', 4)
        try:
            setLineStyle = StreamDic[k]['ls']
        except:
            setLineStyle = kwargs.get('ls', None)
        #Traces definitions
        trace = go.Scatter(x=StreamDic[k]['dates'],
                           y=StreamDic[k]['data'],
                           name=k,
                           line=dict(color=setColor,
                                     width=setWeight,
                                     dash=setLineStyle),
                           opacity=1.0)
        #Update data
        data.append(trace)

    #Rainfall
    if type(Rainfall) == dict:
        trace = go.Scatter(x=Rainfall['dates'],
                           y=Rainfall['data'],
                           line=dict(color='blue'),
                           opacity=0.8,
                           yaxis='y2',
                           fill='tozeroy',
                           fillcolor='rgba(0, 102, 153,0.2)')
        data.append(trace)

    #Layout definition
    layout = dict(showlegend=False,
                  xaxis=dict(title='Dates'),
                  yaxis=dict(title="Streamflow [m3/s]"),
                  yaxis2=dict(autorange="reversed",
                              title="Caudal [m3/s]",
                              overlaying='y',
                              side='right'))

    fig = dict(data=data, layout=layout)
    iplot(fig)
Пример #55
0
                            how='inner',
                            on='settle_date')

    # Step 02: plot atm_iv vs close
    chart_title = f'{SYMBOL_TO_RESEARCH} atm vol vs close'
    df_atm_vs_close = df_atmv[['settle_date', 'atm_iv', 'close']]
    fig_atm_vs_close = plotly_plot(df_atm_vs_close,
                                   x_column='settle_date',
                                   yaxis2_cols=['close'],
                                   y_left_label='atm_iv',
                                   y_right_label='close',
                                   plot_title=chart_title)
    return fig_atm_vs_close


iplot(plot_atm_vs_close('CL', df_iv_final, df_cash_futures, year=2020))
for d in [.05, .1, .2]:
    fig1, fig2 = plot_skew_vs_atm('CL',
                                  df_iv_final,
                                  df_iv_skew,
                                  df_cash_futures,
                                  dist_from_zero=d,
                                  year=2020)
    iplot(fig1)
    iplot(fig2)

# ### Define initial banner and dropdowns

# In[ ]:

logger = dgc.init_root_logger('logfile.log', 'WARN')
Пример #56
0
def Plot_DurationCurve(StreamDic,
                       Rainfall=None,
                       colors=ColorsDefault,
                       Pinf=0.2,
                       Psup=99.8,
                       Nint=50,
                       **kwargs):
    '''Function to plot streamflow data with dates axis and 
    rainfall
    Parameters:
        - Dates: pandas index dates format.
        - StreamDic: Dictionary with the data and plot properties:
            ej: {'Q1':{data:np.array(data), 
                'color': 'rgb(30,114,36)', 
                'lw': 4, 'ls': '--'}}
    Optional:
        - Pinf: Inferior percentile (0.2)
        - Psup: Superior percentile (99.8)
        - Nint: Total intervals (50)
        - Rainfall: Dictionary with rainfall data with the same 
            structure as StreamDic.'''

    #Obtains the excedance probability and
    def GetExcedProb(X):
        Qexc = []
        for p in np.linspace(Pinf, Psup, Nint):
            Qexc.append(np.percentile(X, p))
        return Qexc, np.linspace(Pinf, Psup, Nint)[::-1] / 100.

    #Data definition
    cont = 0
    data = []
    for k in StreamDic.keys():
        #Set del trace
        try:
            setColor = StreamDic[k]['color']
        except:
            setColor = np.random.choice(list(ColorsDefault.keys()), 1)
            setColor = ColorsDefault[setColor[0]]
        try:
            setWeight = StreamDic[k]['lw']
        except:
            setWeight = kwargs.get('lw', 4)
        try:
            setLineStyle = StreamDic[k]['ls']
        except:
            setLineStyle = kwargs.get('ls', None)
        #Values and P(x>X)
        Qexc, P = GetExcedProb(StreamDic[k]['data'])
        #Traces definitions
        trace = go.Scatter(x=P,
                           y=Qexc,
                           name=k,
                           line=dict(color=setColor,
                                     width=setWeight,
                                     dash=setLineStyle),
                           opacity=1.0)
        #Update data
        data.append(trace)

    #Layout definition
    layout = dict(
        showlegend=False,
        xaxis=dict(
            title='P(x>X)',
            tickfont=dict(color='rgb(0, 102, 153)', size=16),
            titlefont=dict(color='rgb(0, 102, 153)', size=20),
        ),
        yaxis=dict(
            title="Streamflow [m3/s]",
            tickfont=dict(color='rgb(0, 102, 153)', size=16),
            titlefont=dict(color='rgb(0, 102, 153)', size=20),
        ),
    )

    fig = dict(data=data, layout=layout)
    iplot(fig)
Пример #57
0
    def plot_log(self, symbol, engine='plotly', notebook=False):
        if engine == 'plotly':

            def draw(i):
                tlog = self.tlog[self.tlog['symbol'] == i]
                ltlog = tlog[tlog['s_type'] == 'LONG']
                stlog = tlog[tlog['s_type'] == 'SHORT']
                price = go.Scatter(x=tlog.index,
                                   y=tlog['price'],
                                   name=i + '_price',
                                   xaxis='x3',
                                   yaxis='y3')
                qty = go.Scatter(x=tlog.index,
                                 y=tlog['qty'],
                                 name=i + '_qty',
                                 xaxis='x4',
                                 yaxis='y4')
                LONG_cur_positions = go.Scatter(x=ltlog.index,
                                                y=ltlog['cur_positions'],
                                                name=i + 'LONG_cur_positions',
                                                xaxis='x4',
                                                yaxis='y4')
                SHORT_cur_positions = go.Scatter(x=stlog.index,
                                                 y=stlog['cur_positions'],
                                                 name=i +
                                                 'SHORT_cur_positions',
                                                 xaxis='x4',
                                                 yaxis='y4')
                LONG_period = go.Scatter(x=ltlog.index,
                                         y=ltlog['period'].astype(int) /
                                         (60 * 60 * 24 * 10**9),
                                         name=i + 'LONG_period',
                                         xaxis='x2',
                                         yaxis='y2')
                SHORT_period = go.Scatter(x=stlog.index,
                                          y=stlog['period'].astype(int) /
                                          (60 * 60 * 24 * 10**9),
                                          name=i + 'SHORT_period',
                                          xaxis='x2',
                                          yaxis='y2')
                LONG_PnL = go.Scatter(x=ltlog.index,
                                      y=ltlog['PnL'],
                                      name=i + 'LONG_PnL',
                                      xaxis='x2',
                                      yaxis='y2')
                SHORT_PnL = go.Scatter(x=stlog.index,
                                       y=stlog['PnL'],
                                       name=i + 'SHORT_PnL',
                                       xaxis='x2',
                                       yaxis='y2')
                self.data.append(price)
                self.data.append(qty)
                self.data.append(LONG_cur_positions)
                self.data.append(SHORT_cur_positions)
                self.data.append(LONG_period)
                self.data.append(SHORT_period)
                self.data.append(LONG_PnL)
                self.data.append(SHORT_PnL)

            if type(symbol) == list:
                for i in symbol:
                    draw(i)
            if type(symbol) == str:
                draw(symbol)

            layout = go.Layout(updatemenus=self.updatemenus,
                               xaxis2=dict(
                                   domain=[0, 1],
                                   anchor='y2',
                               ),
                               xaxis3=dict(domain=[0, 1], anchor='y3'),
                               xaxis4=dict(domain=[0, 1], anchor='y4'),
                               yaxis2=dict(domain=[0, 0.3], ),
                               yaxis3=dict(domain=[0.3, 0.6]),
                               yaxis4=dict(domain=[0.6, 1], ))
            fig = go.Figure(data=self.data, layout=layout)
            if notebook:
                import plotly
                plotly.offline.init_notebook_mode()
                py.iplot(fig, filename='testplot.html', validate=False)
            else:
                py.plot(fig, filename='testplot.html', validate=False)
def two_param_dist_plot(data, group_name, field_name):
    fig = ff.create_distplot([
        data[data[group_name] == i][field_name].dropna()
        for i in data[group_name].value_counts().index
    ], data[group_name].value_counts().index)
    py.iplot(fig)
Пример #59
0
# convert Spark dataframe to pandas in order to visualize data

grouped_by_rfmscore_pandas = grouped_by_rfmscore.toPandas()

# In[66]:

grouped_by_rfmscore_pandas

# In[67]:

grouped_by_rfmscore_pandas[
    'RFM_Score'] = "Seg " + grouped_by_rfmscore_pandas['RFM_Score'].map(str)

# In[73]:

data = [
    go.Bar(x=grouped_by_rfmscore_pandas['RFM_Score'],
           y=grouped_by_rfmscore_pandas['count'])
]

layout = go.Layout(
    title=go.layout.Title(text='Customer RFM Segments'),
    xaxis=go.layout.XAxis(title=go.layout.xaxis.Title(text='RFM Segment')),
    yaxis=go.layout.YAxis(title=go.layout.yaxis.Title(
        text='Number of Customers')))

fig = go.Figure(data=data, layout=layout)
iplot(fig, filename='rfm_Segments')

# In[ ]:

# ## Bivariate Analysis:

# ### Bivariate Analysis on duration and protocol_type

# In[64]:



data = []

for col in connSample['protocol_type'].unique():
    data.append(go.Box(y=connSample[connSample['protocol_type'] == col]['duration'], name=col))

iplot(data)


# - Conclusion: Regardless protocol_type feature most duration features are 0's.

# In[65]:


connSample['duration_log'] = np.log(connSample.duration+1)


# In[66]:


# Examine the distribution with duration_log > 0. Distributions of duration is very different between 'tcp' and 'udp'
connSample[connSample.duration_log > 0][[ "protocol_type"]].iplot(kind='hist' , color = "mediumpurple" , linecolor = 'black' ,  title= 'Top Protocol by Duration' ,  orientation = 'h' ,  xTitle='Duration_Log' ,  yTitle= 'Protocol_Type' , showlegend=True)