Example #1
0
def feature_scatterplot(fset_path, features_to_plot):
    """Create scatter plot of feature set.

    Parameters
    ----------
    fset_path : str
        Path to feature set to be plotted.
    features_to_plot : list of str
        List of feature names to be plotted.

    Returns
    -------
    (fig.data, fig.layout)
        Returns (fig.data, fig.layout) where `fig` is an instance of
        `plotly.tools.FigureFactory`.
    """
    with featureset.from_netcdf(fset_path, engine=cfg['xr_engine']) as fset:
        feat_df = fset.to_dataframe()
        feat_df = feat_df[features_to_plot]

        if 'target' in fset:
            feat_df['target'] = fset.target.values
            index = 'target'
        else:
            index = None

    # TODO replace 'trace {i}' with class labels
    fig = FF.create_scatterplotmatrix(feat_df, diag='box', index=index,
                                      height=800, width=800)

    py.plot(fig, auto_open=False, output_type='div')

    return fig.data, fig.layout
def plot_heatmap(samples_dict, output_path, cancer, df, genes, scale='binary'):
    # Plotting the heatmap
    if scale == 'binary':
        colorscale = [[0, "rgb(111, 168, 220)"], [1, "rgb(5, 10, 172)"]]
        colorbar = {'tick0': 0,'dtick': 1}
    elif scale == 'logarithmic':
        colorscale = [[0, 'rgb(250, 250, 250)'],
                      [1./(4**4), 'rgb(200, 200, 200)'],
                      [1./(4**3), 'rgb(150, 150, 150)'],
                      [1./(4**2), 'rgb(100, 100, 100)'],
                      [1./4, 'rgb(50, 50, 50)'],
                      [1., 'rgb(0, 0, 0)']]
        colorbar = {'tick0': 0,
                    'tickmode': 'array',
                    'tickvals': [0, 4, 16, 64, 256]}
        
    heatmap_trace = go.Heatmap(z=[df[i] for i in genes], y=genes, x=df.patient_id, showscale=True, colorscale=colorscale, colorbar=colorbar)
    mutation_load_trace = go.Bar(x=df.patient_id, y=df.somatic_mutations_count/30.0)
    fig = tls.make_subplots(rows=29, cols=1, specs=[[{'rowspan':5, 'colspan' : 1}]] + [[None]] * 4 + [[{'rowspan' : 24, 'colspan' : 1}]] + [[None]] * 23)
    fig.append_trace(mutation_load_trace, 1, 1)
    fig.append_trace(heatmap_trace, 6, 1)
    fig['layout']['xaxis1'].update(showticklabels = False)
    fig['layout']['xaxis1'].update(zeroline = False, showgrid=False)
    fig['layout']['yaxis1'].update(zeroline = False, showgrid = False, tickfont=dict(family='Arial', size=4))
    fig['layout']['xaxis2'].update(showticklabels = False)
    fig['layout']['xaxis2'].update(zeroline = False, showgrid=False)
    fig['layout']['yaxis2'].update(zeroline = False, showgrid = False, tickfont=dict(family='Arial', size=4))
    plot(fig, auto_open=False, filename="%s_%s_heatmap.html" % (output_path, cancer))
    
    plot_clustered_heatmap(df, genes, cancer, output_path, scale)
def plotly_js_viz(word_2_vec_model):
    tsne_model=TSNE(n_components=2,random_state=5)
    data=tsne_model.fit_transform(word_2_vec_model.syn0)
    xd=list(data[:,0])
    yd=list(data[:,1])
    names_our=word_2_vec_model.index2word
    plot([Scatter(x=xd,y=yd,mode="markers",text=names_our)])
def ranking_trend(billboard):
    #set the dataframe
    df2 = [rows for _, rows in billboard.groupby('Genre')]
    df3 = [rows for _, rows in df2[7].groupby('artist')]
    artist_name = ['Drake','Adele','Rihanna','Lady Gaga','Kanye West','Calvin Harris']
    data = [0]*len(artist_name)
    count = 0
    
    for i in range(len(df3)):
        if list(df3[i]['artist'])[0] in artist_name:
            # Create traces
            data[count]= go.Scatter(
                x = df3[i]['Date'],
                y = df3[i]['rank'],
                mode = 'lines+markers',
                name = list(df3[i]['artist'])[0]
            )
            count = count+1
    layout = go.Layout(
        title='Billboard Ranking Trend',
        xaxis=dict(title='Time'),
        yaxis=dict(title='Rank')
    )
    data1 = go.Figure(data=data, layout=layout)
    plot(data1, filename='line-mode.html')
Example #5
0
def plot_flux_vs_Q(filename, labelX, path='data/analysis_files/', pathexport='graphs/analysis/', plotTitle='$$$$$'):
    # plots flux vs time given a flux file written according to flux.py
    # determine plot title
    if plotTitle == '$$$$$':
        plotTitle = 'Flux vs ' + labelX + ' ' + filename[0:4] + ' channel ' + filename[5]

    df = pd.read_csv(path+filename, skiprows=[0,1], header=0, delim_whitespace=True)

    trace1 = Scatter(
        x=df[labelX],
        y=df['Flux'],
        mode='markers'
    )

    layout = Layout(
        title=plotTitle,
        xaxis=dict(
            title=labelX
        ),
        yaxis=dict(
            title='Flux (events/60s*m^2)'
        ),
        width=2000,
        height=600,
    )

    data = [trace1]

    fig = dict(data=data, layout=layout)
    plot(fig, filename=pathexport+filename+'_plot.html',auto_open=True)
    return pathexport+filename+'_plot.html'
Example #6
0
def make_panel_plot(basepath, outf, dataset=None, atlas=None):
    fnames = [name for name in os.listdir(basepath)
              if os.path.splitext(name)[1] == '.pkl']
    fnames = sorted(fnames)
    paths = [os.path.join(basepath, item) for item in fnames]
    keys = ["_".join(n.split('.')[0].split('_')[1:]) for n in fnames]
    ylabs = ['Count', 'Count', 'Count', 'Count',
             'Eigenvalue', 'Count', 'Count', 'Portion of Total Variance']
    xlabs = ['Betweenness Centrality', 'Clustering Coefficient', 'Degree',
             'Edge Weight', 'Dimension', 'Number of Non-zeros',
             'Locality Statistic-1', 'Dimension']

    traces = list(())
    for idx, curr in enumerate(paths):
        f = open(curr)
        dat = pickle.load(f)[keys[idx]]
        f.close()
        if keys[idx] == 'number_non_zeros':
            fig = pp.plot_rugdensity(dat.values())
        elif keys[idx] == 'eigen_sequence' or keys[idx] == 'scree_eigen':
            dims = len(dat.values()[0])
            fig = pp.plot_series(dat.values())
        else:
            xs = dat['xs'].values()
            ys = dat['pdfs'].values()
            fig = pp.plot_density(xs, ys)
        traces += [pp.fig_to_trace(fig)]

    multi = pp.traces_to_panels(traces)
    for idx, curr, in enumerate(paths):
        key = 'axis%d' % (idx+1)
        d = multi.layout['x'+key]['domain']
        multi.layout['x'+key]['domain'] = [d[0], d[1]-0.0125]
        multi.layout['y'+key]['zeroline'] = False
        multi.layout['x'+key]['zeroline'] = False
        multi.layout['x'+key]['title'] = xlabs[idx]
        multi.layout['y'+key]['title'] = ylabs[idx]
        multi.layout['x'+key]['nticks'] = 3
        multi.layout['y'+key]['nticks'] = 3
        if idx in [0, 2, 3, 6]:
            multi.layout['x'+key]['type'] = 'log'
            multi.layout['x'+key]['title'] += ' (log scale)'
        if idx in [4, 7]:
            multi.layout['x'+key]['range'] = [1, dims]
            multi.layout['x'+key]['tickvals'] = [1, dims/2, dims]
            if idx in [7]:
                multi.layout['y'+key]['range'] = [0, 1]
                multi.layout['y'+key]['tickvals'] = [0, 0.5, 1]
        if idx in [1]:
            multi.layout['x'+key]['range'] = [0, 1]

    if dataset is not None and atlas is not None:
        if atlas == 'desikan':
            atlas = atlas.capitalize()
        tit = dataset + ' Dataset (' + atlas + ' parcellation)'
    else:
        tit = None
    multi.layout['title'] = tit
    # iplot(multi, validate=False)
    plot(multi, validate=False, filename=outf+'.html')
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)
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)
Example #9
0
 def get_plotly_path_period_return(self, file_name='period_return.html'):
     path_plotly = self.path_dir_plotly_html + os.sep + file_name
     
     本基金 = [-2.59,-2.59,-11.07,8.66,-5.84]
     沪深300 = [1.64,1.64,0.73,5.01,14.20]
     同类平均 = [-0.79,-0.79,-2.08,0.76,7.03]
     trace1 = go.Bar(
         x=['今年以来', '最近一个月', '最近三个月', '最近半年', '最近一年'],
         y=本基金,
         name='本基金'
     )
     trace2 = go.Bar(
         x=['今年以来', '最近一个月', '最近三个月', '最近半年', '最近一年'],
         y=沪深300,
         name='沪深300'
     )
     
     trace3 = go.Bar(
         x=['今年以来', '最近一个月', '最近三个月', '最近半年', '最近一年'],
         y=同类平均,
         name='同类平均'
     )
     
     data = [trace1, trace2, trace3]
     layout = go.Layout(
         barmode='group'
     )
     
     fig = go.Figure(data=data, layout=layout)
     pyof.plot(fig,  filename=path_plotly, auto_open=False)
     return path_plotly
Example #10
0
def draw_map(lon, lat, text, titre="NO TITLE"):
    """
    Take 3 list as input, and the title of the map.
    """

    py.plot(
        {  # use `py.iplot` inside the ipython notebook
            "data": [
                {
                    "type": "scattergeo",
                    "locationmode": "france",
                    "lon": lon,
                    "lat": lat,
                    "text": text,
                    "mode": "markers",
                    "marker": dict(size=8, opacity=0.8, line=dict(width=1, color="rgb(102,102,102)")),
                }
            ],
            "layout": {
                "title": str(titre),
                "geo": dict(
                    scope="europe",
                    projection=dict(type="albers europe"),
                    showland=True,
                    landcolor="rgb(200, 200, 200)",
                    subunitcolor="rgb(217, 217, 217)",
                    countrycolor="rgb(217, 217, 217)",
                    countrywidth=1,
                    subunitwidth=1,
                ),
            },
        },
        filename="interactive_map",  # name of the file as saved in your plotly account
        # sharing='public'
    )
Example #11
0
 def get_plotly_path_lagest_back(self, file_name='lagest_back.html'):
     path_plotly = self.path_dir_plotly_html + os.sep + file_name
     
     lagest_down = [-3.74, -3.736, -3.736, -5.969, -5.969]
     lagest_back = [-6.29, -6.285, -6.042, -11.651, -13.942]
     
     std = [10.271, 8.552, 9.123, 10.839, 10.529]
     
     xticks = ['2016/9', '2016/10','2016/11','2016/12','2017/1',]
     
     trace1 = go.Bar(
         x=xticks,
         y=lagest_down,
         name='最大下跌'
     )
     trace2 = go.Bar(
         x=xticks,
         y=lagest_back,
         name='最大回撤'
     )
     
     data = [trace1, trace2]
     layout = go.Layout(barmode='group')
     
     fig = go.Figure(data=data,layout=layout)
     pyof.plot(fig,  filename=path_plotly, auto_open=False)
     return path_plotly
Example #12
0
def ribbonPlot(flattened3d,yearlist):
    y_raw = list(set(yearlist))
    nartists = flattened3d.shape[1]-1
    traces = []
    for i in range(1, nartists):
        z_raw = flattened3d[:,i]
        ci = int(255/nartists*i)
        x = []
        y = []
        z = []
        for j in range(0,len(z_raw)):
            z.append([z_raw[j],z_raw[j]])
            y.append([y_raw[j],y_raw[j]])
            x.append([i*2,i*2+1])
        traces.append(dict(
            z=z,
            x=x,
            y=y,
            colorscale=[[i,'rgb(%d,%d,255)'%(ci, ci)] for i in np.arange(0,1.1,0.1) ],
            showscale=False,
            type='surface',
        ))

    fig = {'data':traces,'layout':{'title':'Frequency of Rare Words by Year'}}
    plot(fig)
Example #13
0
def plot_insert_size_distribution(traces_list, outdir):
    """
    Collect the data to produce a distribution plot of the insert sizes

    Parameters
    ----------
    traces_list : list
        List of plot traces (plotly.graph_objs.Scatter)
    outdir : str
        Path to the output directory

    Returns
    -------
    """

    plot_off.plot({"data": traces_list,
                   "layout": graph_obj.Layout(title="Insert size distribution",
                                              xaxis=dict(title='Insert size'),
                                              yaxis=dict(title='Frequency'))
                   },
                  show_link=False,
                  output_type='file',
                  filename=os.path.join(outdir, 'insert_size_distribution.html'),
                  include_plotlyjs=True,
                  auto_open=False,
                  )
Example #14
0
def _example_main(input_file, output_file, **kwargs):
    """
    This func should be imported from your python package.

    This should have *no* dependency on the pbcommand IO, such as the RTC/TC models.
    """

    # This is just for test purposes
    log.info("Running example main with {i} {o} kw:{k}".format(i=input_file,
                                                               o=output_file,
                                                               k=kwargs))

    # Open dset CSV. Store absolute path of each alignment set.
    dset_paths = _get_dset_paths(input_file[0])

    # Open plots CSV. Store names of plots to produce.
    plots_to_generate = _get_plots_to_generate(input_file[1])

    dsets_kpis = {}
    for f in dset_paths:
        dset = openDataSet(dset_paths[f]['aset'])
        subsampled_dset = _subsample_alignments(dset)
        dsets_kpis[f] = _getKPIs(dset, subsampled_dset)

    figures = []
    # figure tuple has form (plot_group_id, plot_id, figure)
    if 'accuracy_vs_readlength' in plots_to_generate:
        figures.append(('accuracy', 'accuracy_vs_readlength', accuracy_plots._plot_accuracy_vs_readlength(dsets_kpis)))
    if 'accuracy' in plots_to_generate:
        figures.append(('accuracy', 'accuracy', accuracy_plots._plot_accuracy_distribution(dsets_kpis)))
    if 'accuracy_boxplot' in plots_to_generate:
        figures.append(('accuracy', 'accuracy_boxplot', accuracy_plots._plot_accuracy_boxplots(dsets_kpis)))

    all_plots = {} # dictionary of plots. keys are groups
    for plot_group, plot_id, fig in figures:
        if plot_group not in all_plots.keys():
            all_plots[plot_group] = []
        plot(fig, filename='{i}.html'.format(i=plot_id), show_link=False, auto_open=False)
        phantomjs_driver.set_window_size(1920, 1080)
        phantomjs_driver.get('{i}.html'.format(i=plot_id))
        phantomjs_driver.save_screenshot('{i}.png'.format(i=plot_id))
        phantomjs_driver.get('{i}.html'.format(i=plot_id))
        phantomjs_driver.save_screenshot('{i}_thumb.png'.format(i=plot_id))
        os.remove('{i}.html'.format(i=plot_id))
        plot_path = '{i}.png'.format(i=plot_id)
        thumb_path = '{i}_thumb.png'.format(i=plot_id)
        all_plots[plot_group].append(Plot(plot_id, plot_path, thumbnail=thumb_path))

    plot_groups = []
    for plot_group_title in all_plots.keys():
        plot_group = PlotGroup( plot_group_title, plots=all_plots[plot_group_title])
        plot_groups.append(plot_group) 

    report = Report('mh_toy', tables=(), plotgroups=plot_groups, attributes=())
    report.write_json( output_file )

    phantomjs_driver.quit()

    return 0
Example #15
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 pl(df, r, var):
    tmp = df[df.randomSeed.isin(r)]
    plot(px.line(tmp, height=300 * len(r), x="tick",
             y = var, 
             color="FirmNumID",
             line_dash="scenario",
             facet_row="randomSeed"
             ))
Example #17
0
def ptly3d(trace, fileName, off=True, labs=['$x$', '$y$', '$z$'],
           showLegend=False):
    layout = ptlyLayout3d(labs=labs, showLegend=showLegend)
    fig = go.Figure(data=trace, layout=layout)
    if off:
        pyoff.plot(fig, filename=fileName)
    else:
        py.plot(fig, filename=fileName, fileopt='overwrite')
Example #18
0
def render_plotly_viz(data, filename='/tmp/temp-viz.html', show_link=False, auto_open=True, **kwargs):
    offline.plot(
        data, filename=filename,
        show_link=False, auto_open=False,
        **kwargs
    )

    return clean_plotly_viz(filename, auto_open=auto_open)
Example #19
0
    def save(self, filename=None, xLimits=None):
        """
        Saves all plots and their data points that have been added to the
        plotFactory.

        Args:
        filename (str): Name for the output file. Default = "spectrum_plot.html"
        mz_range (tuple): m/z range which should be considered [start, end].
            Default = None
        """
        plot_number = len(self.plots)

        rows, cols = int(math.ceil(plot_number / float(2))), 2

        if plot_number % 2 == 0:
            my_figure = tools.make_subplots(
                rows=rows, cols=cols, vertical_spacing=0.6 / rows
            )
        else:
            specs = [[{}, {}] for x in range(rows - 1)]
            specs.append([{"colspan": 2}, None])
            my_figure = tools.make_subplots(
                rows=rows,
                cols=cols,
                vertical_spacing=0.6 / rows,
                specs=specs,
                subplot_titles=self.titles,
            )

        for i, plot in enumerate(self.plots):
            for j, trace in enumerate(plot):
                my_figure.append_trace(trace, int(math.floor((i / 2) + 1)), (i % 2) + 1)

        for i in range(plot_number):
            if xLimits:
                my_figure["layout"]["xaxis" + str(i + 1)].update(range=xLimits[i])
            my_figure["layout"]["xaxis" + str(i + 1)].update(title="m/z ")
            my_figure["layout"]["yaxis" + str(i + 1)].update(title="Intensity")
            my_figure["layout"]["xaxis" + str(i + 1)].update(
                titlefont={"color": "#000000", "family": "Helvetica", "size": "18"}
            )
            my_figure["layout"]["yaxis" + str(i + 1)].update(
                titlefont={"color": "#000000", "family": "Helvetica", "size": "18"}
            )

        my_figure["layout"]["legend"].update(font={"size": 10, "color": "#FF0000"})

        if self.filename is None:
            _filename = "spectrum_plot.html"
        else:
            _filename = self.filename

        if filename is not None:
            # save fkt name definition overrules original filename
            _filename = filename

        plt.plot(my_figure, filename=_filename, auto_open=False)
        return
Example #20
0
def start160Parralel():
    import plotly.plotly as py
    from plotly.graph_objs import *
    py.sign_in('username', 'api_key')
    trace1 = Box(
        y=[17.107424020767212, 23.18073296546936, 23.362980842590332, 23.812504053115845, 23.346518993377686],
        boxmean='sd',
        marker=Marker(
            color='rgb(8, 81, 156)'
        ),
        name='Mininethosts (1W)',
        ysrc='setchring:103:df6b28'
    )
    trace2 = Box(
        y=[11.409734010696411, 14.017661094665527, 14.139786958694458, 14.00459909439087, 13.936722993850708],
        boxmean='sd',
        marker=Marker(
            color='rgb(8, 81, 156)'
        ),
        name='Mininethosts (2W)',
        ysrc='setchring:103:e9dd30'
    )
    trace3 = Box(
        y=[62.070091009140015, 56.22917199134827, 56.44520902633667, 57.48769211769104, 58.8430700302124],
        boxmean='sd',
        marker=Marker(
            color='rgb(10, 140, 208)'
        ),
        name='Ubuntu Container (1W)',
        ysrc='setchring:103:5a50e4'
    )
    trace4 = Box(
        y=[32.30044984817505, 30.182456016540527, 30.279242038726807, 30.52851104736328, 30.67842388153076],
        boxmean='sd',
        marker=Marker(
            color='rgb(10, 140, 208)'
        ),
        name='Ubuntu Container (2W)',
        ysrc='setchring:103:58e6b9'
    )
    data = Data([trace1, trace2, trace3, trace4])
    layout = Layout(
        title='Startvergleich mit 160 Hosts bzw. Containern'
    )
    layout = Layout(
        showlegend=False,
        title='Startvergleich mit 160 Hosts bzw. Containern (Parallel)',
        yaxis=dict(
            range=[0, 70],
            showgrid=True,
            zeroline=True,
            title="Startzeit in Sekunden"
        ),
        xaxis=dict(tickangle=45))
    fig = Figure(data=data, layout=layout)
    from plotly.offline import init_notebook_mode, plot
    init_notebook_mode()
    plot(fig, image='svg', filename='startvergleich160paralel.html')
Example #21
0
    def plotly_pca(self, plotFile, Wt, pvar, PCs, eigenvalues, cols, plotTitle):
        """
        A plotly version of plot_pca, that's called by it to do the actual plotting
        """
        fig = go.Figure()
        fig['layout']['xaxis1'] = {'domain': [0.0, 0.48], 'anchor': 'x1', 'title': 'PC{} ({:4.1f}% of var. explained)'.format(PCs[0], 100.0 * pvar[PCs[0] - 1])}
        fig['layout']['yaxis1'] = {'domain': [0.0, 1.0], 'anchor': 'x1', 'title': 'PC{} ({:4.1f}% of var. explained)'.format(PCs[1], 100.0 * pvar[PCs[1] - 1])}
        fig['layout']['xaxis2'] = {'domain': [0.52, 1.0], 'title': 'Principal Component'}
        fig['layout']['yaxis2'] = {'domain': [0.0, 1.0], 'anchor': 'x2', 'title': 'Eigenvalue', 'rangemode': 'tozero', 'showgrid': False}
        fig['layout']['yaxis3'] = {'domain': [0.0, 1.0], 'anchor': 'x2', 'title': 'Cumulative variability', 'rangemode': 'tozero', 'side': 'right', 'overlaying': 'y2'}
        fig['layout'].update(title=plotTitle)

        # PCA
        if cols is not None:
            colors = itertools.cycle(cols)
        n = len(self.labels)
        data = []
        for i in range(n):
            trace = go.Scatter(x=[Wt[PCs[0] - 1, i]],
                               y=[Wt[PCs[1] - 1, i]],
                               mode='marker',
                               xaxis='x1',
                               yaxis='y1',
                               name=self.labels[i])
            trace['marker'].update(size=20)
            if cols is not None:
                trace['marker'].update(color=next(colors))
            data.append(trace)

        # Scree plot
        trace = go.Bar(showlegend=False,
                       name='Eigenvalues',
                       x=range(1, n + 1),
                       y=eigenvalues[:n],
                       xaxis='x2',
                       yaxis='y2')
        data.append(trace)

        # Cumulative variability
        trace = go.Scatter(showlegend=False,
                           x=range(1, n + 1),
                           y=pvar.cumsum()[:n],
                           mode='lines+markers',
                           name='Cumulative variability',
                           xaxis='x2',
                           yaxis='y3',
                           line={'color': 'red'},
                           marker={'symbol': 'circle-open-dot', 'color': 'black'})
        data.append(trace)

        annos = []
        annos.append({'yanchor': 'bottom', 'xref': 'paper', 'xanchor': 'center', 'yref': 'paper', 'text': 'PCA', 'y': 1.0, 'x': 0.25, 'font': {'size': 16}, 'showarrow': False})
        annos.append({'yanchor': 'bottom', 'xref': 'paper', 'xanchor': 'center', 'yref': 'paper', 'text': 'Scree plot', 'y': 1.0, 'x': 0.75, 'font': {'size': 16}, 'showarrow': False})

        fig['data'] = data
        fig['layout']['annotations'] = annos
        offline.plot(fig, filename=plotFile, auto_open=False)
 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)
Example #23
0
def plot_df(df, title):
  ax = ['x', 'y', 'z']
  data = [Scatter(y=df[a], name=a) for a in ax]

  layout = Layout(title=title,
                  xaxis=XAxis(title='Timestep'),
                  yaxis=YAxis(title='{x, y, z} coordinates in Gs'))

  plot({'data': data, 'layout': layout}, show_link=False)
def compare(names,columns):
    try:
        data = pd.concat([pd.read_csv(company, index_col=0, parse_dates=True) for company in names], axis=1, keys=names)
    except(FileNotFoundError, IOError):
        print('Wrong file or file path.')
        return None
    data = data.ix[:,data.columns.get_level_values(1).isin(set(columns))]
    fig = data.iplot(theme='pearl',kind='scatter',title='Line Plot of {} of {}.'.format(','.join(columns), ','.join([s.strip('.csv') for s in names])),subplots=True,asFigure=True)
    py.plot(fig,filename='../../plots/'+compareplot,validate=False,auto_open=False)
def plot_clustered_heatmap(df, genes_list, cancer, output_path, scale='binary'):
    # Build nxm matrix (n samples, m genes)
    X = df[genes_list].as_matrix().transpose()
    
    if scale == 'binary':
        Z = linkage(X, method='complete', metric='hamming')
        colorscale = [[0, "rgb(111, 168, 220)"], [1, "rgb(5, 10, 172)"]]
        colorbar = {'tick0': 0,'dtick': 1}
    elif scale == 'logarithmic':
        Z = linkage(X, method='ward')
        X_max = X.max()
        colorscale = [[0, 'rgb(250, 250, 250)'],
                      [1./X_max, 'rgb(200, 200, 200)'],
                      [5./X_max, 'rgb(150, 150, 200)'],
                      [20./X_max, 'rgb(100, 100, 200)'],
                      [100./X_max, 'rgb(50, 50, 200)'],
                      [1., 'rgb(0, 0, 200)']]
        colorbar = {'tick0': 0,
                    'tickmode': 'array',
                    'tickvals': [0, 1, 5, 20, 100, X_max]}
    c, coph_dists = cophenet(Z, pdist(X))
    print "Cophenetic Correlation Coefficient:", c
    
    #layout = go.Layout(yaxis=dict(title='%s germline mutations (ordered by samples somatic mutation load)'% cancer, zeroline=False))    
#    fig = pylab.figure(figsize=(8,8))
#    ax1 = fig.add_axes([0.09,0.1,0.2,0.6])
#    ax1.set_xticks([])
#    ax1.set_yticks([])
#    axmatrix = fig.add_axes([0.3,0.1,0.6,0.6])
    den = dendrogram(Z, orientation='left')
    idx = den['leaves']
    X = X[idx,:]
    print "X shape:", X.shape
    genes_ordered = [genes_list[i] for i in idx]
    logger.info("ordered genes: %s", str(genes_ordered))
    
#    im = axmatrix.matshow(X, aspect='auto', origin='lower', cmap=pylab.cm.YlGnBu)
#    axmatrix.set_xticks([])
#    axmatrix.set_yticks([])
#    # Plot colorbar.
#    axcolor = fig.add_axes([0.91,0.1,0.02,0.6])
#    pylab.colorbar(im, cax=axcolor)
#    fig.savefig(output_path)
    
    # Plotting the heatmap (without the hirarchy)
    heatmap_trace = go.Heatmap(z=X.tolist(), x=df.patient_id, y=genes_ordered, showscale=True, colorscale=colorscale, colorbar=colorbar)
    mutation_load_trace = go.Bar(x=df.patient_id, y=df.somatic_mutations_count/30.0)
    fig = tls.make_subplots(rows=29, cols=1, specs=[[{'rowspan':5, 'colspan' : 1}]] + [[None]] * 4 + [[{'rowspan' : 24, 'colspan' : 1}]] + [[None]] * 23)
    fig.append_trace(mutation_load_trace, 1, 1)
    fig.append_trace(heatmap_trace, 6, 1)
    fig['layout']['xaxis1'].update(showticklabels = False)
    fig['layout']['xaxis1'].update(zeroline = False, showgrid=False)
    fig['layout']['yaxis1'].update(zeroline = False, showgrid = False, tickfont=dict(family='Arial', size=4))
    fig['layout']['xaxis2'].update(showticklabels = False)
    fig['layout']['xaxis2'].update(zeroline = False, showgrid=False)
    fig['layout']['yaxis2'].update(zeroline = False, showgrid = False, tickfont=dict(family='Arial', size=4))
    plot(fig, auto_open=False, filename="%s_%s_heatmap_clustered.html" % (output_path, cancer))
Example #26
0
def main():
    """
    main function
    """

    def fun(v_x, params):
        """
        function
        """
        p_a, = params
        return p_a * v_x ** 2

    def residual(params, v_x, v_y):
        """
        residual
        """
        return fun(v_x, params) - v_y

    x_i = np.array([0.5, 1.0, 1.5, 2.0, 2.5])
    y_i = np.array([0.7, 3.4, 7.2, 12.4, 20.1])

    fit_xy = optimize.leastsq(residual, [1], (x_i, y_i))
    y_i_r = fun(x_i, fit_xy[0])

    fig = dict(
        data=[
            dict(
                type='scatter',
                x=x_i,
                y=y_i_r,
                name='y = ax^2',
                line=dict(
                    shape='spline'
                )
            ),
            dict(
                type='scatter',
                x=x_i,
                y=y_i,
                name='real',
                line=dict(
                    dash='dot',
                    color='red',
                ),
                marker=dict(
                    symbol='x',
                    size=10
                ),
            )
        ],
        layout=dict(
            title='Least-square fitting test'
        )
    )
    print(fit_xy[0])
    py.plot(fig, filename='./08/m0801.html')
Example #27
0
def start160Sequenziell():
    import plotly.plotly as py
    from plotly.graph_objs import *
    py.sign_in('username', 'api_key')
    trace1 = Box(
        y=[16.710778951644897, 23.12172794342041, 23.15219497680664, 23.376353979110718, 23.672327041625977],
        boxmean='sd',
        marker=Marker(
            color='rgb(8, 81, 156)'
        ),
        name='Mininethosts (1W)',
        ysrc='setchring:81:588b00'
    )
    trace2 = Box(
        y=[18.521923065185547, 18.818742036819458, 18.903568029403687, 19.197312116622925, 19.296133041381836],
        boxmean='sd',
        marker=Marker(
            color='rgb(8, 81, 156)'
        ),
        name='Mininethosts (2W)',
        ysrc='setchring:81:188a28'
    )
    trace3 = Box(
        y=[55.71610999107361, 50.11128902435303, 51.42167401313782, 52.32876491546631, 53.01005816459656],
        boxmean='sd',
        marker=Marker(
            color='rgb(10, 140, 208)'
        ),
        name='Ubuntu Container (1W)',
        ysrc='setchring:81:fb5f3d'
    )
    trace4 = Box(
        y=[46.878589153289795, 47.398112058639526, 47.66672992706299, 48.80990219116211, 49.06117510795593],
        boxmean='sd',
        marker=Marker(
            color='rgb(10, 140, 208)'
        ),
        name='Ubuntu Container (2W)',
        ysrc='setchring:81:80411e'
    )
    data = Data([trace1, trace2, trace3, trace4])
    layout = Layout(
        showlegend=False,
        title='Startvergleich mit 160 Hosts bzw. Containern (Sequenziell)',
    yaxis = dict(
        range=[0, 60],
        showgrid=True,
        zeroline=True,
        title="Startzeit in Sekunden"
    ),
        xaxis=dict(tickangle=45)
    )
    fig = Figure(data=data, layout=layout)
    from plotly.offline import init_notebook_mode, plot
    init_notebook_mode()
    plot(fig, image='svg', filename='startvergleich160seq.html')
Example #28
0
def handlePlotly(data, i):
    #py.sign_in("neural", "u47280okou")
    dataToPlot = createDataToPlot(data, i)
    fileName = '-'.join(['simulation', 
                        str(model.votesIncludeSelf), 
                        str(model.probabilityOfStatusQuoShouldCalculateWithOne),
                        str(model.offerMakerUseTheFirstMatrix),
                        str(model.offerMakerAcceptOffersByMinDistance),
                        str(model.stabilizedDistance)])
    plot(dataToPlot, filename=fileName)
Example #29
0
def main():
    x_t = np.linspace(-3.3, 3.3, num=1000)

    y_1 = np.linspace(0, 0, num=1000)
    y_2 = np.linspace(0, 0, num=1000)
    y_3 = np.linspace(0, 0, num=1000)
    y_4 = 1 / np.linspace(0.25, 3, num=500)
    x_1 = np.linspace(0, 0, num=1000)

    for i in range(len(x_t)):

        if (x_t[i] <= 3):
            if (x_t[i] >= -3):
                y_1[i] = math.sqrt(9 - x_t[i] * x_t[i])
                y_3[i] = -1 * math.sqrt(9 - x_t[i] * x_t[i])

        y_2[i] = math.fabs(x_t[i])

    z_t = np.linspace(-3.1, 3.1, num=1000)
    for i in range(len(z_t)):

        x_1[i] = (-1) * 3 * math.fabs(math.sin(z_t[i]))

    # print(x_1)
    fig = dict(
        data=[
            dict(
                x=x_t - 3,
                y=y_1,
                name='x^2+y^2=9',
            ),
            dict(
                x=x_t + 2,
                y=y_2,
                name='y=2|x|',
            ),
            dict(
                x=x_t - 3,
                y=y_3,
                name='x^2+y^2=9',
                showlegend=False,
            ),
            dict(
                x=np.linspace(0.25, 3, num=500) - 8,
                y=y_4,
                name='y=1/x',
            ),
            dict(
                x=x_1 + 6,
                y=z_t,
                name='x=-3|sin(y)|',
            ),
        ],
    )
    py.plot(fig, filename='love.html')
Example #30
0
def plot_train_history(epochs, acc, loss, output_file):
  trace0 = go.Scatter(x=epochs, y=loss, name='Loss')
  trace1 = go.Scatter(x=epochs, y=acc, name='Accuracy')

  layout = go.Layout(showlegend=True, title='Training history')
  fig = go.Figure(data=[trace0, trace1], layout=layout)

  py.plot(fig,
          filename=output_file,
          auto_open=False,
          link_text=False)
                     currentvalue={"prefix": "Date: "},
                     pad={"t": 1},
                     steps=steps)
            ]

            fig.data[0].visible = True
            fig.data[1].visible = True

        fig.update_geos(showcountries=True,
                        countrycolor="RebeccaPurple",
                        projection_type='natural earth')
        fig.update_layout(sliders=sliders,
                          title=(f"Rise of the Novel Coronavirus<br>"
                                 f"A Python Data Visualization "
                                 f"by Advait Joshi"),
                          title_x=0.5,
                          legend_title='Key',
                          height=600)
        return fig


if __name__ == "__main__":
    import plotly.offline as pl

    CORONA_GRAPH = CoronaVirusGrapher(url=URL).create_graph()
    CORONA_GRAPH.show()
    pl.plot(CORONA_GRAPH,
            filename='./map_cov.html',
            validate=True,
            auto_open=False)
Example #32
0
ean = ExergyAnalysis(network=nw, E_F=[power, heat_geo], E_P=[heat_cons])
ean.analyse(pamb, Tamb)
print("\n##### EXERGY ANALYSIS #####\n")
ean.print_results()

# create sankey diagram
links, nodes = ean.generate_plotly_sankey_input()
fig = go.Figure(
    go.Sankey(arrangement="snap",
              node={
                  "label": nodes,
                  'pad': 11,
                  'color': 'orange'
              },
              link=links))
plot(fig, filename='NH3_sankey.html')

# %% plot exergy destruction

# create data for bar chart
comps = ['E_F']
E_F = ean.network_data.E_F
# top bar
E_D = [0]  # no exergy destruction in the top bar
E_P = [E_F]  # add E_F as the top bar
for comp in ean.component_data.index:
    # only plot components with exergy destruction > 1 W
    if ean.component_data.E_D[comp] > 1:
        comps.append(comp)
        E_D.append(ean.component_data.E_D[comp])
        E_F = E_F - ean.component_data.E_D[comp]
def main():
    parser = argparse.ArgumentParser(
        description=
        'Generates a graphic showing how well reference transcripts are covered by a transcript assembly'
    )

    ## output file to be written
    parser.add_argument('-i',
                        '--input_files',
                        type=str,
                        required=True,
                        help='Comma-separated list of cov files to be plotted')
    parser.add_argument('-l',
                        '--labels',
                        type=str,
                        required=True,
                        help='Labels for each cov file passed')
    parser.add_argument('-t',
                        '--title',
                        type=str,
                        required=False,
                        default='Transcript coverage',
                        help='Title for the plot')
    parser.add_argument('-s', '--stacked', dest='stacked', action='store_true')
    parser.set_defaults(stacked=False)
    parser.add_argument('-rf',
                        '--ref_fasta',
                        required=False,
                        help='Only needed if passing --stacked')
    parser.add_argument('-qf',
                        '--qry_fasta',
                        required=False,
                        help='Only needed if passing --stacked')
    parser.add_argument(
        '-mb',
        '--margin_bottom',
        type=int,
        required=False,
        default=120,
        help='Size of the bottom margin, in case X labels are being cut off')
    parser.add_argument(
        '-o',
        '--output_image',
        type=str,
        required=False,
        help=
        'Name for PNG file to be created. If not passed, will post to plotly site'
    )
    args = parser.parse_args()

    cov_files = args.input_files.split(",")
    labels = args.labels.split(",")
    colors = [
        'rgb(49,130,189)',  #blue
        'rgb(204,204,204)',  #light grey
        'rgb(50, 171, 96)',  #green
        'rgb(222,45,38)',  #red
        'rgb(142, 124, 195)',  #purple
        'rgb(100,100,100)',  #darker grey
        'rgb(255,255,61)',  #yellow
        'rgb(255,169,58)'  #orange
    ]

    #print("Got {0} coverage files".format(len(cov_files)))
    #print("Got {0} labels".format(len(labels)))

    if len(labels) > len(colors):
        raise Exception(
            "Sorry, this many datasets is not yet supported (only because not enough colors were defined in code.)"
        )

    # This stores the positions of the labels
    label_position = dict()

    if len(cov_files) > 1 and args.stacked == True:
        raise Exception(
            "Use of the --stacked option requires a single input file")

    # Only used if doing a single-file stacked bar chart
    if args.stacked == True:
        ref_sizes = biocode.utils.fasta_sizes_from_file(args.ref_fasta)
        qry_sizes = biocode.utils.fasta_sizes_from_file(args.qry_fasta)

    traces = []
    file_idx = 0
    for file in cov_files:
        xvals = []
        yvals = []
        stacked_yvals = []

        for line in open(file):
            cols = line.rstrip().split("\t")
            ref_id = cols[0]
            xvals.append(ref_id)
            yvals.append(float(cols[1]))

            if args.stacked == True:
                qry_id = cols[2]
                if qry_sizes[qry_id] > ref_sizes[ref_id]:
                    # what percentage larger than the reference is the query?
                    rel_perc = (qry_sizes[qry_id] / ref_sizes[ref_id]) * 100

                    # for the stacked bars we have to subtract the current yval cov, since this adds to it
                    rel_perc_adj = rel_perc - float(cols[1])
                    stacked_yvals.append(rel_perc_adj)
                else:
                    stacked_yvals.append(0)

        trace = go.Bar(x=xvals,
                       y=yvals,
                       name=labels[file_idx],
                       marker=dict(color=colors[file_idx]))

        traces.append(trace)

        if args.stacked == True:
            trace2 = go.Bar(x=xvals,
                            y=stacked_yvals,
                            name=labels[file_idx],
                            marker=dict(color='rgb(200,200,200)'))

            traces.append(trace2)

        file_idx += 1

    if args.stacked == True:
        barmode = 'stack'
    else:
        barmode = 'group'

    layout = go.Layout(
        title=args.title,
        xaxis=dict(
            # set x-axis' labels direction at 45 degree angle
            tickangle=-65),
        yaxis=dict(title='Percent coverage',
                   titlefont=dict(size=16, color='rgb(107, 107, 107)'),
                   tickfont=dict(size=16, color='rgb(107, 107, 107)')),
        legend=dict(
            #x=0,
            #y=1.2,
            bgcolor='rgba(255, 255, 255, 0)',
            bordercolor='rgba(255, 255, 255, 0)',
            font=dict(size=20, color='#000')),
        barmode=barmode,
        bargap=0.15,
        bargroupgap=0.1,
        width=1500,
        height=800,
        margin=go.Margin(b=args.margin_bottom, pad=5))
    fig = go.FigureWidget(data=traces, layout=layout)

    if args.output_image is None:
        plot_url = py.plot(fig, filename='angled-text-bar')
        print("Navigate to {0} for your image".format(plot_url))
    else:
        #py.image.save_as(fig, filename=args.output_image)
        fig.write_image(args.output_image)
        print("Output written to file: {0}".format(args.output_image))
    labels.append(label)

# Make visualization
data = [{
    'type': 'bar',
    'x': repo_names,
    'y': stars,
    'hovertext': labels,
    'marker': {
        'color': 'rgb(60,100,150)',
        'line': {'width': 1.5, 'color': 'rgb(25,25,25)'}
    },
    'opacity': 0.6,
}]
my_layout = {
    'title': 'Most-Starred Python Projects on GitHub',
    'titlefont': {'size': 28},
    'xaxis': {
        'title': 'Repository',
        'titlefont': {'size': 24},
        'tickfont': {'size': 14},
    },
    'yaxis': {
        'title': 'Stars',
        'titlefont': {'size': 24},
        'tickfont': {'size': 14},
    },
}
fig = {'data': data, 'layout': my_layout}
offline.plot(fig, filename='python_repos.html')
            #                 # 'color': df['color'].values,
            #                 # 'colorscale': colorscale,
            #                 'width': 1
            #             },
            #             'opacity': 0.9
            #         },
            #         'mode': frame_mode,
            #         'name': name_2d,
            #         'type': 'scatter',
            #         'x': df['x'].values[k:k+slice_size],
            #         'xaxis': 'x1',
            #         'y': df['phenomenon'].values[k:k+slice_size],
            #         'yaxis': 'y1'
            #     },
            #     ]
            # }
            # for k in range(0, periods, slice_size)
        ]
    )

    plot(figure, filename='../output/plotly_line_slider.html', auto_open=False)

    logger.info('done')

    finish_time = time()
    elapsed_hours, elapsed_remainder = divmod(finish_time - start_time, 3600)
    elapsed_minutes, elapsed_seconds = divmod(elapsed_remainder, 60)
    logger.info('Time: {:0>2}:{:0>2}:{:05.2f}'.format(int(elapsed_hours), int(elapsed_minutes), elapsed_seconds))
    console_handler.close()
    logger.removeHandler(console_handler)
layout = go.Layout(boxmode='group')
# fig = go.Figure(data=data, layout=layout)
# pyo.plot(fig)

# Present vs Absent by Gender - BAR

trace5 = go.Bar(x=df_present_patients['Gender'].value_counts().index,
                y=df_present_patients['Gender'].value_counts().values,
                name='Present')

trace6 = go.Bar(x=df_absent_patients['Gender'].value_counts().index,
                y=df_absent_patients['Gender'].value_counts().values,
                name='Absent')

data = [trace5, trace6]
layout = go.Layout(barmode='group')

# fig = go.Figure(data=data, layout=layout)
# pyo.plot(fig, filename='grouped-bar')

# Days of the week people do not show up - BAR

trace7 = go.Bar(x=df_absent_patients['WeekDay'].value_counts().index,
                y=df_absent_patients['WeekDay'].value_counts().values,
                name='Absent    ')

data = [trace7]
layout = go.Layout(barmode='group')
fig = go.Figure(data=data, layout=layout)
pyo.plot(fig, filename='grouped-bar')
# Sort and select top 20
new_df = df.sort_values(by=['Total'], ascending=[False]).head(20).reset_index()

# Prepare data
trace_1 = go.Bar(x=new_df['NOC'],
                 y=new_df['Bronze'],
                 name='Bronze',
                 marker={'color': '#e07b39'})
trace_2 = go.Bar(x=new_df['NOC'],
                 y=new_df['Silver'],
                 name='Silver',
                 marker={'color': '#AFAFAF'})
trace_3 = go.Bar(x=new_df['NOC'],
                 y=new_df['Gold'],
                 name='Gold',
                 marker={'color': '#F9FF42'})
data = [trace_1, trace_2, trace_3]

# Prepare Layout
layout = go.Layout(
    title=
    "Number of medals awarded to the top 20 countries in the 2016 Olympics",
    xaxis_title='Country',
    yaxis_title='Number of Medals',
    barmode='stack')

# Plot the graph
fig = go.Figure(data=data, layout=layout)
pyo.plot(fig, filename='stackbarchart.html')
Example #38
0
    def plotly_pca(self, plotFile, Wt, pvar, PCs, eigenvalues, cols,
                   plotTitle):
        """
        A plotly version of plot_pca, that's called by it to do the actual plotting
        """
        fig = go.Figure()
        fig['layout']['xaxis1'] = {
            'domain': [0.0, 0.48],
            'anchor':
            'x1',
            'title':
            'PC{} ({:4.1f}% of var. explained)'.format(
                PCs[0], 100.0 * pvar[PCs[0] - 1])
        }
        fig['layout']['yaxis1'] = {
            'domain': [0.0, 1.0],
            'anchor':
            'x1',
            'title':
            'PC{} ({:4.1f}% of var. explained)'.format(
                PCs[1], 100.0 * pvar[PCs[1] - 1])
        }
        fig['layout']['xaxis2'] = {
            'domain': [0.52, 1.0],
            'title': 'Principal Component'
        }
        fig['layout']['yaxis2'] = {
            'domain': [0.0, 1.0],
            'anchor': 'x2',
            'title': 'Eigenvalue',
            'rangemode': 'tozero',
            'showgrid': False
        }
        fig['layout']['yaxis3'] = {
            'domain': [0.0, 1.0],
            'anchor': 'x2',
            'title': 'Cumulative variability',
            'rangemode': 'tozero',
            'side': 'right',
            'overlaying': 'y2'
        }
        fig['layout'].update(title=plotTitle)

        # PCA
        if cols is not None:
            colors = itertools.cycle(cols)
        n = len(self.labels)
        data = []
        for i in range(n):
            trace = go.Scatter(x=[Wt[PCs[0] - 1, i]],
                               y=[Wt[PCs[1] - 1, i]],
                               mode='marker',
                               xaxis='x1',
                               yaxis='y1',
                               name=self.labels[i])
            trace['marker'].update(size=20)
            if cols is not None:
                trace['marker'].update(color=next(colors))
            data.append(trace)

        # Scree plot
        trace = go.Bar(showlegend=False,
                       name='Eigenvalues',
                       x=range(1, n + 1),
                       y=eigenvalues[:n],
                       xaxis='x2',
                       yaxis='y2')
        data.append(trace)

        # Cumulative variability
        trace = go.Scatter(showlegend=False,
                           x=range(1, n + 1),
                           y=pvar.cumsum()[:n],
                           mode='lines+markers',
                           name='Cumulative variability',
                           xaxis='x2',
                           yaxis='y3',
                           line={'color': 'red'},
                           marker={
                               'symbol': 'circle-open-dot',
                               'color': 'black'
                           })
        data.append(trace)

        annos = []
        annos.append({
            'yanchor': 'bottom',
            'xref': 'paper',
            'xanchor': 'center',
            'yref': 'paper',
            'text': 'PCA',
            'y': 1.0,
            'x': 0.25,
            'font': {
                'size': 16
            },
            'showarrow': False
        })
        annos.append({
            'yanchor': 'bottom',
            'xref': 'paper',
            'xanchor': 'center',
            'yref': 'paper',
            'text': 'Scree plot',
            'y': 1.0,
            'x': 0.75,
            'font': {
                'size': 16
            },
            'showarrow': False
        })

        fig['data'] = data
        fig['layout']['annotations'] = annos
        offline.plot(fig, filename=plotFile, auto_open=False)
colors = ['#FEBFB3', '#E1396C']

trace = Pie(labels=labels, values=values,
               hoverinfo='label+percent', textinfo='value', 
               textfont=dict(size=20),
               marker=dict(colors=colors, 
                           line=dict(color='#000000', width=2)))
data=[trace]
layout = Layout(
    height=600,
    width=800,
)

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

plot(fig) 


zero_list = []
one_list = []
for col in binary_columns:
    zero_list.append((train_master[col]==0).sum())
    one_list.append((train_master[col]==1).sum())
    



trace0 = Bar(
    x=binary_columns,
    y=zero_list
)
Example #40
0
# that plots seven days worth of temperature data on one graph.
# You can use a for loop to assign each day to its own trace.
######

# Perform imports here:
import pandas as pd
import plotly.offline as pyo
import plotly.graph_objects as go

# Create a pandas DataFrame from 2010YumaAZ.csv
df = pd.read_csv('../data/2010YumaAZ.csv')
days = df['DAY'].unique()

# Use a for loop (or list comprehension to create traces for the data list)
data = []

for day in days:
    # What should go inside this Scatter call?
    trace = go.Scatter(x=df['LST_TIME'],
                       y=df[df['DAY'] == day]['T_HR_AVG'],
                       mode='lines',
                       name=day)
    data.append(trace)

# Define the layout
layout = go.Layout(title='Daily Temp Averages')

# Create a fig from data and layout, and plot the fig
fig = go.Figure(data=data, layout=layout)
pyo.plot(fig)
Example #41
0
    def plotly_scatter(self,
                       plot_filename,
                       corr_matrix,
                       plot_title='',
                       minXVal=None,
                       maxXVal=None,
                       minYVal=None,
                       maxYVal=None):
        """Make the scatter plot of a matrix with plotly"""
        n = self.matrix.shape[1]
        self.matrix = self.matrix
        fig = go.Figure()
        domainWidth = 1. / n

        annos = []
        for i in range(n):
            x = domainWidth * (i + 1)
            y = 1 - (domainWidth * i + 0.5 * domainWidth)
            anno = dict(text=self.labels[i],
                        showarrow=False,
                        xref='paper',
                        yref='paper',
                        x=x,
                        y=y,
                        xanchor='right',
                        yanchor='middle')
            annos.append(anno)

        data = []
        zMin = np.inf
        zMax = -np.inf
        for x in range(n):
            xanchor = 'x{}'.format(x + 1)
            base = x * domainWidth
            domain = [base, base + domainWidth]
            if x > 0:
                base = 1 - base
                fig['layout']['xaxis{}'.format(x + 1)] = dict(
                    domain=domain,
                    range=[minXVal, maxXVal],
                    anchor='free',
                    position=base)
            for y in range(0, n):
                yanchor = 'y{}'.format(y + 1)
                if x == 1:
                    base = 1 - y * domainWidth
                    domain = [base - domainWidth, base]
                    fig['layout']['yaxis{}'.format(y + 1)] = dict(
                        domain=domain,
                        range=[minYVal, maxYVal],
                        side='right',
                        anchor='free',
                        position=1.0)

                if x > y:
                    vector1 = self.matrix[:, x]
                    vector2 = self.matrix[:, y]
                    Z, xEdges, yEdges = np.histogram2d(vector1,
                                                       vector2,
                                                       bins=50)
                    Z = np.log10(Z)
                    if np.min(Z) < zMin:
                        zMin = np.min(Z)
                    if np.max(Z) > zMax:
                        zMax = np.max(Z)
                    name = '{}={:.2f}'.format(self.corr_method, corr_matrix[x,
                                                                            y])
                    trace = go.Heatmap(z=Z,
                                       x=xEdges,
                                       y=yEdges,
                                       showlegend=False,
                                       xaxis=xanchor,
                                       yaxis=yanchor,
                                       name=name,
                                       showscale=False)
                    data.append(trace)

        # Fix the colorbar bounds
        for trace in data:
            trace.update(zmin=zMin, zmax=zMax)
        data[-1]['colorbar'].update(title="log10(instances per bin)",
                                    titleside="right")
        data[-1].update(showscale=True)

        fig['data'] = data
        fig['layout'].update(title=plot_title,
                             showlegend=False,
                             annotations=annos)

        offline.plot(fig, filename=plot_filename, auto_open=False)
    all_eq_data = json.load(f)

readable_file = 'data/readable_eqdata_lastday.json'
with open(readable_file, 'w') as f:
    json.dump(all_eq_data, f, indent=4)

all_eq_dicts = all_eq_data['features']

mags, lons, lats = [], [], []
for eq_dict in all_eq_dicts:
    mag = eq_dict['properties']['mag']
    lon = eq_dict['geometry']['coordinates'][0]
    lat = eq_dict['geometry']['coordinates'][1]
    mags.append(mag)
    lons.append(lon)
    lats.append(lat)

# Map the earthquake
data = [{
    'type': 'scattergeo',
    'lon': lons,
    'lat': lats,
    'marker': {
        'size': [5*mag for mag in mags],
    },
}]
my_layout = Layout(title='Global Earthquakes')

fig = {'data': data, 'layout': my_layout}
offline.plot(fig, filename='global_earthquakes.html')
Example #43
0
def test_run():
    '''
    Test run of script
    '''

    d2c = {}  # Displayname to Cluster
    c2d = {}  # Cluster to Displayname
    c2t = {}  # Cluster to Template
    t2c = {}  # Template to Cluster
    resource_positions = {}  # Positions of all the resources
    html_results_string = ""  # Results to print to flask webpage.

    # Add a title to our tab.
    html_results_string += "<head><title>Albion Resource Count</title></head><br>"

    game_data_xml_filenames, cluster_data_xml_filenames = grab_xml_filenames()
    displayname_to_cluster(d2c, c2d)
    cluster_to_template(cluster_data_xml_filenames, c2t, t2c)
    for entry in d2c:
        results, html_results_string = parse_template_resources(
            c2t[d2c[entry]], entry, resource_positions, html_results_string,
            True)

    resources = {}  # Dict of all resources position data

    for displayname in d2c:
        if 'ORE_HIGH_NODE' in results[displayname]:
            resources['ORE_HIGH_NODE'] = []
            x_position = []
            y_position = []
            z_position = []
            for resource in results[displayname]['ORE_HIGH_NODE']:
                resource_list = resource.split()
                x_position.append(int(resource_list[0]))
                z_position.append(int(resource_list[1]))
                y_position.append(int(resource_list[2]))
            trace1 = go.Scatter3d(x=np.asarray(x_position),
                                  y=np.asarray(y_position),
                                  z=np.asarray(z_position),
                                  mode='markers',
                                  marker=dict(size=12,
                                              line=dict(
                                                  color='rgb(228, 26, 28)',
                                                  width=0.5),
                                              opacity=0.8))

        if 'ORE_MEDIUM_NODE' in results[displayname]:
            resources['ORE_MEDIUM_NODE'] = []
            x_position = []
            y_position = []
            z_position = []
            for resource in results[displayname]['ORE_MEDIUM_NODE']:
                resource_list = resource.split()
                x_position.append(int(resource_list[0]))
                z_position.append(int(resource_list[1]))
                y_position.append(int(resource_list[2]))
            trace2 = go.Scatter3d(x=np.asarray(x_position),
                                  y=np.asarray(y_position),
                                  z=np.asarray(z_position),
                                  mode='markers',
                                  marker=dict(size=12,
                                              line=dict(
                                                  color='rgb(55, 126, 184)',
                                                  width=0.5),
                                              opacity=0.8))

        if 'ORE_LOW_NODE' in results[displayname]:
            resources['ORE_LOW_NODE'] = []
            x_position = []
            y_position = []
            z_position = []
            for resource in results[displayname]['ORE_LOW_NODE']:
                resource_list = resource.split()
                x_position.append(int(resource_list[0]))
                z_position.append(int(resource_list[1]))
                y_position.append(int(resource_list[2]))
            trace3 = go.Scatter3d(x=np.asarray(x_position),
                                  y=np.asarray(y_position),
                                  z=np.asarray(z_position),
                                  mode='markers',
                                  marker=dict(size=12,
                                              line=dict(
                                                  color='rgb(77, 175, 74)',
                                                  width=0.5),
                                              opacity=0.8))

            data = [trace1, trace2, trace3]
            layout = go.Layout(margin=dict(l=0, r=0, b=0, t=0))
            fig = go.Figure(data=data, layout=layout)
            offline.plot(fig, filename=displayname)

    return html_results_string
Example #44
0
def index(request):
    if request.method == "POST":
        prediction = PredictionForm(request.POST)
        if prediction.is_valid():
            speed = prediction.cleaned_data['speed']
            therotic = prediction.cleaned_data['therotic']
            direction = prediction.cleaned_data['direction']
            dataset = pd.read_csv('T1.csv')
            features_intake = [
                'LV ActivePower (kW)', 'Wind Speed (m/s)',
                'Theoretical_Power_Curve (KWh)', 'Wind Direction (°)'
            ]
            features = dataset[features_intake]
            features.index = dataset['Date/Time']
            dataset = features.values
            data_in = dataset[:, 1:4]
            data_out = dataset[:, 0]
            try:
                speed = float(speed)
                therotic = float(therotic)
                direction = float(direction)
            except:
                return render(request, 'weather/index.html', {
                    'form': prediction,
                    'msg1': 'Enter Numeric Values'
                })
            user = np.array([speed, therotic, direction])
            user = np.reshape(user, (1, -1))
            data_total = np.concatenate((data_in, user))
            scale = MinMaxScaler(feature_range=(0, 1))
            data_out = np.reshape(data_out, (-1, 1))
            data_in = scale.fit_transform(data_total)
            data_out = scale.fit_transform(data_out)
            lis = []
            for i in range(1, 121):
                lis.append(data_in[-i, :])
            data = np.array(lis)
            model = load_model('saved_model_result/final_model.h5')
            x_train = np.reshape(data, (1, 120, 3))
            x = model.predict(x_train)
            pred = scale.inverse_transform(x)[0][:]
            trace = Scatter(x=np.arange(200),
                            y=pred,
                            fill='tonexty',
                            fillcolor='orange',
                            marker={'color': 'green'})
            data = Data([trace])
            layout = Layout(
                title='The  Predicted Wind Power in 72 Hours Future is',
                xaxis={'title': 'Hours'},
                yaxis={'title': 'Power'})
            fig = Figure(data=data, layout=layout)
            fig.update_layout(
                legend_title=dict(text='Hours', font=dict(color='blue')))
            div = plot(fig, auto_open=False, output_type='div')
            return render(request, 'weather/index.html', {
                'speed': div,
                'form': prediction
            })

    else:
        prediction = PredictionForm()
    return render(request, 'weather/index.html', {'form': prediction})
Example #45
0
import numpy as np
import plotly.offline as pyo
import plotly.graph_objs as go
np.random.seed(56)

# Now we will create evenly distributed 100 points for our x-axis values
x_values = np.linspace(0, 1, 100)

# for y we'll create random values
y_values = np.random.randn(100)

trace0 = go.Scatter(x=x_values, y=y_values + 5, mode='markers', name='markers')
trace1 = go.Scatter(x=x_values, y=y_values, mode='lines', name='mylines')
trace2 = go.Scatter(x=x_values,
                    y=y_values - 5,
                    mode='lines+markers',
                    name='both')
# now we'll append all these traces to the data list
data = [trace0, trace1, trace2]

# Now we'll create a layout
layout = go.Layout(title='Line Charts',
                   xaxis=dict(title='My X-Axis'),
                   yaxis=dict(title='My Y-Axis'))
# Now we will create the figure object
fig = go.Figure(data=data, layout=layout)

# now we'll plot the graph
pyo.plot(fig, filename='Line.html')
Example #46
0
def LineSurvey(
    spec,
    overlay=None,
    wunit="cm-1",
    Iunit="hitran",
    medium="air",
    cutoff=None,
    plot="S",
    lineinfo=["int", "A", "El"],
    barwidth=0.07,
    yscale="log",
    writefile=None,
    xunit=None,
    yunit=None,  # deprecated
):
    """Plot Line Survey (all linestrengths above cutoff criteria) in Plotly (html)


    Parameters
    ----------

    spec: Spectrum
        result from SpectrumFactory calculation (see spectrum.py)

    overlay: tuple (w, I, [name], [units]), or list or tuples
        plot (w, I) on a secondary axis. Useful to compare linestrength with
        calculated / measured data

    wunit: ``'nm'``, ``'cm-1'``
        wavelength / wavenumber units

    Iunit: ``'hitran'``, ``'splot'``
        Linestrength output units:

        - ``'hitran'``: (cm-1/(molecule/cm-2))
        - ``'splot'`` : (cm-1/atm)   (Spectraplot units [2]_)

        Note: if not None, cutoff criteria is applied in this unit.
        Not used if plot is not 'S'

    medium: ``'air'``, ``'vacuum'``
        show wavelength either in air or vacuum. Default ``'air'``

    plot: str
        what to plot. Default ``'S'`` (scaled line intensity). But it can be
        any key in the lines, such as population (``'nu'``), or Einstein coefficient (``'Aul'``)

    lineinfo: list, or ``'all'``
        extra line information to plot. Should be a column name in the databank
        (s.lines). For instance: ``'int'``, ``'selbrd'``, etc... Default [``'int'``]

    Other Parameters
    ----------------

    writefile: str
        if not ``None``, a valid filename to save the plot under .html format.
        If ``None``, use the ``fig`` object returned to show the plot.

    yscale: ``'log'``, ``'linear'``
        Default ``'log'``

    Returns
    -------

    fig: a Plotly figure.
        If using a Jupyter Notebook, the plot will appear. Else, use ``writefile``
        to export to an html file.

    See typical output in [1]_

    Examples
    --------

    An example using the :class:`~radis.lbl.factory.SpectrumFactory` to generate a spectrum::

        from radis import SpectrumFactory
        sf = SpectrumFactory(
                             wavenum_min=2380,
                             wavenum_max=2400,
                             mole_fraction=400e-6,
                             path_length=100,  # cm
                             isotope=[1],
                             db_use_cached=True)
        sf.load_databank('HITRAN-CO2-TEST')
        s = sf.eq_spectrum(Tgas=1500)
        s.apply_slit(0.5)
        s.line_survey(overlay='radiance_noslit', barwidth=0.01)

    See the output in :ref:`Examples <label_examples>`

    References
    ----------

    .. [1] `RADIS Online Documentation (LineSurvey) <https://radis.readthedocs.io/en/latest/tools/line_survey.html>`__

    .. [2] `SpectraPlot <http://www.spectraplot.com/survey>`__


    """

    # Check inputs
    if xunit is not None:
        warn(DeprecationWarning("xunit replaced with wunit"))
        wunit = xunit
    if yunit is not None:
        warn(DeprecationWarning("yunit replaced with Iunit"))
        Iunit = yunit
    assert yscale in ["log", "linear"]

    try:
        spec.lines
        assert spec.lines is not None
    except (AttributeError, AssertionError):
        raise AttributeError(
            "Spectrum has no `lines`. Cant run line survey. If your code allows "
            + "it, recompute the spectrum with 'export_lines=True'"
        )

    # Get input
    T = spec.conditions["Tgas"]
    P = spec.conditions["pressure_mbar"] / 1000  # bar1013.25 # atm
    Xi = spec.conditions["mole_fraction"]
    sp = spec.lines.copy()
    dbformat = spec.conditions["dbformat"]

    if not plot in list(sp.keys()):
        raise KeyError(
            "Key {0} is not in line database: {1}".format(plot, list(sp.keys()))
        )

    def hitran2splot(S):
        """convert Linestrength in HITRAN units (cm-1/(molecules.cm-2)) to
        SpectraPlot units (cm-2/atm)"""

        return S / (k_b * T) / 10

    # Parsers to get units and more details
    if dbformat == "hitran":
        columndescriptor = hitrancolumns
    elif dbformat == "cdsd-hitemp":
        columndescriptor = cdsdcolumns
    elif dbformat == "cdsd-4000":
        columndescriptor = cdsd4000columns

    # Apply cutoff, get ylabel
    if plot == "S":
        if Iunit == "hitran":
            if cutoff is None:
                cutoff = spec.conditions["cutoff"]
            sp = sp[(sp.S > cutoff)]
            Iunit_str = "cm-1/(molecule/cm-2)"
        elif Iunit == "splot":
            if cutoff is None:
                cutoff = spec.conditions["cutoff"]
                # if None, use default cutoff expressed in Hitran units
                sp = sp[(sp.S > cutoff)]
            else:
                sp["S"] = hitran2splot(sp.S)
                sp = sp[(sp.S > cutoff)]
            Iunit_str = "cm-1/atm"
        else:
            raise ValueError("Unknown Iunit: {0}".format(Iunit))
        ylabel = "Linestrength ({0})".format(Iunit_str)
    else:
        cutoff = 0
        try:  # to find units and real name (if exists in initial databank)
            _, _, name, unit = columndescriptor[plot]
            ylabel = "{0} - {1} [{2}]".format(name, plot, unit)
        except KeyError:
            ylabel = plot

    print(len(sp), "lines")
    if len(sp) == 0:
        raise ValueError("0 lines. Change your cutoff criteria?")

    # %% Plot - Plotly version

    def get_x(w):
        """w (input) is supposed to be in vacuum wavenumbers in the
        lines database"""

        # Convert wavelength / wavenumber
        if wunit == "cm-1":
            x = w
        elif wunit == "nm":
            x = cm2nm(w)

            # Correct if requested medium is air
            if medium == "air":
                x = vacuum2air(x)
            elif medium == "vacuum":
                pass
            else:
                raise ValueError("Unknown medium: {0}".format(medium))
        else:
            raise ValueError("Unknown wunit: {0}".format(wunit))
        return x

    # Parse databank to get relevant information on each line
    # (one function per databank format)

    def get_label_hitran(row, details):
        """
        Todo
        -------

        replace with simple astype(str) statements and str operations

        ex:
        > '['+df[locl].astype(str)+']('+df[globl].astype(str)+'->'+
        >     df[globu].astype(str)'+)'

        will be much faster!
        """

        molecule = get_molecule(row.id)

        # Get global labels
        if molecule in HITRAN_CLASS1:
            label = (
                "{molec}[iso{iso:.0f}] [{branch}{jl:.0f}]({vl:.0f})->({vu:.0f})".format(
                    **dict(
                        [(k, row[k]) for k in ["vu", "vl", "jl", "iso"]]
                        + [
                            ("molec", molecule),
                            ("branch", _fix_branch_format[row["branch"]]),
                        ]
                    )
                )
            )
        elif molecule in HITRAN_CLASS4:
            label = "{molec}[iso{iso:.0f}] [{branch}{jl:.0f}]({v1l:.0f}{v2l:.0f}`{l2l:.0f}`{v3l:.0f})->({v1u:.0f}{v2u:.0f}`{l2u:.0f}`{v3u:.0f})".format(
                **dict(
                    [
                        (k, row[k])
                        for k in [
                            "v1u",
                            "v2u",
                            "l2u",
                            "v3u",
                            "v1l",
                            "v2l",
                            "l2l",
                            "v3l",
                            "jl",
                            "iso",
                        ]
                    ]
                    + [
                        ("molec", molecule),
                        ("branch", _fix_branch_format[row["branch"]]),
                    ]
                )
            )
        elif molecule in HITRAN_CLASS5:
            label = "{molec}[iso{iso:.0f}] [{branch}{jl:.0f}]({v1l:.0f}{v2l:.0f}`{l2l:.0f}`{v3l:.0f} {rl:.0f})->({v1u:.0f}{v2u:.0f}`{l2u:.0f}`{v3u:.0f} {ru:.0f})".format(
                **dict(
                    [
                        (k, row[k])
                        for k in [
                            "v1u",
                            "v2u",
                            "l2u",
                            "v3u",
                            "v1l",
                            "v2l",
                            "l2l",
                            "v3l",
                            "rl",
                            "ru",
                            "jl",
                            "iso",
                        ]
                    ]
                    + [
                        ("molec", molecule),
                        ("branch", _fix_branch_format[row["branch"]]),
                    ]
                )
            )
        else:
            raise NotImplementedError(
                "No label for {0}. Please add it!".format(molecule)
            )

        # Add details about some line properties
        for k in details:
            name, _, unit = details[k]
            if is_float(row[k]):
                label += "<br>{0} {1}: {2:.3g} {3}".format(k, name, row[k], unit)
            else:
                label += "<br>{0} {1}: {2} {3}".format(k, name, row[k], unit)

        return label

    def get_label_cdsd(row, details):
        label = "CO2[iso{iso}] [{branch}{jl:.0f}](p{polyl:.0f}c{wangl:.0f}n{rankl:.0f})->(p{polyu:.0f}c{wangu:.0f}n{ranku:.0f})".format(
            **dict(
                [
                    (k, row[k])
                    for k in [
                        "polyl",
                        "wangl",
                        "rankl",
                        "polyu",
                        "wangu",
                        "ranku",
                        "jl",
                        "iso",
                    ]
                ]
                + [("branch", _fix_branch_format[row["branch"]])]
            )
        )

        for k in details:
            name, _, unit = details[k]
            if is_float(row[k]):
                label += "<br>{0} {1}: {2:.3g} {3}".format(k, name, row[k], unit)
            else:
                label += "<br>{0} {1}: {2} {3}".format(name, k, row[k], unit)

        return label

    def get_label_cdsd_hitran(row, details):
        label = "CO2[iso{iso}] [{branch}{jl:.0f}]({v1l:.0f}{v2l:.0f}`{l2l:.0f}`{v3l:.0f})->({v1u:.0f}{v2u:.0f}`{l2u:.0f}`{v3u:.0f})".format(
            **dict(
                [
                    (k, row[k])
                    for k in [
                        "v1u",
                        "v2u",
                        "l2u",
                        "v3u",
                        "v1l",
                        "v2l",
                        "l2l",
                        "v3l",
                        "jl",
                        "iso",
                    ]
                ]
                + [("branch", _fix_branch_format[row["branch"]])]
            )
        )

        for k in details:
            name, _, unit = details[k]
            if is_float(row[k]):
                label += "<br>{0} {1}: {2:.3g} {3}".format(k, name, row[k], unit)
            else:
                label += "<br>{0} {1}: {2} {3}".format(name, k, row[k], unit)

        return label

    def get_label_none(row):
        return "unknown databank format. \ndetails cant be read"

    # add extra info to label
    details = {}
    if columndescriptor:
        for k in lineinfo:
            if not k in sp.columns:
                raise KeyError(
                    "{0} not a {1} databank entry ({2} format)".format(
                        k, columndescriptor, dbformat.upper()
                    )
                )
            try:  # to find units and real name (if exists in initial databank)
                _, ktype, name, unit = columndescriptor[k]
                details[k] = (name, ktype, " [{0}]".format(unit))
            except:
                details[k] = ("", None, "")  # keep short name

    # Get label
    if dbformat == "hitran":
        sp["label"] = sp.apply(lambda r: get_label_hitran(r, details), axis=1)
    elif dbformat in ["cdsd-hitemp", "cdsd-4000"]:
        try:
            sp["label"] = sp.apply(lambda r: get_label_cdsd_hitran(r, details), axis=1)
        except KeyError:
            sp["label"] = sp.apply(lambda r: get_label_cdsd(r, details), axis=1)
    else:
        sp["label"] = sp.apply(get_label_none, axis=1)

    # from plotly.graph_objs import Scatter, Figure, Layout
    #
    l = [
        go.Bar(
            x=get_x(sp.shiftwav),
            y=sp[plot],
            text=sp.label,
            width=barwidth,
            name="linestrength",
        )
    ]

    if wunit == "nm":
        xlabel = "Wavelength (nm) [{0}]".format(medium)
    elif wunit == "cm-1":
        xlabel = "Wavenumber (cm-1)"
    else:
        raise ValueError("unknown wunit: {0}".format(wunit))

    if yscale == "log":
        plot_range = (
            int(np.log10(max(cutoff, sp[plot].min()))),
            int(np.log10(sp[plot].max())) + 1,
        )
    else:
        plot_range = (max(cutoff, sp[plot].min()), sp[plot].max() + 1)

    layout = go.Layout(
        title="Line Survey ({T}K, {P:.3f}bar, Mfrac={Xi:.3f})".format(
            **{"T": T, "P": P, "Xi": Xi}
        ),
        hovermode="closest",
        xaxis=dict(
            title=xlabel,
        ),
        yaxis=dict(
            title=ylabel,
            # note: LaTeX doesnt seem to work in Offline mode yet.
            type=yscale,
            range=plot_range,
            titlefont=dict(color="#1f77b4"),
            tickfont=dict(color="#1f77b4"),
        ),
        showlegend=False,
    )

    # %% Add overlay
    def add_overlay(overlay):

        over_w = overlay[0]
        over_I = overlay[1]
        args = overlay[2:]
        over_name = args[0] if len(args) > 0 else "overlay"
        over_units = args[1] if len(args) > 1 else "a.u"

        l.append(
            go.Scatter(
                x=over_w,
                #                        y=spec.out[overlay],
                y=over_I,
                yaxis="y2",
                name=over_name,
            )
        )

        layout["yaxis2"] = dict(
            title="{0} ({1})".format(
                over_name.capitalize(), over_units
            ),  # note: LaTeX doesnt seem to
            # work in Offline mode yet.
            overlaying="y",
            type="log",
            side="right",
            titlefont=dict(color="#ff7f0e"),
            tickfont=dict(color="#ff7f0e"),
        )

    if overlay is not None:

        if type(overlay) is not list:
            overlay = [overlay]

        for ov in overlay:
            add_overlay(ov)

    # %% Plot

    fig = go.Figure(data=l, layout=layout)

    if writefile:
        py.plot(fig, filename=writefile, auto_open=True)

    return fig
Example #47
0
def generate_view(options):

    # find all the log_names to load
    log_names = []
    views_per_figure = []
    for i, view_raw in enumerate(options['view']):
        views = []
        for view_interim in view_raw.split('+'):
            log_name, view_name = view_interim.split(':')
            views.append({
                'view_interim': view_interim,  # logs:train_epoch.loss
                'log_name': log_name,  # logs
                'view_name': view_name,  # train_epoch.loss
                'split_name': view_name.split('.')[0]  # train_epoch
            })
            log_names.append(log_name)
        views_per_figure.append(views)

    log_names = list(set(log_names))  # unique

    data_dict = {}
    for log_name in log_names:
        path_json = os.path.join(options['exp']['dir'],
                                 '{}.json'.format(log_name))
        if os.path.isfile(path_json):
            with open(path_json, 'r') as handle:
                data_json = json.load(handle)
            data_dict[log_name] = data_json
        else:
            Logger()("Json log file '{}' not found in '{}'".format(
                log_name, path_json),
                     log_level=Logger.WARNING)

    nb_keys = len(options['view'])
    nb_rows = math.ceil(nb_keys / 2)
    nb_cols = min(2, nb_keys)

    figure = tools.make_subplots(rows=nb_rows,
                                 cols=nb_cols,
                                 subplot_titles=options['view'],
                                 print_grid=False)

    colors = {
        'train_epoch': 'rgb(214, 39, 40)',
        'train_batch': 'rgb(214, 39, 40)',
        #'trainval_epoch': 'rgb(214, 39, 40)', 'trainval_batch': 'rgb(214, 39, 40)',
        'val_epoch': 'rgb(31, 119, 180)',
        'val_batch': 'rgb(31, 119, 180)',
        'eval_epoch': 'rgb(31, 119, 180)',
        'eval_batch': 'rgb(31, 119, 180)',
        'test_epoch': 'rgb(31, 180, 80)',
        'test_batch': 'rgb(31, 180, 80)'
    }

    for figure_id, views in enumerate(views_per_figure):

        figure_pos_x = figure_id % 2 + 1
        figure_pos_y = int(figure_id / 2) + 1

        for view in views:
            if view['log_name'] not in data_dict:
                continue

            if view['view_name'] not in data_dict[view['log_name']]:
                Logger()("View '{}' not in '{}.json'".format(
                    view['view_name'], view['log_name']),
                         log_level=Logger.WARNING)
                continue

            if view['split_name'] not in colors:
                Logger()("Split '{}' not in colors '{}'".format(
                    view['split_name'], list(colors.keys())),
                         log_level=Logger.WARNING)
                color = colors['train_epoch']
            else:
                color = colors[view['split_name']]

            y = data_dict[view['log_name']][view['view_name']]

            if 'epoch' in view['split_name']:
                # example: data_dict['logs_last']['test_epoch.epoch']
                key = view[
                    'split_name'] + '.epoch'  # TODO: ugly fix, to be remove
                if key not in data_dict[view['log_name']]:
                    key = 'eval_epoch.epoch'
                x = data_dict[view['log_name']][key]
            else:
                x = list(range(len(y)))

            scatter = go.Scatter(x=x,
                                 y=y,
                                 name=view['view_interim'],
                                 line=dict(color=color))
            figure.append_trace(scatter, figure_pos_y, figure_pos_x)

    figure['layout'].update(autosize=False, width=1800, height=400 * nb_rows)
    path_view = os.path.join(options['exp']['dir'], 'view.html')
    plot(figure, filename=path_view, auto_open=False)
    Logger()('View generated in ' + path_view)
Example #48
0
Total, CountryName, lons, lats = [], [], [], []
for wholedata_dict in wholedata_dicts:
    cases = wholedata_dict['cases']
    lon = wholedata_dict['countryInfo']['long']
    lat = wholedata_dict['countryInfo']['lat']
    Total.append(cases)
    lons.append(lon)
    lats.append(lat)

data = [Scattergeo(lon=lons, lat=lats)]

data = [{
    'type': 'scattergeo',
    'lon': lons,
    'lat': lats,
    'marker': {
        'size': [0.0004 * cases for cases in Total],
        'color': Total,
        'colorscale': 'Viridis',
        'reversescale': True,
        'colorbar': {
            'title': 'Total Cases',
        },
    },
}]

my_layout = Layout(title='Global Coronavirus Cases')

fig = {'data': data, 'layout': my_layout}
offline.plot(fig, filename='Program1_plot.html')
        brightnesses.append(brightness)
        lats.append(row[0])
        lons.append(row[1])
        hover_texts.append(label)

        row_num += 1
        if row_num == num_rows:
            break

# Map the fires.
data = [{
    'type': 'scattergeo',
    'lon': lons,
    'lat': lats,
    'text': hover_texts,
    'marker': {
        'size': [brightness / 20 for brightness in brightnesses],
        'color': brightnesses,
        'colorscale': 'YlOrRd',
        'reversescale': True,
        'colorbar': {
            'title': 'Brightness'
        },
    },
}]

my_layout = Layout(title='Global Fire Activity')

fig = {'data': data, 'layout': my_layout}
offline.plot(fig, filename='global_fires.html')
Example #50
0
)]

data3 = [go.Heatmap(
    z=tempMinY,
    zmin=-7,
    zmax=27,
)]

data4 = [go.Heatmap(
    z=tempMinO,
    zmin=-7,
    zmax=27,
)]

#Creates divs from plots
plot1 = py.plot(data1, include_plotlyjs=False, output_type='div')
plot2 = py.plot(data2, include_plotlyjs=False, output_type='div')
plot3 = py.plot(data3, include_plotlyjs=False, output_type='div')
plot4 = py.plot(data4, include_plotlyjs=False, output_type='div')

#Beginning of webpage
html_str1 = """
<html>
<head><b>Daymet 2000 Minimum Temperature Heat Maps of Tucson, AZ<b/></head>
<p>
<p>
<body>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
"""

#End of webpage
Example #51
0
def make_plot(plot_list,
              use_median=False,
              use_five_numbers=False,
              plot_width=None,
              plot_height=None,
              title=None,
              xlim=None,
              ylim=None):
    data = []
    p25, p50, p75 = [], [], []
    p0, p100 = [], []
    for idx, plt in enumerate(plot_list):
        color = core.color_defaults[idx % len(core.color_defaults)]
        if use_median:
            p25.append(np.mean(plt.percentile25))
            p50.append(np.mean(plt.percentile50))
            p75.append(np.mean(plt.percentile75))
            x = list(range(len(plt.percentile50)))
            y = list(plt.percentile50)
            y_upper = list(plt.percentile75)
            y_lower = list(plt.percentile25)
            y_extras = []
        elif use_five_numbers:
            p0.append(np.mean(plt.percentile0))
            p25.append(np.mean(plt.percentile25))
            p50.append(np.mean(plt.percentile50))
            print('>>> mean: {}'.format(plt.mean))
            p75.append(np.mean(plt.percentile75))
            p100.append(np.mean(plt.percentile100))
            x = list(range(len(plt.percentile50)))
            y = list(plt.percentile50)
            y_upper = list(plt.percentile75)
            y_lower = list(plt.percentile25)
            y_extras = [
                list(ys) for ys in [plt.percentile0, plt.percentile100]
            ]

        else:
            x = list(range(len(plt.means)))
            y = list(plt.means)
            y_upper = list(plt.means + plt.stds)
            y_lower = list(plt.means - plt.stds)
            y_extras = []

        if hasattr(plt, "custom_x"):
            x = list(plt.custom_x)

        data.append(
            go.Scatter(x=x + x[::-1],
                       y=y_upper + y_lower[::-1],
                       fill='tozerox',
                       fillcolor=core.hex_to_rgb(color, 0.2),
                       line=go.Line(color='hsva(0,0,0,0)', width=4),
                       showlegend=False,
                       legendgroup=plt.legend,
                       hoverinfo='none'))
        data.append(
            go.Scatter(
                x=x,
                y=y,
                name=plt.legend,
                legendgroup=plt.legend,
                line=dict(color=core.hex_to_rgb(color), width=4),
            ))

        for y_extra in y_extras:
            data.append(
                go.Scatter(
                    x=x,
                    y=y_extra,
                    showlegend=False,
                    legendgroup=plt.legend,
                    line=dict(color=core.hex_to_rgb(color), dash='dot')
                    # choices: solid, dot, dash, longdash, dashdot, longdashdot
                ))

    def numeric_list_to_string(numbers):
        s = '['
        for num in numbers:
            s += (str(num) + ',')
        s += ']'
        return s

    print(numeric_list_to_string(p25))
    print(numeric_list_to_string(p50))
    print(numeric_list_to_string(p75))

    layout = go.Layout(
        legend=dict(x=1,
                    y=1,
                    font=dict(size=24)
                    # xanchor="left",
                    # yanchor="bottom",
                    ),
        width=1000,
        height=500,
        title=title,
        xaxis=go.XAxis(range=xlim, tickfont=dict(size=24)),
        yaxis=go.YAxis(range=ylim, tickfont=dict(size=24)),
    )
    fig = go.Figure(data=data, layout=layout)

    fig_div = po.plot(fig, output_type='div', include_plotlyjs=False)
    if "footnote" in plot_list[0]:
        footnote = "<br />".join([
            r"<span><b>%s</b></span>: <span>%s</span>" %
            (plt.legend, plt.footnote) for plt in plot_list
        ])
        return r"%s<div>%s</div>" % (fig_div, footnote)
    else:
        return fig_div
Example #52
0
def generate_report(summary_file,
                    barcode_file,
                    outfile,
                    qual=7,
                    filter_calibration=False,
                    config=None,
                    template_file=None,
                    verbose_level=1,
                    title=None):
    """ Runs pycoQC and generates the HTML report"""

    # Parse configuration file
    logger.warning("PARSE CONFIGURATION FILE")
    config_dict = parse_config_file(config)
    logger.debug(config_dict)

    # Initiate pycoQC
    logger.warning("PARSE DATA FILES")
    p = pycoQC(seq_summary_file=summary_file,
               barcode_summary_file=barcode_file,
               verbose_level=verbose_level,
               min_pass_qual=qual,
               filter_calibration=filter_calibration)

    # Loop over configuration file and run the pycoQC functions defined
    logger.warning("GENERATES PLOTS")
    plots = list()
    titles = list()
    for method_name, method_args in config_dict.items():

        # Check if method exists and is callable
        if not method_name in plot_methods:
            logger.info(
                "\tWarning: Method {} is defined in configuration but not supported"
                .format(method_name))

        try:
            logger.info("\tRunning method {}".format(method_name))
            logger.debug("\t{} ({})".format(method_name, method_args))

            # Store plot title for HTML tittle and remove from data passed to plotly
            plot_title = method_args["plot_title"]
            method_args["plot_title"] = ""

            # Get method and generate plot
            method = getattr(p, method_name)
            fig = method(**method_args)
            plot = py.plot(fig,
                           output_type='div',
                           include_plotlyjs=False,
                           image_width='',
                           image_height='',
                           show_link=False,
                           auto_open=False)

            plots.append(plot)
            titles.append(plot_title)

        except pycoQCError as E:
            logger.info("\t\t{}".format(E))

    logger.warning("WRITE HTML REPORT")

    # Load HTML template for Jinja
    logger.info("\tLoad HTML template")
    template = get_jinja_template(template_file)

    # Set a title for the HTML report
    report_title = ""
    if title:
        report_title += title + "<br>"
    report_title += "generated on " + datetime.datetime.now().strftime(
        "%d/%m/%y")

    # # Calculate SHA checksum and pass it to template
    # with open(summary_file, 'rb') as f:
    #     contents = f.read()
    # summary_file_hash = hashlib.sha256(contents).hexdigest()

    # Render plots
    logger.info("\tRender plots with Jinja2")
    rendering = template.render(plots=plots,
                                titles=titles,
                                plotlyjs=py.get_plotlyjs(),
                                report_title=report_title)

    # Write to HTML file
    logger.info("\tWrite to HTML file")
    with open(outfile, "w") as f:
        f.write(rendering)
feature = 'sum'
group = 'A'
title = feature + group + ' ' + vec + ' ' + measure + ' ' + stem[:1]

sem = sem[feature + group]
sur = sur[feature + group]

uni = uni[feature + group]

trace1 = go.Box(x=sem, opacity=1, name='seminal', marker=dict(color='blue'))
trace2 = go.Box(x=sur, opacity=1, name='survey', marker=dict(color='orange'))
trace3 = go.Box(x=uni, opacity=1, name='uninfluential', marker=dict(color='green'))

layout = go.Layout(showlegend=False, autosize=False, width=800, height=250, xaxis_type='log',
                   margin=go.layout.Margin(l=50, r=15, b=40, t=10, pad=4),
                   xaxis=dict(
                       title='Value for ' + feature + group, showgrid=True, gridcolor='#E2E2E2'
                   ),
                   yaxis=dict(
                       showgrid=False
                   ),
                   paper_bgcolor='#FFFFFF', plot_bgcolor='#FFFFFF'
                   )

fig = go.Figure(layout=layout)
fig.add_trace(trace3)
fig.add_trace(trace2)
fig.add_trace(trace1)

plot(fig, get_file_base() + 'plots/' + title, image='jpeg')
Example #54
0
def make_plot(plot_list, title=None):
    data = []
    for idx, plt in enumerate(plot_list):
        color = core.color_defaults[idx % len(core.color_defaults)]
        x = list(plt.xs)

        if plt.display_mode in ["mean_std", "mean_se"]:
            y = list(plt.means)
            if plt.display_mode == "mean_std":
                y_upper = list(plt.means + plt.stds)
                y_lower = list(plt.means - plt.stds)
            elif plt.display_mode == "mean_se":
                y_upper = list(plt.means + plt.ses)
                y_lower = list(plt.means - plt.ses)
            else:
                raise NotImplementedError
            data.append(
                go.Scatter(x=x + x[::-1],
                           y=y_upper + y_lower[::-1],
                           fill='tozerox',
                           fillcolor=core.hex_to_rgb(color, 0.2),
                           line=go.Line(color='transparent'),
                           showlegend=False,
                           legendgroup=plt.legend,
                           hoverinfo='none'))
            data.append(
                go.Scatter(
                    x=x,
                    y=y,
                    name=plt.legend,
                    legendgroup=plt.legend,
                    line=dict(color=core.hex_to_rgb(color)),
                ))
        elif plt.display_mode == "individual":
            for idx, y in enumerate(plt.ys):
                data.append(
                    go.Scatter(
                        x=x,
                        y=y,
                        name=plt.legend,
                        legendgroup=plt.legend,
                        line=dict(color=core.hex_to_rgb(color)),
                        showlegend=idx == 0,
                    ))
        else:
            raise NotImplementedError

    layout = go.Layout(
        legend=dict(
            x=1,
            y=1,
        ),
        title=title,
    )
    fig = go.Figure(data=data, layout=layout)
    fig_div = po.plot(fig, output_type='div', include_plotlyjs=False)
    if "footnote" in plot_list[0]:
        footnote = "<br />".join([
            r"<span><b>%s</b></span>: <span>%s</span>" %
            (plt.legend, plt.footnote) for plt in plot_list
        ])
        return r"%s<div>%s</div>" % (fig_div, footnote)
    else:
        return fig_div
Example #55
0
def main():
    # DATA load
    dateparse = lambda dates: pd.datetime.strptime(dates, '%Y-%m-%d')
    path = r'D:\users\S598658\Projects\Flat_pattern\env\Data_production_variations.csv'
    data = pd.read_csv(path,
                       sep=',',
                       parse_dates=['TimeIndex'],
                       index_col='TimeIndex',
                       date_parser=dateparse)
    data = data[102:162]
    ts = data["Data"]
    last_element = ts.index[-1]
    nb_predictions = 10
    SPLIT = 1 - (nb_predictions / len(ts))
    TRAIN_SIZE = int(len(ts) * SPLIT)
    ts_train, ts_test = ts[0:TRAIN_SIZE], ts[TRAIN_SIZE:len(ts)]

    train = np.array(ts_train)
    test = np.array(ts_test)
    index_test = ts_test.index
    ts_test.reset_index(drop=True, inplace=True)

    ###############

    # Modeling & Prediction DATA
    signal_no_tendance, coef_tendance = _retirerTendance(train)
    FRAC = 1 / 20
    coefficients = _LOESS(ts_train, frac=FRAC)
    WINDOW = len(ts_test)
    coefficients = coefficients[len(coefficients) - WINDOW:]
    index_train = coefficients.index
    TS_train = pd.Series(index=index_train, data=coefficients.values)

    first_coef = TS_train.values[1]
    last_coef = TS_train.values[-1]
    delta = last_coef - first_coef
    DELTA = 0.1
    coefficients.reset_index(drop=True, inplace=True)
    print('delta :')
    print(delta)
    f = interp1d(coefficients.index, coefficients.values)
    if (abs(delta) > DELTA):
        y_predictions = f(ts_test.index) + delta
        print('ajout delta')
    else:
        y_predictions = f(ts_test.index)

    TS_pred = pd.Series(index=index_test, data=y_predictions)
    TS_final = pd.concat([TS_train, TS_pred], axis=0)

    mean_signal = int(sum(TS_pred)) / len(TS_pred)
    std = np.std(TS_pred)
    born_sup = TS_pred + (mean_signal - 1.96 * std) / np.sqrt(len(test))
    born_inf = TS_pred - (mean_signal - 1.96 * std) / np.sqrt(len(test))

    # Estimation error on predictions
    _error(ts_test, TS_pred)

    ############

    # Plotting results
    trace0 = go.Scatter(x=data.index, y=data.Data, mode='markers', name='TS')
    trace1 = go.Scatter(x=TS_train.index,
                        y=TS_train,
                        mode='lines',
                        name='Model Fourier')
    trace2 = go.Scatter(x=TS_pred.index,
                        y=TS_pred,
                        mode='lines',
                        name='Predictions Loess & interpolation')
    trace3 = go.Scatter(x=TS_pred.index,
                        y=born_sup,
                        name='Born Sup',
                        line=dict(color='gray', dash='dot'))
    trace4 = go.Scatter(x=TS_pred.index,
                        y=born_inf,
                        name='Born Inf',
                        line=dict(color='gray', dash='dot'))
    trace5 = go.Scatter(x=TS_pred.index,
                        y=born_sup,
                        fill='tonexty',
                        mode='none',
                        name='IC 95%')
    layout = go.Layout({
        'shapes': [{
            'type': 'line',
            'x0': '2015-01-01',
            'y0': 1,
            'x1': last_element,
            'y1': 1,
            'line': {
                'color': 'red',
            }
        }, {
            'type': 'line',
            'x0': '2015-01-01',
            'y0': -1,
            'x1': last_element,
            'y1': -1,
            'line': {
                'color': 'red'
            }
        }]
    })

    donnees = [trace0, trace1, trace2, trace3, trace4, trace5]
    fig = dict(data=donnees, layout=layout)
    py.plot(fig, filename='TS_Loess_interp.html')
Example #56
0
def DrawHeatMap(data):
    fig = px.density_mapbox(data, lat='lat', lon='lon', z='size', radius=15,
                            color_continuous_scale=px.colors.sequential.Sunsetdark, height=800, width=1000)
    pltoff.plot(fig, filename='1_forecast_heatmap_19.html')
Example #57
0
                    )
    fig.update_layout(
        margin={'l': 0, 't': 0, 'b': 0, 'r': 0},
        mapbox={
            # 'center': {'lon': places19.ln[0], 'lat': places19.lat[0]},
            # 'style': "stamen-terrain",
            # 'center': {'lon': places19.ln[0], 'lat': places19.lat[0]},
            'zoom': 1})
    pltoff.plot(fig, filename='1_forecast_heatmap.html')

# draw heatmap
def DrawHeatMap(data):
    fig = px.density_mapbox(data, lat='lat', lon='lon', z='size', radius=15,
                            color_continuous_scale=px.colors.sequential.Sunsetdark, height=800, width=1000)
    pltoff.plot(fig, filename='1_forecast_heatmap_19.html')


data = ConvertFile('GM_19.txt')
DrawHeatMap(data)
# print(data)
# DrawMapScatter(data)
'''''
fig=px.scatter_mapbox(data,lat='lat',lon='lon',color='size',
                      color_continuous_scale=px.colors.sequential.Agsunset,
                      center={'lat':48.95559,'lon':-122.661},
                      mapbox_style='satellite',
                      #width=600,height=500)
                      )
pltoff.plot(fig,filename='1_forecast_heatmap.html')
'''
Convert the data to stationary if it is not Converting from time series to supervised
for having the feature set of LSTM model Scale the data- check if the data is not stationary? see plot: 
'''

#plot monthly sales

plot_data = [go.Scatter(
    x=data['BILLING_DATE'],
    y=data['SALES_VOLUME'],
)]

plot_layout = go.Layout(title='VSP Monthly Sales - Volume Analysis')
fig = go.Figure(data=plot_data, layout=plot_layout)
pyoff.iplot(fig)
# Below code prints plot as html page in new window in browser
pyoff.plot(fig)
'''
Data is not stationary and has an increasing trend over the months.
Get the difference in sales compared to the previous month and build the model on it:
'''
#create a new dataframe to model the difference in sales volume
# df_diff = data.copy() is causing keyerror and hence changed df_diff = data.copy() to df_diff = data

# df_diff = data.copy()

df_diff = data

#add previous sales to the next row
data['prev_sales'] = df_diff['SALES_VOLUME'].shift(1)
#drop the null values and calculate the difference
df_diff = df_diff.dropna()
from plotly.graph_objs import Bar,Layout
from plotly import offline


from die import Die
#Utworzenie kosci typu D6 i D10
die_1=Die()
die_2=Die()
#Wykonanie pewnej liczby rzutów i umieszczenie wynikow na liscie
results = []
for roll_num in range(11000):
	result = die_1.roll()*die_2.roll()
	results.append(result)
#Analizam wynikow
frequencies = []
max_result = die_1.num_sides * die_2.num_sides
for value in range(1,max_result+1):
	frequency = results.count(value)
	frequencies.append(frequency)
#Wizualizacja wynikow na histogramie
x_values = list(range(1,max_result+1))
data = [Bar(x=x_values,y=frequencies)]

x_axis_config ={'title':"Wynik",'dtick':1}
y_axis_config = {'title': 'Częstotliwośc występowania wartości'}
my_layout = Layout(title='Wynik wyrzucenia dwoma kośćmi D6 i pomnożenia ich przez sie - ilosc probek 5000',xaxis=x_axis_config,yaxis = y_axis_config)
offline.plot({'data':data,'layout':my_layout},filename='d6xd6.html')
Example #60
0
    text_annotation = [annotation] * 10
    x_trace.append(None)
    y_trace.append(None)
    text_annotation.append(None)
    edge_trace['x'] += x_trace
    edge_trace['y'] += y_trace
    edge_trace['text'] += text_annotation

for node in G.nodes():
    x, y = graph_pos[node]
    node_trace['x'].append(x)
    node_trace['y'].append(y)
    node_info = ''.join(
        (str(node + 1), ': ', str(list(topic_terms[node])[:n_ann_terms])))
    node_trace['text'].append(node_info)

for node, adjacencies in enumerate(G.adjacency()):
    node_trace['marker']['color'].append(len(adjacencies))

fig = Figure(data=Data([edge_trace, node_trace]),
             layout=Layout(showlegend=False,
                           hovermode='closest',
                           xaxis=XAxis(showgrid=True,
                                       zeroline=False,
                                       showticklabels=True),
                           yaxis=YAxis(showgrid=True,
                                       zeroline=False,
                                       showticklabels=True)))

py.plot(fig)