예제 #1
0
def plot_original_price_series(df_fut_orig):
    """
    Plots Original Corn Futures Price Series using Plotly
    """

    fig = tls.make_subplots(rows=5,
                            cols=1,
                            shared_xaxes=True,
                            print_grid=False,
                            specs=[[{
                                'rowspan': 4
                            }], [None], [None], [None], [{}]])

    for col in ['Settle']:
        fig.append_trace(
            {
                'x': df_fut_orig.index,
                'y': df_fut_orig[col],
                'type': 'scatter',
                'name': col
            }, 1, 1)
    for col in ['Volume']:
        fig.append_trace(
            {
                'x': df_fut_orig.index,
                'y': df_fut_orig[col],
                'type': 'bar',
                'name': col
            }, 5, 1)

    fig['layout']['xaxis'].update(title='Date')
    fig['layout']['yaxis1'].update(title='Settling Price (Cents) ')
    fig['layout']['yaxis2'].update(title='Volume')
    cf.iplot(fig)
예제 #2
0
def distplot1d(ss: pd.Series,
               bins=10,
               hist=True,
               kde=True,
               rug=True,
               color=None,
               as_figure=False,
               legend=True,
               title=True,
               grid=True,
               figsize=None,
               **kwargs):
    ss = pd.Series(ss)
    bin_size = (ss.max() - ss.min()) / bins
    curve_type = 'kde' if kde else 'normal'
    fig = ff.create_distplot(hist_data=[ss.values],
                             group_labels=[str(ss.name)],
                             bin_size=bin_size,
                             curve_type=curve_type,
                             colors=color,
                             show_hist=hist,
                             show_rug=rug,
                             **kwargs)
    fig['layout'].update(showlegend=legend, title=title)
    if figsize:
        fig['layout'].update(width=figsize[0], height=figsize[1])
    for k, v in fig['layout'].items():
        if 'axis' in k:
            v.update(showgrid=grid)
    if as_figure:
        return fig
    cf.iplot(fig)
예제 #3
0
def plot_grouped_by_year_data(df_weekly, title):
    """
    Plots weekly combined series (price series and cot report) using Plotly. Use trading weeks on X axis
    """
    x_series = np.arange(0, 54)
    groups = df_weekly['Settle'].groupby(Grouper(freq='A'))
    fig = tls.make_subplots(rows=sum(1 for (name, grp) in groups
                                     if len(grp.values) >= 52),
                            cols=1,
                            shared_xaxes=True,
                            print_grid=False)
    fig['layout'].update(height=600, width=899, title=title)
    for i, (name, group) in enumerate([(name, grp) for (name, grp) in groups
                                       if len(grp.values) >= 52]):
        # chart only data where we have full year (52 weeks)
        if len(group.values) >= 52:
            fig.append_trace(
                {
                    'x': x_series,
                    'y': group.values,
                    'type': 'scatter',
                    'name': name.year
                }, i + 1, 1)
            fig['layout']['yaxis' + str((i + 1))].update(showticklabels=False)

    cf.iplot(fig)
예제 #4
0
def plot_fit(series, filename):
    y = series.tolist()
    x = np.array(range(len(y)))
    gauss = Gauss(x, y)
    y_pred = gauss.fit()
    fit_series = pd.Series(y_pred, series.index, name="Fitted Curve")

    _, m, s = gauss.par
    current = int(sum(y))
    estimate = gauss.estimate_total()

    fig1 = series.iplot(kind="bar", asFigure=True)
    fig2 = fit_series.iplot(asFigure=True,
                            colors=['blue'],
                            width=2,
                            dash="dashdot")

    fig = cf.tools.merge_figures([fig1, fig2])
    fig = go.Figure(fig)
    fig.update_layout(
        title_text="Total Infection Estimate<br>-------------------------------"\
                   "<br>Current: {} people,"\
                   " Estimate: {} people".format(current,estimate),
        yaxis_title="Capita [-]")

    cf.iplot(figure=fig, asUrl=True, filename=filename)
    return
예제 #5
0
def plot_series_to_compare(series1, series2, series1_name, series2_name,
                           title):
    """
    Plots Two series for easy comparison
    """

    fig = tls.make_subplots(rows=1,
                            cols=1,
                            shared_xaxes=True,
                            print_grid=False)
    fig['layout'].update(height=400, width=899, title=title)

    x_series = np.arange(0, len(series1))

    fig.append_trace(
        {
            'x': x_series,
            'y': series1,
            'type': 'scatter',
            'name': series1_name
        }, 1, 1)
    fig.append_trace(
        {
            'x': x_series,
            'y': series2,
            'type': 'scatter',
            'name': series2_name
        }, 1, 1)

    fig['layout']['xaxis'].update(title='Trading Week')
    fig['layout']['yaxis'].update(title='Price (Cents)')
    cf.iplot(fig)
예제 #6
0
def distplot(df: pd.DataFrame,
             bins=10,
             hist=True,
             kde=True,
             rug=False,
             color=None,
             as_figure=False,
             legend=True,
             title=True,
             grid=True,
             figsize=None,
             subplots=False,
             layout=None,
             sharex=False,
             sharey=False,
             **kwargs):
    """mimic seaborn.distplot"""
    df = pd.DataFrame(df).rename(columns=str)
    if not subplots:
        hist_data = [v.values for k, v in df.iteritems()]
        group_labels = df.columns.tolist()
        bin_size = (np.max(df.max()) - np.min(df.min())) / bins
        curve_type = 'kde' if kde else 'normal'
        fig = ff.create_distplot(hist_data=hist_data,
                                 group_labels=group_labels,
                                 bin_size=bin_size,
                                 curve_type=curve_type,
                                 colors=color,
                                 show_hist=hist,
                                 show_rug=rug,
                                 **kwargs)
    else:
        figures = [
            distplot1d(ss,
                       bins=bins,
                       hist=hist,
                       kde=kde,
                       rug=rug,
                       color=color,
                       as_figure=True,
                       **kwargs) for _, ss in df.iteritems()
        ]
        fig = tools.get_subplots(figures,
                                 sharex=sharex,
                                 sharey=sharey,
                                 layout=layout)

    fig['layout'].update(showlegend=legend, title=title)
    if figsize:
        fig['layout'].update(width=figsize[0], height=figsize[1])
    for k, v in fig['layout'].items():
        if 'axis' in k:
            v.update(showgrid=grid)
    if as_figure:
        return fig
    cf.iplot(fig)
예제 #7
0
파일: viewgen.py 프로젝트: divir94/bitcoin
    def returns_tear(self):
        """
        Create the following charts:
            1. Dollar cumulative return and price vs time
            2. Dollar return and price vs time
            3. Dollar return distribution
            4. View time series
            5. Mean return by view
        """
        df = self.result

        # cum return chart
        chart_df = df[['price', 'cum_return']].fillna(method='pad')
        chart_df.iplot(title='Cumulative Return and Price',
                       yTitle='Dollar Cumulative Return',
                       secondary_y='price',
                       secondary_y_title='Price')

        # return chart
        chart_df = df[['price', 'returns']].fillna(method='pad')
        chart_df.iplot(title='Return and Price',
                       yTitle='Dollar Return',
                       secondary_y='price',
                       secondary_y_title='Price')

        # return distribution chart
        chart_df = df.loc[df['view'] != 0, 'returns'].dropna()
        chart_df.iplot(title='Return Distribution',
                       kind='hist',
                       xTitle='Return',
                       yTitle='Occurrences')

        # view time series
        figure = df[['view', 'price']].iplot(title='View and Price',
                                             kind='area',
                                             fill=True,
                                             secondary_y='price',
                                             yTitle='View',
                                             secondary_y_title='Price',
                                             asFigure=True)
        figure['data'][0]['line']['shape'] = 'hv'
        cf.iplot(figure)

        # view statistics
        return_group = df.groupby(df.view)['fwd_price_change']
        view_stats = return_group.describe()
        view_stats['cum_return'] = return_group.sum()
        print view_stats
        view_stats['mean'].iplot(title='Mean Return by View',
                                 kind='bar',
                                 xTitle='View',
                                 yTitle='Mean Dollar Return')

        return
예제 #8
0
def plot_weekly_combined_series_by_date(df_weekly):
    """
    Plots weekly combined series (price series and cot report) using Plotly
    """

    fig = tls.make_subplots(rows=5,
                            cols=1,
                            shared_xaxes=True,
                            print_grid=False,
                            specs=[[{
                                'rowspan': 2
                            }], [None], [{
                                'rowspan': 2
                            }], [None], [{}]])

    for col in ['Settle']:
        fig.append_trace(
            {
                'x': df_weekly['Date'],
                'y': df_weekly[col],
                'type': 'scatter',
                'name': col
            }, 1, 1)
    for col in ['Open_Interest', 'Longs', 'Shorts']:
        fig.append_trace(
            {
                'x': df_weekly['Date'],
                'y': df_weekly[col],
                'type': 'scatter',
                'name': col
            }, 3, 1)

    for col in ['Volume']:
        fig.append_trace(
            {
                'x': df_weekly['Date'],
                'y': df_weekly[col],
                'type': 'bar',
                'name': col
            }, 5, 1)

    fig['layout']['xaxis'].update(title='Date')
    fig['layout']['yaxis1'].update(title='Price (Cents)')
    fig['layout']['yaxis2'].update(title='Open Interest')
    fig['layout']['yaxis3'].update(title='Volume')
    cf.iplot(fig)
예제 #9
0
def plot_cover(df_cover,
               df_cover_rand,
               label_rand,
               title='Percentual de cobertura'):
    """
    Plota a curva de precisão-recall para comparação de duas métricas de ordenamento dos alunos (ex: probabilidade de evasão do modelo e idade)
    
    df_cover, df_cover_rand : pandas.DataFrame
        Tabelas ordenadas com a métrica de ordenação, precision e recall (gerada pela função calculate_prec_recall)
        
    label_rand, title : str
        Nome do modelo de comparação (ex: random, idade) e titulo de grafico, respectivamente
    """

    # Plot
    data = [
        go.Scatter(x=df_cover_rand['perc_cover'],
                   y=df_cover_rand['precision'],
                   mode='markers',
                   marker=dict(color=colorscale[1], opacity=.7),
                   name='Precision ({})'.format(label_rand)),
        go.Scatter(x=df_cover_rand['perc_cover'],
                   y=df_cover_rand['recall'],
                   mode='markers',
                   marker=dict(color=colorscale[5], opacity=.7),
                   name='Recall ({})'.format(label_rand)),
        go.Scatter(x=df_cover['perc_cover'],
                   y=df_cover['precision'],
                   mode='lines',
                   marker=dict(color=colorscale[0]),
                   name='Precision'),
        go.Scatter(x=df_cover['perc_cover'],
                   y=df_cover['recall'],
                   mode='lines',
                   marker=dict(color=colorscale[4]),
                   name='Recall')
    ]

    layout = dict(xaxis=dict(
        title='% de alunos cobertos (ordenado pela prob. de evasão)'),
                  title=title)
    fig = go.Figure(data=data, layout=layout)
    cf.iplot(fig)
예제 #10
0
def plot_roc(y_test, y_score, title, colors=colorscale):
    """
    Plota a curva ROC para os dados de treino e teste (preditos).
    
    y_test : numpy.array
    y_score : pandas.DataFrame (scores dos modelos a serem comparados)
    
    title : str
    """

    ref = np.linspace(0, 1)
    data = [
        go.Scatter(x=ref,
                   y=ref,
                   mode='markers',
                   marker=dict(color='grey'),
                   showlegend=False)
    ]

    i = 0
    for model in y_score.columns:
        # Compute ROC curve and ROC area for each class
        fpr, tpr, _ = roc_curve(y_test, y_score[model], pos_label=1)
        roc_auc = auc(fpr, tpr)

        data.append(
            go.Scatter(x=fpr,
                       y=tpr,
                       mode='lines',
                       marker=dict(color=colors[i]),
                       name='{} (area = {})'.format(model, round(roc_auc, 2))))
        i += 1

    layout = dict(xaxis=dict(title='Taxa de falsos positivos (TFP)'),
                  yaxis=dict(title='Taxa de verdadeiros positivos (TVP)'),
                  title=title)

    fig = go.Figure(data=data, layout=layout)
    cf.iplot(fig)
예제 #11
0
def mplot(df: pd.DataFrame,
          kind='scatter',
          subplots=False,
          sharex=False,
          sharey=False,
          layout=(1, -1),
          figsize=None,
          title=None,
          xlabel=True,
          ylabel=True,
          legend=True,
          xlim=None,
          ylim=None,
          as_figure=False,
          grid=True,
          **kwargs):
    name = df.columns.name if isinstance(df, pd.DataFrame) else df.name
    title = title or name
    xTitle = xlabel
    if isinstance(xlabel, bool) and xlabel:
        xTitle = df.index.name
    yTitle = ylabel
    if isinstance(ylabel, bool) and ylabel:
        yTitle = name

    dist_kws = kwargs.pop('dist_kws', dict())
    if kind == 'dist':
        fig = distplot(df,
                       as_figure=True,
                       subplots=subplots,
                       sharex=sharex,
                       sharey=sharey,
                       legend=legend,
                       title=title,
                       layout=layout,
                       grid=grid,
                       figsize=figsize,
                       **dist_kws)
        # fig['layout'].update(width=figsize[0], height=figsize[1])
    else:
        fig = df.iplot(kind=kind,
                       subplots=subplots,
                       shared_xaxes=sharex,
                       shared_yaxes=sharey,
                       showlegend=legend,
                       dimensions=figsize,
                       title=title,
                       xTitle=xTitle,
                       yTitle=yTitle,
                       shape=layout,
                       asFigure=True,
                       xrange=xlim,
                       yrange=ylim,
                       **kwargs)

    layout = fig.setdefault('layout', dict())
    layout.update(showlegend=legend)

    for k, v in layout.items():
        if k.startswith(('xaxis', 'yaxis')):
            v.update(showgrid=grid)
            if True:
                if k.startswith('xaxis'):
                    v.update(title=xTitle)
                    if xlim:
                        v.update(range=xlim)
                if k.startswith('yaxis'):
                    v.update(title=yTitle)
                    if ylim:
                        v.update(range=ylim)
    if as_figure:
        return fig

    cf.iplot(fig)
예제 #12
0
def pairplot(df: pd.DataFrame,
             kind='scatter',
             diag_kind='dist',
             theme=None,
             bins=10,
             color='grey',
             size=2,
             figsize=None,
             title=False,
             as_figure=False,
             sharex=False,
             sharey=False,
             grid=False):
    """"""
    if not theme:
        theme = cf.auth.get_config_file()['theme']

    figs = []
    for coly in df.columns:
        for colx in df.columns:
            if colx == coly:
                if diag_kind == 'dist':
                    fig = distplot(df[colx],
                                   hist=False,
                                   as_figure=True,
                                   rug=False,
                                   bins=bins)
                else:
                    fig = df.iplot(kind=diag_kind,
                                   keys=[colx],
                                   asFigure=True,
                                   bins=bins)
                figs.append(fig)
            else:
                figs.append(
                    df.iplot(kind=kind,
                             mode='markers',
                             x=colx,
                             y=coly,
                             asFigure=True,
                             size=size,
                             colors=[color]))
    layout = tools.get_layout(theme=theme)
    layout['xaxis1'].update(showgrid=grid)
    layout['yaxis1'].update(showgrid=grid)
    fig = tools.get_subplots(figs,
                             layout=(len(df.columns), len(df.columns)),
                             sharex=sharex,
                             sharey=sharey,
                             horizontal_spacing=.05,
                             vertical_spacing=.07,
                             base_layout=layout)

    if isinstance(title, bool) and title:
        fig['layout'].update(title=df.columns.name)
    elif title:
        fig['layout'].update(title=title)
    if figsize:
        fig['layout'].update(width=figsize[0], height=figsize[1])
    fig['layout'].update(bargap=.02, showlegend=False)
    if as_figure:
        return fig
    cf.iplot(fig)
예제 #13
0
df.iplot(subplots=True, subplot_titles=True, legend=False)

df = cf.datagen.bubble(10, 50, mode='stocks')
figs = cf.figures(df, [
    dict(kind='histogram', keys='x', color='blue'),
    dict(kind='scatter', mode='markers', x='x', y='y', size=5),
    dict(kind='scatter', mode='markers', x='x', y='y', size=5, color='teal')
],
                  asList=True)
figs.append(
    cf.datagen.lines(1).figure(bestfit=True,
                               colors=['blue'],
                               bestfit_colors=['pink']))
base_layout = cf.tools.get_base_layout(figs)
sp = cf.subplots(
    figs,
    shape=(3, 2),
    base_layout=base_layout,
    vertical_spacing=.15,
    horizontal_spacing=.03,
    specs=[[{
        'rowspan': 2
    }, {}], [None, {}], [{
        'colspan': 2
    }, None]],
    subplot_titles=['Histogram', 'Scatter 1', 'Scatter 2', 'Bestfit Line'])
sp['layout'].update(showlegend=False)
cf.iplot(sp)

help(df.iplot)
예제 #14
0
    autosize=True,
    showlegend=True,
    legend=dict(
        font=dict(
            family='sans-serif',
            #size=20,
            color='#000'), ),
    title=dict(
        text="Global Fatalities",
        font=(
            dict(  #size=40,
                color='#000'))),
    # Add annotations in the center of the donut pies.
    annotations=[dict(text=total_deaths, align="center", showarrow=False)])

cf.iplot(figure=fig, filename=plot_folder + "/death", asUrl=True)


def plot_fit(series, filename):
    y = series.tolist()
    x = np.array(range(len(y)))
    gauss = Gauss(x, y)
    y_pred = gauss.fit()
    fit_series = pd.Series(y_pred, series.index, name="Fitted Curve")

    _, m, s = gauss.par
    current = int(sum(y))
    estimate = gauss.estimate_total()

    fig1 = series.iplot(kind="bar", asFigure=True)
    fig2 = fit_series.iplot(asFigure=True,