Example #1
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
def make_subplots(one_one_traces = [], one_two_traces = [], two_one_traces = [], two_two_traces = []):
    if two_one_traces or two_two_traces:
        fig = tools.make_subplots(rows=2, cols=2)
    else:
        fig = tools.make_subplots(rows=1, cols=2)
    for trace in one_one_traces:
        fig.append_trace(trace, 1, 1)
    for trace in one_two_traces:
        fig.append_trace(trace, 1, 2)
    for trace in two_one_traces:
        fig.append_trace(trace, 1, 1)
    for trace in two_two_traces:
        fig.append_trace(trace, 1, 2)
    iplot(fig)
def combine_two_heatmaps_xaxis(plot1, plot2, plotname):

    #combine plots in a panel
    fig = tools.make_subplots(rows=1, cols=2, print_grid=False)
    for trace in plot1['data']:
        trace['colorbar']['x'] = 0.4
        trace['colorbar']['thickness'] = 20
        fig.append_trace(trace, 1, 1)

    for trace in plot2['data']:
        trace['colorbar']['x'] = 0.95
        trace['colorbar']['xpad'] = 50
        trace['colorbar']['thickness'] = 20
        fig.append_trace(trace, 1, 2)

    fig['layout']['title']  = ""
    fig['layout']['xaxis1'].update(plot1['layout']['xaxis'])
    fig['layout']['xaxis1']['title'] = ""
    fig['layout']['xaxis1']['domain'] = [0,0.4]
    fig['layout']['yaxis1'].update(plot1['layout']['yaxis'])
    fig['layout']['yaxis1']['title'] = ""

    fig['layout']['xaxis2'].update(plot2['layout']['xaxis'])
    fig['layout']['yaxis2'].update(plot2['layout']['yaxis'])
    fig['layout']['xaxis2']['domain'] = [0.55, 0.95]
    fig['layout']['yaxis2']['side']='right'
    fig['layout']['yaxis2']['scaleanchor'] = 'x2'
    fig['layout']['xaxis2']['title'] = ""
    fig['layout']['yaxis2']['title'] = ""

    fig['layout']['font']['size']=18
    fig['layout']['hovermode']='closest'
    fig['layout']['margin']['t'] = 10

    plotly_plot(fig, filename=plotname, auto_open=False)
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)
Example #5
0
    def generate_plot(self, key, ranges=None):
        ranges = self.compute_ranges(self.layout, self.keys[-1], None)
        plots = [[] for i in range(self.rows)]
        insert_rows, insert_cols = [], []
        for r, c in self.coords:
            subplot = self.subplots.get((r, c), None)
            if subplot is not None:
                subplots = subplot.generate_plot(key, ranges=ranges)

                # Computes plotting offsets depending on
                # number of adjoined plots
                offset = sum(r >= ir for ir in insert_rows)
                if len(subplots) > 2:
                    # Add pad column in this position
                    insert_cols.append(c)
                    if r not in insert_rows:
                        # Insert and pad marginal row if none exists
                        plots.insert(r+offset, [None for _ in range(len(plots[r]))])
                        # Pad previous rows
                        for ir in range(r):
                            plots[ir].insert(c+1, None)
                        # Add to row offset
                        insert_rows.append(r)
                        offset += 1
                    # Add top marginal
                    plots[r+offset-1] += [subplots.pop(-1), None]
                elif len(subplots) > 1:
                    # Add pad column in this position
                    insert_cols.append(c)
                    # Pad previous rows
                    for ir in range(r):
                        plots[r].insert(c+1, None)
                    # Pad top marginal if one exists
                    if r in insert_rows:
                        plots[r+offset-1] += 2*[None]
                else:
                    # Pad top marginal if one exists
                    if r in insert_rows:
                        plots[r+offset-1] += [None] * (1+(c in insert_cols))
                plots[r+offset] += subplots
                if len(subplots) == 1 and c in insert_cols:
                    plots[r+offset].append(None)

        rows, cols = len(plots), len(plots[0])
        fig = tools.make_subplots(rows=rows, cols=cols, print_grid=False,
                                  horizontal_spacing=self.hspacing,
                                  vertical_spacing=self.vspacing)

        width, height = self._get_size()
        ax_idx = 0
        for r, row in enumerate(plots):
            for c, plot in enumerate(row):
                ax_idx += 1
                if plot:
                    add_figure(fig, plot, r, c, ax_idx)
        fig['layout'].update(height=height, width=width,
                             title=self._format_title(key))

        self.handles['fig'] = fig
        return self.handles['fig']
Example #6
0
def region_sectors_plot():
    Const_Res = go.Scatter(x=df_region_sec_line["period"], y=df_region_sec_line["Region: Const./Res"], name= "Const./Res")
    FIRE = go.Scatter(x=df_region_sec_line["period"], y=df_region_sec_line["Region: FIRE"], name= "FIRE" )
    Manufacturing = go.Scatter(x=df_region_sec_line["period"], y=df_region_sec_line["Region: Manufacturing"], name= "Manufacturing")
    Retail = go.Scatter(x=df_region_sec_line["period"], y=df_region_sec_line["Region: Retail"], name= "Retail")
    Service = go.Scatter(x=df_region_sec_line["period"], y=df_region_sec_line["Region: Service"], name = "Service")
    WTU = go.Scatter(x=df_region_sec_line["period"], y=df_region_sec_line["Region: WTU"], name = "WTU")
    Government = go.Scatter(x=df_region_sec_line["period"], y=df_region_sec_line["Region: Government"], name = "Government")
    Education = go.Scatter(x=df_region_sec_line["period"], y=df_region_sec_line["Region: Education"], name= "Education")

    fig = tools.make_subplots(rows=2, cols=4, subplot_titles=('Const./Res', 'FIRE', 'Manufacturing', 'Retail', 'Service', 'WTU', 'Government', 'Education'), 
                          )

    fig.append_trace(Const_Res, 1, 1)
    fig.append_trace(FIRE, 1, 2)
    fig.append_trace(Manufacturing, 1, 3)
    fig.append_trace(Retail, 1, 4)
    fig.append_trace(Service, 2, 1)
    fig.append_trace(WTU, 2, 2)
    fig.append_trace(Government, 2, 3)
    fig.append_trace(Education, 2, 4)

    fig['layout'].update(title="Regional Wage & Salary Jobs " + str(hist_year)+ "-" + str(df_sa_region_total.iloc[-1,0])+":" +
                                                  ' PSRC Industries; (Available latest month: ' + str(df_region_sec_line.iloc[-1,1])+", "+str(df_region_sec_line.iloc[-1,0])+')', 
                                     showlegend = False, plot_bgcolor='rgba(245, 246, 249, 1)', titlefont = dict(color= "black", size = 25 ), margin=dict(t=100, b=75,autoexpand=True), 
                )

    plotly.offline.plot(fig, filename = "2. Region_10-16_Sectors"+"_"+time.strftime("%m%d%Y")+".html")
def test_append_trace_col_out_of_range():
    trace = Scatter(
           x=[1,2,3],
           y=[2,3,4]
    )
    fig = tls.make_subplots(rows=2, cols=3)
    fig.append_trace(trace, 2, 0)
def test_append_scatter3d():
    expected = Figure(
        data=Data([
            Scatter3d(
                x=[1, 2, 3],
                y=[2, 3, 4],
                z=[1, 2, 3],
                scene='scene2'
            ),
            Scatter3d(
                x=[1, 2, 3],
                y=[2, 3, 4],
                z=[1, 2, 3],
                scene='scene2'
            )
        ]),
        layout=Layout(
            scene1=Scene(
                domain={'y': [0.575, 1.0], 'x': [0.0, 1.0]}
            ),
            scene2=Scene(
                domain={'y': [0.0, 0.425], 'x': [0.0, 1.0]}
            )
        )
    )

    fig = tls.make_subplots(rows=2, cols=1,
                            specs=[[{'is_3d': True}], 
                                   [{'is_3d': True}]])
    trace = Scatter3d(x=[1, 2, 3], y=[2, 3, 4], z=[1, 2, 3])
    fig.append_trace(trace, 1, 1)
    fig.append_trace(trace, 2, 1)
    assert fig == expected
Example #9
0
    def generate_plot(self, key, ranges=None):
        ranges = self.compute_ranges(self.layout, self.keys[-1], None)
        plots = [[] for r in range(self.cols)]
        for i, coord in enumerate(self.layout.keys(full_grid=True)):
            r = i % self.cols
            subplot = self.subplots.get(wrap_tuple(coord), None)
            if subplot is not None:
                plot = subplot.initialize_plot(ranges=ranges)
                plots[r].append(plot)
            else:
                plots[r].append(None)

        rows, cols = len(plots), len(plots[0])
        fig = tools.make_subplots(rows=rows, cols=cols, print_grid=False,
                                  shared_xaxes=True, shared_yaxes=True,
                                  horizontal_spacing=self.hspacing,
                                  vertical_spacing=self.vspacing)
        ax_idx = 0
        for r, row in enumerate(plots):
            for c, plot in enumerate(row):
                ax_idx += 1
                if plot:
                    add_figure(fig, plot, r, c, ax_idx)
        w, h = self._get_size(subplot.width, subplot.height)
        fig['layout'].update(width=w, height=h,
                             title=self._format_title(key))

        self.handles['fig'] = fig
        return self.handles['fig']
def test_specs_colspan_rowpan_bottom_left():
    expected = Figure(
        data=Data(),
        layout=Layout(
            xaxis1=XAxis(domain=[0.0, 0.6444444444444445], anchor="y1"),
            xaxis2=XAxis(domain=[0.7111111111111111, 1.0], anchor="y2"),
            xaxis3=XAxis(domain=[0.7111111111111111, 1.0], anchor="y3"),
            xaxis4=XAxis(domain=[0.0, 0.2888888888888889], anchor="y4"),
            xaxis5=XAxis(domain=[0.35555555555555557, 0.6444444444444445], anchor="y5"),
            xaxis6=XAxis(domain=[0.7111111111111111, 1.0], anchor="y6"),
            yaxis1=YAxis(domain=[0.0, 0.6333333333333333], anchor="x1"),
            yaxis2=YAxis(domain=[0.0, 0.26666666666666666], anchor="x2"),
            yaxis3=YAxis(domain=[0.36666666666666664, 0.6333333333333333], anchor="x3"),
            yaxis4=YAxis(domain=[0.7333333333333333, 1.0], anchor="x4"),
            yaxis5=YAxis(domain=[0.7333333333333333, 1.0], anchor="x5"),
            yaxis6=YAxis(domain=[0.7333333333333333, 1.0], anchor="x6"),
        ),
    )

    fig = tls.make_subplots(
        rows=3,
        cols=3,
        specs=[[{"colspan": 2, "rowspan": 2}, None, {}], [None, None, {}], [{}, {}, {}]],
        start_cell="bottom-left",
    )
    assert fig == expected
Example #11
0
def monthly_change_plot():
    Sector_Change = go.Bar(
        x=["Const./Res", "FIRE", "Manufacturing", "Retail", "Services", "WTU", "Education", "Government", "PSRC Region"], # assign x as the dataframe column 'x'
        y=df_region_sec_subs.ix[0:-1,0],
        marker = dict(color = ['rgb(90,180,172)','rgb(90,180,172)','rgb(90,180,172)','rgb(90,180,172)','rgb(90,180,172)','rgb(90,180,172)','rgb(90,180,172)',
                               'rgb(90,180,172)','rgb(241,105,19)'], 
                      line = dict(color= ['rgb(1,102,94)','rgb(1,102,94)','rgb(1,102,94)','rgb(1,102,94)', 'rgb(1,102,94)','rgb(1,102,94)','rgb(1,102,94)',
                                          'rgb(1,102,94)','rgb(217,72,1)' ], width = 2) ),
        hoverinfo= "x+y"
    )
    
    Region_Change = go.Bar(
        x=["King & Snohomish Counties", "Pierce County", "Kitsap County", "PSRC Region"], # assign x as the dataframe column 'x'
        y=df_region_subs[0],
        marker = dict(color = ['rgb(128,125,186)','rgb(65,171,93)', 'rgb(29,145,192)','rgb(241,105,19)'], 
                      line = dict(color= ['rgb(106,81,163)','rgb(35,132,67)','rgb(0,90,50)','rgb(217,72,1)' ], width = 2) ),
    
       hoverinfo = "x+y"
    )

    title_region = ('Monthly Wage & Salary Jobs: Change in Counties, '+ str(df_sa_region_total.iloc[-2,1])+"-"+str(df_sa_region_total.iloc[-2,0])+
                                                " to " +  str(df_sa_region_total.iloc[-1,1])+"-"+str(df_sa_region_total.iloc[-1,0]))
    title_sec=('Monthly Wage & Salary Jobs: Change in Industries, '+ str(df_sa_region_total.iloc[-2,1])+"-"+str(df_sa_region_total.iloc[-2,0])+
                                                " to " +  str(df_sa_region_total.iloc[-1,1])+"-"+str(df_sa_region_total.iloc[-1,0]))

    fig = tools.make_subplots(rows=1, cols=2, subplot_titles=(title_region, title_sec))


    fig.append_trace(Region_Change, 1, 1)
    fig.append_trace(Sector_Change, 1,2)
    
    fig['layout'].update(title = ('Changes in Wage & Salary Jobs: '+ str(df_sa_region_total.iloc[-2,1])+"-"+str(df_sa_region_total.iloc[-2,0])+
                                                " to " +  str(df_sa_region_total.iloc[-1,1])+"-"+str(df_sa_region_total.iloc[-1,0])),
                                             showlegend = False, plot_bgcolor='rgba(245, 246, 249, 1)',  titlefont = dict(color= "black", size = 25 ))
    plotly.offline.plot(fig, filename = "3. Region_Monthly_Change"+"_"+time.strftime("%m%d%Y")+".html")
Example #12
0
 def renderIfExhausted(self):
     if not self.isExhausted():
         return False
     fig = tools.make_subplots(self.rows, self.cols, subplot_titles=tuple(self.makeTitles()), print_grid=False)
     row = 1
     col = 1
     n = 1
     for f in self.figures:
         for trace in f.getScatterGOs():
             fig.append_trace(trace, row, col)
         col += 1
         n += 1
         if col > row:
             while col <= self.cols:
                 for key in ('xaxis'+str(n), 'yaxis'+str(n)):
                     fig['layout'][key]['showgrid'] = False
                     fig['layout'][key]['showline'] = False
                     fig['layout'][key]['showticklabels'] = False
                     fig['layout'][key]['ticks'] = ''
                     fig['layout'][key]['zeroline'] = False
                 n+=1
                 col+=1
             col = 1
             row += 1
             if row > self.rows:
                 row = self.rows
     plotly.offline.iplot(fig)
     return True
Example #13
0
 def renderIfExhausted(self):
     if not self.isExhausted():
         return False
     fig = tools.make_subplots(self.rows, self.cols, subplot_titles=tuple(f.title for f in self.figures), print_grid=False)
     row = 1
     col = 1
     n = 1
     for f in self.figures:
         for trace in f.getScatterGOs():
             fig.append_trace(trace, row, col)
         if f.logx:
             fig['layout']['xaxis'+str(n)]['type'] = 'log'
             fig['layout']['xaxis'+str(n)]['autorange'] = True
         if f.logy:
             fig['layout']['yaxis'+str(n)]['type'] = 'log'
             fig['layout']['yaxis'+str(n)]['autorange'] = True
         col += 1
         n += 1
         if col > self.cols:
             col = 1
             row += 1
             if row > self.rows:
                 row = self.rows
     plotly.offline.iplot(fig)
     return True
def plotGraphComps():
    bttrace = go.Bar(x=time, y=bt)
    activitytrace = go.Bar(x=time, y=activity)
    audiotrace = go.Bar(x=time, y=audio)
    locationtrace = go.Bar(x=time, y=location)

    fig = tools.make_subplots(rows=4, cols=1,
                              subplot_titles=("Proximity", "Speech", "Physical Activity", "Location"),
                              shared_xaxes=True)
    fig.append_trace(bttrace, 1, 1, )
    fig.append_trace(audiotrace, 2, 1)
    fig.append_trace(activitytrace, 3, 1)
    fig.append_trace(locationtrace, 4, 1)
    fig['layout'].update(width=800,
                         height=600,
                         showlegend=False,
                         title=''
                         )


    # Set x Axes
    fig['layout']['xaxis1'].update(timexaxis)
    # Set y Axes
    fig['layout']['yaxis1'].update(booleanyaxes)
    fig['layout']['yaxis2'].update(booleanyaxes)
    fig['layout']['yaxis3'].update(booleanyaxes)
    plotly.offline.plot(fig, filename='comps.html')
Example #15
0
 def plot(self):
     fig = pytls.make_subplots(shared_xaxes=True, rows=30, cols=1)
     for colname in self.normalized.columns.values:
         fig.append_trace({'x': self.normalized.index, 'y': self.normalized[colname],
                           'type':'scatter', 'name': self.PRETTYCOLNAMES[colname][0]},
                           self.PRETTYCOLNAMES[colname][1], 1)
     py.iplot(fig,filename='biostampy')
def correlation_analysis(dataset):
    # read
    tb = pd.read_sql_table(dataset, db, index_col = 'ID')
    X = tb.iloc[:, :-1]; y = tb.iloc[:, -1]
    # compute correlation
    X.drop(X.columns[X.var() < 1e-5], axis = 1, inplace = True)
    r = np.array([pearsonr(X.ix[:,i], y) for i in range(X.shape[1])])
    rank = np.abs(r[:, 0]).argsort()[::-1]
    # plot top ones
    N = 9
    top = rank[:N]
    traces = []
    names = []
    for (i, c) in enumerate(X.columns[top]):
        names.append('{}<br>(r={:0.2g} p={:0.2g})'.format(
            c, r[top[i], 0], r[top[i], 1]))
        traces.append(go.Scatter(x = X[c].values.tolist(),
                                 y = y.values.tolist(),
                                 mode = 'markers',
                                 showlegend = False))
    fig = tools.make_subplots(rows = 3, cols = 3,
                              subplot_titles = names,
                              vertical_spacing = 0.1,
                              horizontal_spacing = 0.1)
    for (i, p) in enumerate(traces):
        fig.append_trace(p, i // 3 + 1, i % 3 + 1)
    fig['layout'].update(height = 700, width = 1100)
    fig['layout'].update(margin = go.Margin(l = 50, r = 50, b = 50,
                                            t = 50, pad = 0))
    for a in fig.layout.annotations:
        a['font'].update(size = 14)
    return (X.columns[rank], utils.plot_to_div(fig))
def test_append_scatter3d_after_deleting_scene():
    fig = tls.make_subplots(rows=2, cols=1,
                            specs=[[{'is_3d': True}], 
                                   [{'is_3d': True}]])
    trace = Scatter3d(x=[1, 2, 3], y=[2, 3, 4], z=[1, 2, 3])
    fig['layout'].pop('scene1', None)
    fig.append_trace(trace, 1, 1)
def regression_and_rmse(scatter_trace, regression_traces, rss_calc_trace):
    fig = tools.make_subplots(rows=1, cols=2)
    for reg_trace in regression_traces:
        fig.append_trace(reg_trace, 1, 1)
    fig.append_trace(scatter_trace, 1, 1)
    fig.append_trace(rss_calc_trace, 1, 2)
    iplot(fig)
Example #19
0
    def save(self, filename = "spectra.xhtml", mzRange = 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 = "spectra.xhtml"
            mzRange (tuple): m/z range which should be considered [start, end]. Default = None
        """
        if mzRange == None:
            mzRange = [-float('inf'), float('Inf')]

        plotNumber = len(self.plots)
        rows, cols = int(math.ceil(plotNumber/float(2))), 2

        # enable possibility to have different subplots in on Pic
        if plotNumber%2 == 0:
            myFigure = 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])
            myFigure = tools.make_subplots(rows=rows, cols=cols, vertical_spacing=0.6/rows, specs=specs)
    
        for i, plot in enumerate(self.plots):
            for j, trace in enumerate(plot):
                trace['y'] = [self.functionMapper[x](i) if x in self.functionMapper and mzRange[0] <= x <= mzRange[1] else x for x in trace['y']]
                myFigure.append_trace(trace, int(math.ceil((i/2)+1)), (i%2)+1)

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

        myFigure['layout']['legend'].update(font={ 'size' :10,
                                                        'color' : '#FF0000'
                                                        })
        #myFigure['layout']['title'].update(title=self.header[-1])
        plt.plot(myFigure, filename=filename, auto_open=False)
        return
Example #20
0
def violin_dict(data, data_header, group_header, colors, use_colorscale,
                group_stats, rugplot, sort, height, width, title):
    """
    Refer to FigureFactory.create_violin() for docstring.

    Returns fig for violin plot without colorscale.

    """

    # collect all group names
    group_name = []
    for name in data[group_header]:
        if name not in group_name:
            group_name.append(name)

    if sort:
        group_name.sort()

    # check if all group names appear in colors dict
    for group in group_name:
        if group not in colors:
            raise exceptions.PlotlyError("If colors is a dictionary, all "
                                         "the group names must appear as "
                                         "keys in colors.")

    gb = data.groupby([group_header])
    L = len(group_name)

    fig = make_subplots(rows=1, cols=L,
                        shared_yaxes=True,
                        horizontal_spacing=0.025,
                        print_grid=False)

    for k, gr in enumerate(group_name):
        vals = np.asarray(gb.get_group(gr)[data_header], np.float)
        plot_data, plot_xrange = violinplot(vals, fillcolor=colors[gr],
                                            rugplot=rugplot)
        layout = graph_objs.Layout()

        for item in plot_data:
            fig.append_trace(item, 1, k + 1)

        # add violin plot labels
        fig['layout'].update(
            {'xaxis{}'.format(k + 1): make_XAxis(group_name[k], plot_xrange)}
        )

    # set the sharey axis style
    fig['layout'].update({'yaxis{}'.format(1): make_YAxis('')})
    fig['layout'].update(
        title=title,
        showlegend=False,
        hovermode='closest',
        autosize=False,
        height=height,
        width=width
    )

    return fig
Example #21
0
 def plot_pca(self, ncomponents=6):
     pca_df = self.get_pca_dataframe(ncomponents=ncomponents)
     fig = pytls.make_subplots(shared_xaxes=True, rows=ncomponents, cols=1)
     for col in range(0, ncomponents):
         fig.append_trace({'x': pca_df.index, 'y': pca_df[col],
                           'type':'scatter', 'name': ("Component " + str(col+1))},
                          col+1, 1)
     py.iplot(fig, filename=('biostampy-pca:' + str(ncomponents)))
def test_append_scatter_after_deleting_yaxis():
    trace = Scatter(
           x=[1,2,3],
           y=[2,3,4]
    )
    fig = tls.make_subplots(rows=2, cols=3)
    fig['layout'].pop('yaxis5', None)
    fig.append_trace(trace, 2, 2)
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))
 def multi_plot(self, width = 2480, height = 3508):
     # Print A4 in px: 2480x3508
     rows = int(math.ceil(float(len(self.multi_figures))/self.multi_columns))
     # Reminder: it's possible to make some figures span multiple columns, probably not going to use that though
     figure = plt.make_subplots(rows=rows, cols=self.multi_columns, subplot_titles=self.multi_titles)
     for i in xrange(len(self.multi_figures)):
         figure.append_trace(self.multi_figures[i], (i%self.multi_columns)+1, (i%rows)+1)
     figure['layout'].update(width=width, height=height, title=self.multi_name)
     self._plot(figure, self.multi_name)
Example #25
0
    def plot_loss_reward(total_losses, total_rewards):

    figure = tools.make_subplots(rows=1, cols=2, subplot_titles=('loss', 'reward'), print_grid=False)
    figure.append_trace(Scatter(y=total_losses, mode='lines', line=dict(color='skyblue')), 1, 1)
    figure.append_trace(Scatter(y=total_rewards, mode='lines', line=dict(color='orange')), 1, 2)
    figure['layout']['xaxis1'].update(title='epoch')
    figure['layout']['xaxis2'].update(title='epoch')
    figure['layout'].update(height=400, width=900, showlegend=False)
    iplot(figure)
Example #26
0
    def __init__(self, rows, cols, title="", subtitles=[], height=1000, width=1000):
        """
        Initialization.
        """
        self.rows = rows
        self.cols = cols

        self.fig = tools.make_subplots(rows=rows,cols=cols,
            subplot_titles=subtitles)
        self.fig['layout'].update(height=height,width=width, title=title)
Example #27
0
def violin_no_colorscale(data, data_header, group_header, colors,
                         use_colorscale, group_stats, rugplot,
                         height, width, title):
    """
    Refer to FigureFactory.create_violin() for docstring.

    Returns fig for violin plot without colorscale.

    """

    # collect all group names
    group_name = []
    for name in data[group_header]:
        if name not in group_name:
            group_name.append(name)
    group_name.sort()

    gb = data.groupby([group_header])
    L = len(group_name)

    fig = make_subplots(rows=1, cols=L,
                        shared_yaxes=True,
                        horizontal_spacing=0.025,
                        print_grid=False)
    color_index = 0
    for k, gr in enumerate(group_name):
        vals = np.asarray(gb.get_group(gr)[data_header], np.float)
        if color_index >= len(colors):
            color_index = 0
        plot_data, plot_xrange = violinplot(vals,
                                            fillcolor=colors[color_index],
                                            rugplot=rugplot)
        layout = graph_objs.Layout()

        for item in plot_data:
            fig.append_trace(item, 1, k + 1)
        color_index += 1

        # add violin plot labels
        fig['layout'].update(
            {'xaxis{}'.format(k + 1): make_XAxis(group_name[k], plot_xrange)}
        )

    # set the sharey axis style
    fig['layout'].update({'yaxis{}'.format(1): make_YAxis('')})
    fig['layout'].update(
        title=title,
        showlegend=False,
        hovermode='closest',
        autosize=False,
        height=height,
        width=width
    )

    return fig
Example #28
0
    def _get_box_plot_object(self) -> go.Figure:
        """Get box plot for the entire corpus.

        :return: A plotly object that contains the box plot.
        """
        # Get file names.
        labels = [self._id_temp_label_map[file_id]
                  for file_id in self._active_doc_term_matrix.index.values]

        # Set up the points.
        scatter_plot = go.Scatter(
            x=[0 for _ in labels],
            y=self._active_doc_term_matrix.sum(1).values,
            name="Corpus Scatter Plot",
            hoverinfo="text",
            mode="markers",
            text=labels
        )

        # Set up the box plot.
        box_plot = go.Box(
            x0=0,  # Initial position of the box plot
            y=self._active_doc_term_matrix.sum(1).values,
            name="Corpus Box Plot",
            hoverinfo="y",
            marker=dict(color='rgb(10, 140, 200)')
        )

        # Create a figure with two subplots and fill the figure.
        figure = tools.make_subplots(rows=1, cols=2, shared_yaxes=True)
        figure.append_trace(trace=scatter_plot, row=1, col=1)
        figure.append_trace(trace=box_plot, row=1, col=2)

        # Hide useless information on x-axis and set up title.
        figure.layout.update(
            title="Document Size Statistics of the Given Corpus",
            xaxis=dict(
                title="Scatter plot of Text Size",
                showgrid=False,
                zeroline=False,
                showline=False,
                showticklabels=False
            ),
            xaxis2=dict(
                title="Box Plot of Counts",
                showgrid=False,
                zeroline=False,
                showline=False,
                showticklabels=False
            ),
            hovermode="closest"
        )

        # Return the plotly figure.
        return figure
Example #29
0
def _two_plot(main_fig, secondary_fig, title="", share_xaxis=True):
    figure = tools.make_subplots(
        print_grid=False, shared_xaxes=share_xaxis, rows=2, cols=1
    )
    [figure.append_trace(datum, 1, 1) for datum in main_fig["data"]]
    [figure.append_trace(datum, 2, 1) for datum in secondary_fig["data"]]
    figure["layout"].update(title=title, showlegend=False)
    figure["layout"]["yaxis1"].update(domain=[0.40, 1.0])
    figure["layout"]["yaxis2"].update(domain=[0.0, 0.15])

    return figure
def traces_to_panels(traces, names=[], ylabs=None, xlabs=None):
    r, c, locs = panel_arrangement(len(traces))
    multi = tools.make_subplots(rows=r, cols=c, subplot_titles=names)
    for idx, loc in enumerate(locs):
        if idx < len(traces):
            for component in traces[idx]:
                multi.append_trace(component, *loc)
        else:
            multi = panel_invisible(multi, idx+1)
    multi.layout['showlegend']=False
    return multi
Example #31
0
def get_fig(currency_pair, ask, bid, type_trace, studies, period):
    # Get OHLC data
    data_frame = currency_pair_data[currency_pair]
    t = datetime.datetime.now()
    data = data_frame.loc[
        : t.strftime(
            "2016-01-05 %H:%M:%S"
        )  # all the data from the beginning until current time
    ]
    data_bid = data["Bid"]
    df = data_bid.resample(period).ohlc()

    subplot_traces = [  # first row traces
        "accumulation_trace",
        "cci_trace",
        "roc_trace",
        "stoc_trace",
        "mom_trace",
    ]
    selected_subplots_studies = []
    selected_first_row_studies = []
    row = 1  # number of subplots

    if studies:
        for study in studies:
            if study in subplot_traces:
                row += 1  # increment number of rows only if the study needs a subplot
                selected_subplots_studies.append(study)
            else:
                selected_first_row_studies.append(study)

    fig = tools.make_subplots(
        rows=row,
        shared_xaxes=True,
        shared_yaxes=True,
        cols=1,
        print_grid=False,
        vertical_spacing=0.12,
    )

    # Add main trace (style) to figure
    fig.append_trace(eval(type_trace)(df), 1, 1)

    # Add trace(s) on fig's first row
    for study in selected_first_row_studies:
        fig = eval(study)(df, fig)

    row = 1
    # Plot trace on new row
    for study in selected_subplots_studies:
        row += 1
        fig.append_trace(eval(study)(df), row, 1)

    fig["layout"][
        "uirevision"
    ] = "The User is always right"  # Ensures zoom on graph is the same on update
    fig["layout"]["margin"] = {"t": 50, "l": 50, "b": 50, "r": 25}
    fig["layout"]["autosize"] = True
    fig["layout"]["height"] = 400
    fig["layout"]["xaxis"]["rangeslider"]["visible"] = False
    fig["layout"]["xaxis"]["tickformat"] = "%H:%M"
    fig["layout"]["yaxis"]["showgrid"] = True
    fig["layout"]["yaxis"]["gridcolor"] = "#3E3F40"
    fig["layout"]["yaxis"]["gridwidth"] = 1
    fig["layout"].update(paper_bgcolor="#21252C", plot_bgcolor="#21252C")

    return fig
Example #32
0
init_notebook_mode(connected=True)
#count plot of Low Grade in different schools across regions
#Grades High and Low across region
%matplotlib inline
sns.countplot(school["Grade Low"],palette="vlag")

sns.countplot(school["Grade High"],palette="vlag")

sns.countplot(school["Community School?"])
data = []
city_list = list(school["City"].value_counts().index)
for i in city_list:
    data.append(
        go.Bar(
          y = [school["Percent Asian"][school["City"] == i].mean(), school["Percent Black"][school["City"] == i].mean(), school["Percent Hispanic"][school["City"] == i].mean(), school["Percent White"][school["City"] == i].mean(), school["Others"][school["City"] == i].mean()],
          x = ['Asian','Black','Hispanic', 'White', 'Others'],
          name = i,
          opacity = 0.6
        )
    )
k=0
fig = tools.make_subplots(rows=15, cols=3, subplot_titles=city_list, print_grid=False)
for i in range(1,16):
    for j in range(1,4):
        fig.append_trace(data[k], i, j)
        k = k + 1
fig['layout'].update(height=2000, title='Average racial distribution in different cities',showlegend=False)
iplot(fig)


Example #33
0
def plotly_qc_st(fpath, saveto, sep='\t', name=''):
    bool_success = False
    if not is_nonempty_file(fpath):
        return bool_success
    if not name:
        name = base_name(fpath)

    ST = pd.read_csv(fpath, sep=sep)
    print_logger(('ST UMI-count matrix has '
                  '{} spots x {} genes').format(ST.shape[0], ST.shape[1]))

    ST_total_UMIs = ST.iloc[:, 2:].sum(axis=1)
    ST_detected_genes = (ST.iloc[:, 2:] > 0).sum(axis=1)
    mt_cols = [
        x for x in ST.columns if x.startswith('mt-') or x.startswith('MT-')
    ]
    if not mt_cols:
        ST_percent_mt = 0
    else:
        ST_percent_mt = ST[mt_cols].sum(axis=1) / ST_total_UMIs
        ST_percent_mt.replace(np.inf, 0)

    ST_qc = pd.DataFrame(
        dict(Row=ST.Row,
             Col=ST.Col,
             total_num_UMIs=ST_total_UMIs,
             num_detected_genes=ST_detected_genes,
             percent_mt=ST_percent_mt))

    # 1/3
    plotly_ST_g = plotly_scatter(
        x=ST_qc.Row,
        y=ST_qc.Col,
        mask_by=ST_qc.num_detected_genes,
        hover_text=ST_qc.num_detected_genes.astype('str'),
        colorscale='Viridis',
        mask_title=('#Detected Genes '
                    '(median={})').format(ST_qc.num_detected_genes.median()))
    # 2/3
    plotly_ST_UMIs = plotly_scatter(
        x=ST_qc.Row,
        y=ST_qc.Col,
        mask_by=ST_qc.total_num_UMIs,
        hover_text=ST_qc.total_num_UMIs.astype('str'),
        colorscale='Viridis',
        mask_title=('#Total UMIs '
                    '(median={})').format(ST_qc.total_num_UMIs.median()))
    # 3/3
    plotly_ST_mt = plotly_scatter(x=ST_qc.Row,
                                  y=ST_qc.Col,
                                  mask_by=ST_qc.percent_mt,
                                  hover_text=ST_qc.percent_mt.astype('str'),
                                  colorscale='Viridis',
                                  mask_title=('MT Fraction '
                                              '(median={:6.4f})').format(
                                                  ST_qc.percent_mt.median()))
    # Merge the 3 figures together
    fig = tools.make_subplots(rows=1,
                              cols=3,
                              subplot_titles=('#Total UMIs', '#Detected Genes',
                                              'MT Fraction'))

    fig.append_trace(plotly_ST_UMIs.data[0], 1, 1)
    fig.append_trace(plotly_ST_g.data[0], 1, 2)
    fig.append_trace(plotly_ST_mt.data[0], 1, 3)

    fig['layout'].update(height=600, width=1900, title=name)

    fig.layout.showlegend = False
    # Manually change the locations of other two color bars to proper places
    fig.data[0].marker.colorbar.x = 0.28
    fig.data[1].marker.colorbar.x = 0.64

    plot(fig, filename=saveto, auto_open=False)

    bool_success = True
    return bool_success
Example #34
0
new_frame_Test = new_frame[new_frame['Date'] > '2010-12-31']

trace1 = go.Scatter(x=new_frame['Date'],
                    y=new_frame['T_mod_max'],
                    showlegend=False)
trace2 = go.Scatter(x=new_frame['Date'],
                    y=new_frame['Delta_T_mod'],
                    showlegend=False)
trace3 = go.Scatter(x=new_frame['Date'], y=new_frame['UV'], showlegend=False)
trace4 = go.Scatter(x=new_frame['Date'],
                    y=new_frame['RH_amb'],
                    showlegend=False)

fig = tools.make_subplots(rows=2,
                          cols=2,
                          subplot_titles=('Daily Maximum Module Temperature',
                                          'Daily Cyclic Temperature',
                                          'Daily Average UV Radiation',
                                          'Daily Average Relative Humidity'))

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

fig['layout']['xaxis1'].update(title='Time')
fig['layout']['xaxis2'].update(title='Time')
fig['layout']['xaxis3'].update(title='Time')
fig['layout']['xaxis4'].update(title='Time')

fig['layout']['yaxis1'].update(title='Module Temperature (Degree Celcius)')
fig['layout']['yaxis2'].update(
Example #35
0
trace5 = go.Bar(x=bacteria,
                y=posData["Streptomycin "],
                name='Positive Streptomycin',
                text=posData["Streptomycin "],
                textposition='auto',
                marker=dict(color='rgb(103, 131, 168)', ))

trace6 = go.Bar(x=bacteria,
                y=posData["Neomycin"],
                text=posData["Neomycin"],
                name='Positive Neomycin',
                textposition='auto',
                marker=dict(color='rgb(198, 222, 222)', ))

fig = tools.make_subplots(rows=1, cols=2, shared_yaxes=True)

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

fig.append_trace(trace4, 1, 2)
fig.append_trace(trace5, 1, 2)
fig.append_trace(trace6, 1, 2)

layout = go.Layout(title=go.layout.Title(text="Antibiotics Data"),
                   yaxis=dict(type='log', autorange=True))

fig['layout'].update(layout)

py.iplot(fig, filename='multiple-subplots-shared-yaxes')
Example #36
0
def PCA_sumplot(Z,
                Z_chose,
                Theta_record,
                pca_obj,
                fig_dens_I=[],
                new_data=[],
                Ncols=2,
                PC_select=2,
                height=600,
                width=1000):
    titles = [
        'probab', 'Ave. PC coordinates among kde sampled theta vectors',
        'loadings of PC {}'.format(PC_select + 1)
    ]

    fig_subplots = tools.make_subplots(rows=int(len(titles) / float(Ncols)) +
                                       (len(titles) % Ncols > 0),
                                       cols=Ncols,
                                       subplot_titles=tuple(titles))
    for gp in range(len(titles)):
        pos1 = int(float(gp) / Ncols) + 1
        pos2 = gp - (pos1 - 1) * Ncols + 1

        title = titles[gp]

        if gp == 0:
            zprime = Z[Z_chose]
            bandwidth = estimate_bandwidth(zprime, quantile=0.2, n_samples=500)

            X_plot = np.linspace(-2, 8, 100)[:, np.newaxis]

            kde_plot = KernelDensity(kernel='gaussian',
                                     bandwidth=bandwidth).fit(zprime)
            log_dens = kde_plot.score_samples(X_plot)

            trace = go.Scatter(x=X_plot[:, 0],
                               y=np.exp(log_dens),
                               mode='lines',
                               fill='tozeroy',
                               line=dict(color='red', width=2))

            fig_subplots.append_trace(trace, pos1, pos2)

            if len(fig_dens_I):
                fig_subplots.append_trace(fig_dens_I[0], pos1, pos2)

        if gp == 1:
            feat_sum = np.sum(new_data, axis=0)
            trace = go.Bar(
                x=['PC {}'.format(x + 1) for x in range(new_data.shape[1])],
                y=feat_sum,
                marker=dict(color='rgb(0,0,205)'))
            fig_subplots.append_trace(trace, pos1, pos2)

            fig_subplots['layout']['yaxis' + str(gp + 1)].update(title='mean')

        if gp == 2:

            times_data = [
                list(Theta_record[x]['comb'][:, 0])
                for x in Theta_record.keys()
            ]
            times_data = np.array(times_data)
            times_av = np.mean(times_data, axis=0)
            times_av = [int(x) for x in times_av / 1000]
            times_av = ['{}k y'.format(x) for x in times_av]

            Xcomps = pca_obj.components_

            trace = go.Bar(x=times_av,
                           y=Xcomps[PC_select, :],
                           marker=dict(color='rgb(0,0,205)'))

            fig_subplots.append_trace(trace, pos1, pos2)

            fig_subplots['layout']['yaxis' +
                                   str(gp + 1)].update(title='eigen value')

    fig_subplots['layout'].update(height=height, width=width)

    layout = go.Layout(title=title)

    fig = go.Figure(data=fig_subplots, layout=layout)
    iplot(fig_subplots)
Example #37
0
    ),
)

pvr_ncr = go.Scatter(
    y = zomato_ncr_rated['Aggregate rating'],
    x = zomato_ncr_rated['Average Cost for two'],
    mode = 'markers',
    marker = dict(
        size = 6,
        color = zomato_ncr_rated['Aggregate rating'], #set color equal to a variable
        colorscale = 'Viridis',
        showscale = False
    ),
)

fig = tools.make_subplots(rows = 1, cols = 2,subplot_titles=('Non-NCR', 'NCR'))

fig['layout']['yaxis1'].update(title = 'Rating (Outside NCR) on scale of 5')
fig['layout']['yaxis2'].update(title = 'Rating (NCR) on scale of 5')

fig['layout']['xaxis1'].update(title = 'Avg Price for 2')
fig['layout']['xaxis2'].update(title = 'Avg Price for 2')

fig.append_trace(pvr_india, 1, 1)
fig.append_trace(pvr_ncr, 1, 2)

fig['layout'].update(height = 600, width = 800, title = 'Price Vs Rating')
pyo.iplot(fig, filename = 'simple-subplot-with-annotations')
#What are the most value for money restaurants in NCR?
zomato_ncr_votes = zomato_ncr_rated[zomato_ncr_rated.Votes >= 500]
zomato_ncr_rating = zomato_ncr_votes[zomato_ncr_votes['Aggregate rating'] > 3.9]
# Investigating https://github.com/plotly/plotly.js/issues/2609#issuecomment-386302768

import dash
import dash_core_components as dcc
import dash_html_components as html
from plotly import tools
import plotly.graph_objs as go


app = dash.Dash()


fig = tools.make_subplots(3, 1, shared_xaxes=True)
fig.append_trace(go.Scattergl({
      'x': df_1.index,
      'y': df1['column'].values,
      'mode': 'lines',
      'name': 'df1',
}), 1, 1)
  fig.append_trace(go.Scattergl({
      'x': df2.index,
      'y': df2['column'].values,
      'mode': 'lines',
      'name': 'df2',
  }), 2, 1)
  fig.append_trace(go.Scattergl({
      'x': df3.index,
      'y': df3['column'].values,
      'mode': 'lines',
      'name': 'df3',
  }), 3, 1)
# trace for each heatmap

trace1 = go.Heatmap(x=df1['DAY'],
                    y=df1['LST_TIME'],
                    z=df1['T_HR_AVG'].values.tolist(),
                    colorscale='Jet',zmin=5,zmax=40)
trace2 = go.Heatmap(x=df2['DAY'],
                    y=df2['LST_TIME'],
                    z=df2['T_HR_AVG'].values.tolist(),
                    colorscale='Jet',zmin=5,zmax=40)
trace3 = go.Heatmap(x=df3['DAY'],
                    y=df3['LST_TIME'],
                    z=df3['T_HR_AVG'].values.tolist(),
                    colorscale='Jet',zmin=5,zmax=40)

from plotly import tools

figx = tools.make_subplots(rows=1,cols=3, #rows and columns (horiztonal or vertical stacking)
                          subplot_titles=['Sitka AK', 'SB CA', 'Yuma AZ'],
                          shared_yaxes=False) #same time stamp for all 3 data, so we keep separte ones

# allotment of heatmap in rows and columns
figx.append_trace(trace1,1,1)
figx.append_trace(trace2,1,2)
figx.append_trace(trace3,1,3)

figx['layout'].update(title='Temps for 3 cities') # to add title for mulitple heatmaps in one plot

pyo.plot(figx,filename='Mutiple-Heatmap-Temps.html')

Example #40
0
    def plotCategoryAccuracies(self, trialAccuracies, trainSizes):
        """
    Shows the accuracy for the categories at a certain training size
    
    @param trialAccuracies    (dict)    A dictionary of dictionaries. For each
        train size, there is a dictionary that maps a category to a list of 
        accuracies for that category.
    
    @param trainSizes         (list)    Size of training set for each trial.
    """
        sizes = sorted(set(trainSizes))
        size_sqrt = math.sqrt(len(sizes))
        subplotDimension = int(math.ceil(size_sqrt))

        rows = subplotDimension
        cols = subplotDimension
        if len(sizes) <= subplotDimension * (subplotDimension - 1):
            rows -= 1

        fig = tls.make_subplots(rows=rows,
                                cols=cols,
                                shared_xaxes=True,
                                shared_yaxes=True,
                                print_grid=False)
        num_categories = 0
        for i, s in enumerate(sizes):
            # 1-indexed
            col = i % cols + 1
            row = (i - col + 1) / cols + 1
            classificationAccuracies = trialAccuracies[s]
            num_categories = max(num_categories,
                                 len(classificationAccuracies.keys()))

            x = []
            y = []
            std = []
            for label, acc in classificationAccuracies.iteritems():
                x.append(label)
                y.append(numpy.mean(acc))
                std.append(numpy.std(acc))

            trace = Scatter(x=x,
                            y=y,
                            name=s,
                            mode='markers',
                            error_y=ErrorY(type='data',
                                           symmetric=False,
                                           array=std,
                                           arrayminus=std,
                                           visible=True))

            fig.append_trace(trace, row, col)

        fig["layout"]["title"] = "Accuracies for category by training size"
        half_way_cols = int(math.ceil(cols / 2.0))
        half_way_rows = int(math.ceil(rows / 2.0))
        fig["layout"]["xaxis{}".format(
            half_way_cols)]["title"] = "Category Label"
        fig["layout"]["yaxis{}".format(half_way_rows)]["title"] = "Accuracy"
        for i in xrange(1, cols + 1):
            fig["layout"]["xaxis{}".format(i)]["tickangle"] = -45
            fig["layout"]["xaxis{}".format(i)]["nticks"] = num_categories * 2
            if i <= rows:
                fig["layout"]["yaxis{}".format(i)]["range"] = [-.1, 1.1]
        fig["layout"]["margin"] = {"b": 120}

        plot_url = py.plot(fig)
        print "Category Accuracies URL: ", plot_url
def Assembly(fileNumber, atomLocs1, pic1Num, **standardAssemblyArgs):
    """
    This function checks the efficiency of generating a picture;
    I.e. finding atoms at multiple locations at the same time.
    """
    (atomLocs1, atomLocs2, key, thresholds, pic1Data, pic2Data, fit, ensembleStats, avgPic, atomCounts, keyName,
     indvStatistics, lossAvg,
     lossErr) = standardAssemblyAnalysis(fileNumber, atomLocs1, pic1Num, **standardAssemblyArgs)
    # ######################## Plotting
    # get the colors for the plot.
    colors, colors2 = getColors(len(atomLocs1) + 1)
    mainPlot = [go.Scatter(x=key, y=ensembleStats['avg'], mode="markers", name='Ensemble',
                           error_y={"type": 'data', "array": ensembleStats['err'], 'color': '#000000'},
                           marker={'color': '#000000'}, legendgroup='ensemble'),
                go.Scatter(x=key, y=lossAvg, mode="markers", name='Loss',
                           error_y={"type": 'data', "array": lossErr, 'color': '#000000'},
                           marker={'color': '#000000', 'symbol': 'x', 'size': 10}, legendgroup='ensemble')
                ]
    # loss is the loss %, but for these plots it's the % of atoms lost regardless location. i.e. it looks at
    # number in first picture & number in second picture.
    for atomStats, loc, color in zip(indvStatistics, atomLocs1, colors):
        mainPlot.append(go.Scatter(x=key, y=atomStats['avg'], mode="markers", name=str(loc),
                                   error_y={"type": 'data', "array": atomStats['err'], 'color': color},
                                   marker={'color': color}, legendgroup=str(loc)))

    countsHist, countsFig, loadingPlot, avgFig = [[] for _ in range(4)]
    avgFig.append(go.Heatmap(z=avgPic, colorscale='Viridis', colorbar=go.heatmap.ColorBar(x=1, y=0.15, len=0.3)))

    bins = []
    for data in pic1Data:
        b, _ = np.histogram(data, bins=100)
        bins.append(b)
    maxHistHeight = max(arr(bins).flatten())
    for data, loc, color, threshold in zip(pic1Data, atomLocs1, colors, thresholds):
        countsHist.append(go.Histogram(y=data, nbinsy=100, legendgroup=str(loc), showlegend=False, opacity=0.3,
                                       xbins=dict(start=min(pic1Data.flatten()), end=max(pic1Data.flatten())),
                                       marker=dict(color=color)))
        countsHist.append(go.Scatter(x=[0, maxHistHeight], y=[threshold, threshold],
                                     showlegend=False, mode='lines', line={'color': color, 'width': 1},
                                     hoverinfo='none', legendgroup=str(loc)))
        # countsFig.append(go.Scatter(y=data, mode='markers', marker={'color':color, 'size':1},
        #                            legendgroup=str(loc), showlegend=False))
        # countsFig.append(go.Scatter(x=[0,len(pic1Data[0].flatten())], y=[threshold,threshold], showlegend=False,
        #                             mode='lines', line={'color':color, 'width':1}, hoverinfo='none',
        #                             legendgroup=str(loc)))
        # loadingPlot.append(go.Scatter(x=key, y=load, mode="markers", name=str(loc),
        #                              marker ={'color' : color}, legendgroup=str(loc), showlegend=False))
        avgFig.append(go.Scatter(x=[loc[1]], y=[loc[0]], mode='markers', hoverinfo='none',
                                 showlegend=False, legendgroup=str(loc), marker={'size': 5, 'color': '#FF0000'}))
    """
    """
    fig = make_subplots(rows=3, cols=12, print_grid=False, horizontal_spacing=0, vertical_spacing=0,
                        specs=[[{'rowspan': 3, 'colspan': 9}, None, None, None, None, None, None, None, None,
                                {'colspan': 2}, None, {}],
                               [None, None, None, None, None, None, None, None, None, {'colspan': 3}, None, None],
                               [None, None, None, None, None, None, None, None, None, {'colspan': 3}, None, None]])
    for mainLine in mainPlot:
        fig.add_trace(mainLine, 1, 1)
    for avgPart in avgFig:
        fig.add_trace(avgPart, 3, 10)
    # for load in loadingPlot:
    #    fig.add_trace(load, 2, 10)
    for counts in countsFig:
        fig.add_trace(counts, 1, 10)
    for hist in countsHist:
        fig.add_trace(hist, 1, 12)
    fig['layout'].update(barmode='overlay')
    fig['layout']['yaxis1'].update(title="Ensemble %", range=[0, 1])
    fig['layout']['xaxis1'].update(title=str(keyName))
    fig['layout']['yaxis2'].update(title="Count", range=[min(pic1Data.flatten()), max(pic1Data.flatten())])
    fig['layout']['xaxis2'].update(range=[0, len(pic1Data[0].flatten())])
    fig['layout']['yaxis3'].update(range=[min(pic1Data.flatten()), max(pic1Data.flatten())], showticklabels=False)
    fig['layout']['xaxis3'].update(showticklabels=False)
    fig['layout']['yaxis4'].update(title="Loading %", range=[0, 1])
    fig['layout']['yaxis5'].update(title="Average Image")
    iplot(fig)
    return key, fig
def make_snp_plots(samples_dict, snp_name="", jbrouwse_url=None):
    height = 1 * 350
    width = 2 * 350
    # if there are multiple plots the space in between has to be acounted for.
    width += width * 0.2
    #height += height*0.3
    # and the borders
    height += 180
    width += 160
    fig = tools.make_subplots(rows=1,
                              cols=2,
                              subplot_titles=('allele depth',
                                              'allele 2 percent'))
    fig['layout'].update(
        title="sequence variant {}.".format(snp_name),
        width=width,
        height=height,
        hovermode='closest',
        showlegend=False,
        annotations=[
            dict(
                x=0.48,
                y=1,
                xref='paper',
                yref='paper',
                yanchor='middle',
                xanchor='right',
                text='lines do not reflect SNP calling rules',
                font=go.Font(size=8),
                showarrow=False,
            ),
            dict(
                x=1,
                y=1,
                xref='paper',
                yref='paper',
                yanchor='middle',
                xanchor='right',
                text='lines do not reflect SNP calling rules',
                font=go.Font(size=8),
                showarrow=False,
            )
        ],
        shapes=[
            {
                'type': 'line',
                'x0': 0,
                'x1': 10,
                'xref': "x",
                'y0': 10,
                'y1': 0,
                'yref': "y",
                'line': {
                    'color': 'yellow',
                    'width': 2,
                },
            },
            {
                'type': 'line',
                'x0': 9,
                'x1': 10000,
                'xref': "x",
                'y0': 1,
                'y1': 1000,
                'yref': "y",
                'line': {
                    'color': 'red',
                    'width': 2,
                },
            },
            {
                'type': 'line',
                'x0': 1000,
                'x1': 1,
                'xref': "x",
                'y0': 10000,
                'y1': 9,
                'yref': "y",
                'line': {
                    'color': 'blue',
                    'width': 2,
                }
            },
            {
                'type': 'line',
                'x0': 5,
                'x1': 10000,
                'xref': "x",
                'y0': 5,
                'y1': 10000,
                'yref': "y",
                'line': {
                    'color': 'purple',
                    'width': 2,
                }
            },
            {
                'type': 'line',
                'x0': 6.6,
                'x1': 10000,
                'xref': "x",
                'y0': 3.3,
                'y1': 5000,
                'yref': "y",
                'line': {
                    'color': 'black',
                    'width': 2,
                }
            },
            {
                'type': 'line',
                'x0': 3.3,
                'x1': 5000,
                'xref': "x",
                'y0': 6.6,
                'y1': 10000,
                'yref': "y",
                'line': {
                    'color': 'black',
                    'width': 2,
                }
            },
            # and now the percentage plot
            {
                'type': 'line',
                'x0': 10,
                'x1': 10,
                'xref': "x2",
                'y0': 0,
                'y1': 100,
                'yref': "y2",
                'line': {
                    'color': 'yellow',
                    'width': 2,
                }
            },
            {
                'type': 'line',
                'x0': 0,
                'x1': 10000,
                'xref': "x2",
                'y0': 10,
                'y1': 10,
                'yref': "y2",
                'line': {
                    'color': 'red',
                    'width': 2,
                }
            },
            {
                'type': 'line',
                'x0': 0,
                'x1': 10000,
                'xref': "x2",
                'y0': 90,
                'y1': 90,
                'yref': "y2",
                'line': {
                    'color': 'blue',
                    'width': 2,
                }
            },
            {
                'type': 'line',
                'x0': 0,
                'x1': 10000,
                'xref': "x2",
                'y0': 66.6,
                'y1': 66.6,
                'yref': "y2",
                'line': {
                    'color': 'black',
                    'width': 2,
                }
            },
            {
                'type': 'line',
                'x0': 0,
                'x1': 10000,
                'xref': "x2",
                'y0': 33.3,
                'y1': 33.3,
                'yref': "y2",
                'line': {
                    'color': 'black',
                    'width': 2,
                }
            }
        ],
    )
    #make both axes the same scale
    max_depth = max([sum([v['x'], v['y']]) for v in samples_dict.values()])
    fig['layout']['xaxis1'].update(range=[max_depth * -0.05, max_depth * 1.05],
                                   title='Reference depth (vcf)')
    fig['layout']['yaxis1'].update(range=[max_depth * -0.05, max_depth * 1.05],
                                   title='Alternate depth (vcf)')
    fig['layout']['xaxis2'].update(range=[max_depth * -0.05, max_depth * 1.05],
                                   title='depth (vcf)')
    fig['layout']['yaxis2'].update(tickvals=[0, 25, 50, 75, 100],
                                   ticktext=['0%', '', '50%', '', '100%'],
                                   title='percent alternate allele')
    # making the traces themselves
    fig.append_trace(
        make_allele_depth_trace(samples_dict, jbrouwse_url=jbrouwse_url), 1, 1)
    fig.append_trace(
        make_allele_percent_trace(samples_dict, jbrouwse_url=jbrouwse_url), 1,
        2)
    locus_div = pyoff.plot(fig, include_plotlyjs=False, output_type='div')
    return locus_div
Example #43
0
    },  #set marker details
    name="Average Price",
    text=etsy['sellername'])  #enter text

best_seller = go.Scatter(  # initialize scatter object
    x=etsy['bestseller2'],
    y=etsy['sales'],  #enter x,y
    mode='markers',
    marker={
        'color': 'green',
        'size': 6
    },  #set marker details
    name="Best Sellers",
    text=etsy['sellername'])  #enter text

fig2 = tools.make_subplots(rows=3, cols=1)  #Create Layout

fig2.append_trace(comments, 1, 1)
fig2.append_trace(price, 2, 1)
fig2.append_trace(best_seller, 3, 1)  #Append traces together

fig2['layout'].update(height=600, width=600, title='Etsy Scatters')
plot(fig2)  #Plot the scatters

#Compare histogram of comments and ratings; proving ratings are unreliable
import plotly.figure_factory as ff

x = np.log(etsyratings['comments']
           )  #using log so outliers are taken care of for visualization
x = x.replace([-np.inf], 0)  #Because some log values go to negative infinity
Example #44
0
first_name = first_name.sort_values('counts', ascending=False).head(20)
df = go.Bar(x=first_name['counts'],
            y=first_name['first'],
            orientation='h',
            name='First Name')

#last names
first_name = u_name.groupby('last',
                            as_index=False).size().reset_index(name='counts')
first_name = first_name.sort_values('counts', ascending=False).head(20)
df1 = go.Bar(x=first_name['counts'],
             y=first_name['last'],
             orientation='h',
             name='Last Name')
fig = tools.make_subplots(rows=1,
                          cols=2,
                          subplot_titles=('first Name', 'Last Name'))
fig.append_trace(df, 1, 1)
fig.append_trace(df1, 1, 2)
fig['layout'].update(height=400,
                     width=1000,
                     title='First and Last Names of Fake Accounts')
plot(fig, filename='basic line')

#remove the special characters
des = fake__users.description.copy().astype(str)
des = des.str.replace('[^\w\s]', '')
des = des.str.replace('[\\r|\\n\\t|_]', ' ')
des = des.str.strip()

tweets_des = tweets_details.copy()
Example #45
0
def plot_rec_InfSites(point_up,
                      root_lib,
                      funk,
                      titles,
                      range_theta,
                      height=500,
                      width=900):
    Ncols = 1

    fig_subplots = tools.make_subplots(rows=int(len(titles) / float(Ncols)) +
                                       (len(titles) % Ncols > 0),
                                       cols=Ncols,
                                       subplot_titles=tuple(titles))

    for gp in range(len(titles)):

        pos1 = int(float(gp) / Ncols) + 1
        pos2 = gp - (pos1 - 1) * Ncols + 1

        title = titles[gp]

        Inf_sites_est = []
        there = []

        runUp_use = funk[gp]

        t1 = time.time()

        for x in range_theta:

            ## run up the tree.
            Browse = runUp_use(point_up,
                               root_lib,
                               layer=0,
                               start=0,
                               Theta=x,
                               prob_vec=[])
            probe_rec = sum(Browse)

            Inf_sites_est.append(probe_rec)
            there.append(x)

        t2 = time.time()
        tscale = 's'
        tpass = t2 - t1

        if tpass > 600:
            tpass = tpass / 60
            tscale = 'm'

        tpass = round(tpass, 3)

        trace1 = go.Scatter(y=Inf_sites_est,
                            x=there,
                            mode='markers',
                            name=titles[gp])

        fig_subplots.append_trace(trace1, pos1, pos2)

        fig_subplots['layout']['yaxis' + str(gp + 1)].update(title='P')
        fig_subplots['layout']['yaxis' + str(gp + 1)].update(
            range=[0, max(Inf_sites_est) + max(Inf_sites_est) / 10])
        fig_subplots['layout']['xaxis' + str(gp + 1)].update(
            title='theta - ts {} {}'.format(tpass, tscale))

    layout = go.Layout(title=title, )

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

    fig['layout'].update(height=height, width=width)

    iplot(fig)
def bra(coy):

    my_path = os.path.abspath(os.path.dirname(__file__))
    path = os.path.join(my_path, "../input_fields.csv")

    input_fields = pd.read_csv(path)

    short = input_fields["short_name"]
    codes = input_fields["code_or_ticker"]

    my_path = os.path.abspath(os.path.dirname('__file__'))
    path_out = my_path + "/data/similarweb/"
    fi_dict = pickle.load(open(path_out + "website.p", "rb"))

    import plotly.plotly as py
    import plotly.graph_objs as go
    from plotly import tools
    import plotly.plotly as py
    import plotly.graph_objs as go
    from plotly import tools

    perc_p_s = fi_dict["social"][coy + "_perc"]
    webs_p_s = fi_dict["social"][coy + "_webs"]

    perc_s = fi_dict["top"][coy + "_perc"]
    webs_s = fi_dict["top"][coy + "_webs"]

    trace3 = go.Bar(x=perc_p_s,
                    y=webs_p_s,
                    text="%",
                    name="Social",
                    orientation='h',
                    marker=dict(
                        color='rgb(158,202,225)',
                        line=dict(color='rgb(8,48,107)', width=3),
                    ))

    trace4 = go.Bar(x=perc_s,
                    y=webs_s,
                    text="%",
                    name="Other",
                    orientation='h',
                    marker=dict(color='rgba(208, 105, 80, 0.6)',
                                line=dict(color='rgba(208, 105, 80, 1.0)',
                                          width=3)))

    perc = fi_dict["top_org"][coy + "_perc"]
    webs = fi_dict["top_org"][coy + "_webs"]
    perc_p = fi_dict["top_paid"][coy + "_perc"]
    webs_p = fi_dict["top_paid"][coy + "_webs"]

    trace1 = go.Bar(x=perc,
                    y=webs,
                    text="%",
                    name="Organic",
                    orientation='h',
                    marker=dict(
                        color='rgb(158,202,225)',
                        line=dict(color='rgb(8,48,107)', width=3),
                    ))

    trace2 = go.Bar(x=perc_p,
                    y=webs_p,
                    text="%",
                    name="Paid",
                    orientation='h',
                    marker=dict(color='rgba(208, 105, 80, 0.6)',
                                line=dict(color='rgba(208, 105, 80, 1.0)',
                                          width=3)))

    fig = tools.make_subplots(rows=2, cols=2)

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

    fig["layout"].update(
        height=300,
        margin=go.Margin(
            l=110,
            t=20,
            b=30,
        ),
        xaxis1=dict(
            showgrid=False,
            zeroline=False,
            showline=False,
        ),
        yaxis1=dict(showgrid=False,
                    zeroline=False,
                    showline=False,
                    tickangle=20),
        xaxis2=dict(
            showgrid=False,
            zeroline=False,
            showline=False,
        ),
        yaxis2=dict(showgrid=False,
                    zeroline=False,
                    showline=False,
                    tickangle=20),
        xaxis3=dict(
            showgrid=False,
            zeroline=False,
            showline=False,
        ),
        yaxis3=dict(showgrid=False,
                    zeroline=False,
                    showline=False,
                    tickangle=20),
        xaxis4=dict(
            showgrid=False,
            zeroline=False,
            showline=False,
        ),
        yaxis4=dict(showgrid=False,
                    zeroline=False,
                    showline=False,
                    tickangle=20),
    )

    def make_dash_table(df):
        ''' Return a dash definitio of an HTML table for a Pandas dataframe '''
        table = []
        for index, row in df.iterrows():
            html_row = []
            for i in range(len(row)):
                html_row.append(html.Td([row[i]]))
            table.append(html.Tr(html_row))
        return table

    layout = html.Div([
        html.Br([]),
        html.Div(
            [
                dcc.Dropdown(id='selector_dd',
                             options=[{
                                 'label': r,
                                 'value': v
                             } for r, v in zip(short, codes)],
                             value=coy,
                             clearable=False,
                             className="dropper")
            ],
            style={
                'background-color': "white",
                'padding-right': '0.3cm',
                'color': 'rgb(217, 224, 236)',
                'width': '28%'
            }),
        html.H6(["Visit Stats"],
                className="gs-header gs-text-header padded",
                style={
                    'margin-top': "3mm",
                    'margin-bottom': "3mm"
                }),
        html.Div([
            html.Iframe(
                style={
                    'width': "100%",
                    'height': '420',
                    'right': '0',
                    'position': 'relative',
                    'border': '0',
                    'margin-left': '-0.2cm'
                },
                src=
                "https://public.tableau.com/views/website_cross/Sheet4?:embed=y&:display_count=no&:toolbar=no?:embed=y&:showVizHome=no&:hoswidtt_url=https%3A%2F%2Fpublic.tableau.com%2F&:tabs=no&:toolbar=no&:animate_transition=yes&:display_static_image=no&:display_spinner=no&:display_overlay=no&:display_count=no#3",
                width="645",
                height="420",
                seamless="seamless")
        ],
                 style={
                     "width": "100%",
                     "height": "190",
                     "overflow": "hidden"
                 }),
        html.Br([]),
        html.H6(["Inbound Links"],
                className="gs-header gs-text-header padded",
                style={
                    'margin-top': "3mm",
                    'margin-bottom': "3mm"
                }),
        dcc.Graph(id='fig_social',
                  figure=fig,
                  config={'displayModeBar': False}),
        html.H6(["Website Stats"],
                className="gs-header gs-text-header padded",
                style={
                    'margin-top': "3mm",
                    'margin-bottom': "3mm"
                }),
        html.Div(
            [html.Table(make_dash_table(fi_dict["key_metrics"]),
                        id="df_rat")]),
    ])

    return layout, fig
Example #47
0
def all():
    df_action = movie
    df_action.replace('  ', 'Not Available', inplace=True)
    info = []
    info.append(len(df_action))
    info.append(df_action['Cast 1'].value_counts().index[0])
    info.append(df_action['Director 1'].value_counts().index[0])
    info.append(df_action['Studio'].value_counts().index[0])
    info.append([
        df_action['scores'].max(),
        df_action[df_action['scores'] == df_action['scores'].max()]
    ])
    info.append([
        df_action['scores'].min(),
        df_action[df_action['scores'] == df_action['scores'].min()]
    ])
    fig = tools.make_subplots(
        rows=2,
        cols=2,
        subplot_titles=('Most Popular Actors', 'Most popular director',
                        'Most popular studio', 'Scores Distribution'))
    actor = go.Bar(y=df_action['Cast 1'].value_counts()[:10],
                   x=df_action['Cast 1'].value_counts()[:10].index[:10],
                   text=df_action['Cast 1'].value_counts()[:10],
                   textposition='auto',
                   marker=dict(
                       color='rgb(234,125,92)',
                       line=dict(color='rgb(8,48,107)', width=1.5),
                   ),
                   name='Actor',
                   opacity=0.6)
    director = go.Bar(y=df_action['Director 1'].value_counts()[:10],
                      x=df_action['Director 1'].value_counts()[:10].index[:10],
                      text=df_action['Director 1'].value_counts()[:10],
                      textposition='auto',
                      marker=dict(
                          color='rgb(119,174,103)',
                          line=dict(color='rgb(8,48,107)', width=1.5),
                      ),
                      name='Director',
                      opacity=0.6)
    studio = go.Bar(y=df_action['Studio'].value_counts()[:10],
                    x=df_action['Studio'].value_counts()[:10].index[:10],
                    text=df_action['Studio'].value_counts()[:10],
                    textposition='auto',
                    marker=dict(
                        color='rgb(204,106,118)',
                        line=dict(color='rgb(8,48,107)', width=1.5),
                    ),
                    name='Studio',
                    opacity=0.6)
    score = go.Histogram(x=df_action['scores'],
                         xbins=dict(start=0, end=10, size=0.5),
                         marker=dict(
                             color='rgb(103,162,178)',
                             line=dict(color='rgb(8,48,107)', width=1.5),
                         ),
                         name='Score',
                         opacity=0.6)

    fig.append_trace(actor, 1, 1)
    fig.append_trace(director, 1, 2)
    fig.append_trace(studio, 2, 1)
    fig.append_trace(score, 2, 2)
    fig['layout'].update(height=1600,
                         width=1000,
                         title='Information about Comedy movie')
    my_plot = plot(fig, output_type='div')
    return render_template('Overall.html', info=info, mydiv=Markup(my_plot))
    repeated_cards = ([card.drawn
                       for card in cards] + [card.drawn for card in discard] +
                      [card.drawn for card in removed])
    return (ii, tribe_events, repeated_cards, tribes, tribes_half_time,
            discard_pile_length, stats, count(removed, Cards.REMOVE_STOP),
            hist(start_cards), card_stats(start_cards))


if __name__ == '__main__':
    repetitions = 10000
    fig = tools.make_subplots(rows=3,
                              cols=3,
                              subplot_titles=[
                                  'tribe_events', 'mean number of cards',
                                  'num tribes', 'mean number of cards',
                                  'repeated_cards', 'num tribes half time',
                                  'number of cards discard pile',
                                  'repeated_cards', 'num_tribe_events'
                              ])
    summary_fig2 = tools.make_subplots(
        rows=3, cols=3, subplot_titles=['cards drawn', 'card_stats'])
    fig2 = tools.make_subplots(rows=1, cols=5)
    fig3 = tools.make_subplots(rows=5, cols=5)
    fig4 = tools.make_subplots(rows=5, cols=5)

    tribe_attacks_list = []
    iis = []
    xis = []
    repeated_cards_list = []
    tribes_list = []
trace4 = go.Scatter(x=times,
                    y=age3,
                    line={'width': 2},
                    marker={
                        'color': colors[4],
                        'symbol': 100,
                        'size': "10"
                    },
                    mode="markers+lines",
                    text=['65+ yrs old'],
                    name='65+ yrs')

fig = tools.make_subplots(rows=1,
                          cols=2,
                          specs=[[{}, {}]],
                          shared_xaxes=True,
                          shared_yaxes=False,
                          vertical_spacing=0.001)

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

number = [age0[-1], age1[-1], age2[-1], age3[-1]]
percent = [round(100.0 * (float(x) / float(total[-1])), 1) for x in number]
age_bracket = ['18-24 yrs', '25-44 yrs', '45-65 yrs', '65+ yrs']

trace5 = go.Bar(
    x=age_bracket,
def Transfer(fileNumber, atomLocs1, atomLocs2, show=True, fitModules=[None], showCounts=False, showGenerationData=False,
             histBins=150, showFitDetails=False, savePlotlyFig=False, leanPlot=False, **standardTransferArgs):
    """
    Standard data analysis package for looking at survival rates throughout an experiment.
    """
    avgColor = '#FFFFFF'
    res = TransferAnalysis.standardTransferAnalysis(fileNumber, atomLocs1, atomLocs2, fitModules=fitModules,
                                   **standardTransferArgs)
    (atomLocs1, atomLocs2, transferData, transferErrs, initPopulation, pic1Data, keyName, key,
     repetitions, initThresholds, fits, avgTransferData, avgTransferErr, avgFit, avgPics, otherDimValues,
     locsList, genAvgs, genErrs, tt, transVarAvg, transVarErr, initAtomImages, transAtomImages,
     pic2Data, transThresholds, fitModules, transThresholdSame, basicInfoStr) = res
    if not show:
        return key, transferData, transferErrs, initPopulation
    
    # get the colors for the plots.
    pltColors, pltColors2 = getColors(len(locsList) + 1)
    scanType = "S." if atomLocs1 == atomLocs2 else "T."
    if scanType == "S.":
        legends = [r"%d,%d " % (loc1[0], loc1[1]) + (scanType + "% = " + str(errString(d[0], e[0])) if len(d) == 1
                   else "") for loc1, d, e in zip(locsList, transferData, transferErrs)]

    elif otherDimValues[0] is not None:
        legends = [r"%d,%d>%d,%d @%d " % (loc1[0], loc1[1], loc2[0], loc2[1], other) +
                   (scanType + "%=" + str(errString(d[0]), e[0]) if len(d) == 1 else "")
                   for loc1, loc2, d, e, other in zip(locsList, locsList, transferData, transferErrs,
                                                      otherDimValues)]
    else:
        legends = [r"%d,%d>%d,%d " % (loc1[0], loc1[1], loc2[0], loc2[1]) +
                   (scanType + "%=" + errString(d[0], e[0]) if len(d) == 1 else "")
                   for loc1, loc2, d, e in zip(locsList, locsList, transferData, transferErrs)]
    transferErrs = list(transferErrs)
    # Make the plots
    alphaVal = 0.5
    mainPlot, countsHist, countsFig, initPopPlot = [[] for _ in range(4)]
    avgFigs = [[] for _ in range(2)]
    avgFigs[0].append(go.Heatmap(z=avgPics[0], colorscale='Viridis'))
    avgFigs[1].append(go.Heatmap(z=avgPics[1], colorscale='Viridis'))
    fitCharacters = []
    
    if fitModules[0] is not None and showFitDetails:
        frames = getFitsDataFrame(fits, fitModules, avgFit)
        for frame in frames:
            display(frame)
    for data, err, loc, color, legend, fitData, gen, genErr, module in zip(transferData, transferErrs, locsList, pltColors,
                                                      legends, fits, genAvgs, genErrs, fitModules):
        mainPlot.append(go.Scatter(x=key, y=data, opacity=alphaVal, mode="markers", name=legend,
                                   error_y={"type": 'data', 'symmetric':False, "array": [e[1] for e in err], 
                                            'arrayminus':[e[0] for e in err],
                                            'color': color, 'visible':True},
                                   marker={'color': color}, legendgroup=legend))
        if showGenerationData:
            mainPlot.append(go.Scatter(x=key, y=gen, opacity=alphaVal, mode="markers", name=legend,
                                       error_y={"type": 'data', "array": genErr, 'color': color, 'visible':True},
                                       marker={'color': color, 'symbol': 'star'}, legendgroup=legend, showlegend=False))
        if fitModules[0] is not None:
            if fitData['vals'] is None:
                print(loc, 'Fit Failed!')
                continue
            if showFitDetails:
                fitCharacters.append(module.fitCharacter(fitData['vals']))
                mainPlot.append(go.Scatter(x=[0], y=[0.5], mode='text', text=[module.getFitCharacterString(fitData['vals'])], 
                                           textposition="top center", legendgroup=legend, showlegend=False))
                
            mainPlot.append(go.Scatter(x=fitData['x'], y=fitData['nom'], line={'color': color},
                                       legendgroup=legend, showlegend=False, opacity=alphaVal))
            if fitData['std'] is not None:
                rgbcolor = color.strip('#')
                rgbColor = tuple(int(rgbcolor[i:i+2], 16) for i in (0, 2, 4))
                mainPlot.append(go.Scatter(x=fitData['x'], y=fitData['nom'] + fitData['std'],
                                           opacity=alphaVal / 2, line={'color': color},
                                           legendgroup=legend, showlegend=False, hoverinfo='none'))
                mainPlot.append(go.Scatter(x=fitData['x'], y=fitData['nom'] - fitData['std'],
                                           opacity=alphaVal / 2, line={'color': color},
                                           legendgroup=legend, fill='tonexty', showlegend=False,
                                           hoverinfo='none', 
                                           fillcolor='rgba('+str(rgbColor[0])+', '+str(rgbColor[1])+', '+str(rgbColor[2])+', '+str(alphaVal/2) + ')'))
    transferPic = np.zeros(avgPics[0].shape)
    for i, loc in enumerate(atomLocs1):
        transferPic[loc[0], loc[1]] = np.mean(transferData[i])
    if fitModules[0] is not None and showFitDetails:
        print('Fit Character:', fitCharacters)
        print('Average Fit Character:', np.mean(fitCharacters), np.median(fitCharacters))
        fitCharacterPic, vmin, vmax = ah.genAvgDiscrepancyImage(fitCharacters, avgPics[0].shape, atomLocs1)
        fitCharacterFig = [go.Heatmap(z=fitCharacterPic, colorscale='Viridis', colorbar=go.heatmap.ColorBar(x=1, y=0.15, len=0.3))]
        layout = go.Layout(title='Fit-Character Pic')
        iplot(go.Figure(data=fitCharacterFig, layout=layout))
    maxHeight = np.max(arr([np.histogram(data.flatten(), bins=histBins)[0] for data in pic1Data]).flatten())
    for data, pop, loc1, loc2, color, threshold, legend in zip(pic1Data, initPopulation, atomLocs1, atomLocs2, pltColors,
                                                         initThresholds, legends):
        countsHist.append(go.Histogram(x=data, nbinsx=histBins, legendgroup=legend, showlegend=False, opacity=alphaVal/2,
                                       marker=dict(color=color)))
        countsHist.append(go.Scatter(y=[0, maxHeight], x=[threshold[0].t, threshold[0].t], showlegend=False,
                                     mode='lines', line={'color': color, 'width': 1}, hoverinfo='none',
                                     legendgroup=legend))
        initPopPlot.append(go.Scatter(x=key, y=pop, mode="markers", name=str(loc1),
                                      marker={'color': color}, legendgroup=legend, showlegend=False,
                                      opacity=alphaVal))
        
        avgFigs[0].append(go.Scatter(x=[loc1[1]], y=[loc1[0]], mode='markers', hoverinfo='none',
                                 showlegend=False, legendgroup=legend, marker={'size': 2, 'color': '#FF0000'}))
        avgFigs[1].append(go.Scatter(x=[loc2[1]], y=[loc2[0]], mode='markers', hoverinfo='none',
                                 showlegend=False, legendgroup=legend, marker={'size': 2, 'color': '#FF0000'}))
    # average stuff
    mainPlot.append(go.Scatter(x=key, y=avgTransferData, mode="markers", name='avg',
                               
                               error_y={"type": 'data', 'color': avgColor, 'symmetric':False, 
                                        "array": [e[1] for e in avgTransferErr], 'arrayminus':[e[0] for e in avgTransferErr]},
                               marker={'color': avgColor}, legendgroup='avg'))
    if fitModules[0] is not None:
        mainPlot.append(go.Scatter(x=avgFit['x'], y=avgFit['nom'], line={'color': avgColor},
                                   legendgroup='avg', showlegend=False, opacity=alphaVal))
        mainPlot.append(go.Scatter(x=[0], y=[0.5], mode='text', text=[fitModules[-1].getFitCharacterString(avgFit['vals'])], 
                                   textposition="top center", legendgroup='avg', showlegend=False))
        if avgFit['std'] is not None:
            mainPlot.append(go.Scatter(x=avgFit['x'], y=avgFit['nom'] + avgFit['std'],
                                       opacity=alphaVal / 2, line={'color': avgColor},
                                       legendgroup='avg', showlegend=False, hoverinfo='none'))
            mainPlot.append(go.Scatter(x=avgFit['x'], y=avgFit['nom'] - avgFit['std'],
                                       opacity=alphaVal / 2, line={'color': avgColor},
                                       legendgroup='avg', fill='tonexty', showlegend=False,
                                       hoverinfo='none'))
    if showCounts:
        avgOnly = True
        if avgOnly:
            countsFig.append(go.Scatter(y=arr(transpose(pic1Data)).flatten(), mode='markers',
                                        marker={'color': avgColor, 'size': 1}, legendgroup='avg', showlegend=False))
        countsHist.append(go.Histogram(x=atomCounts.flatten(), nbinsx=200, legendgroup='avg',
                                       showlegend=False, opacity=0.1, marker=dict(color=avgColor),
                                       xbins=dict(start=min(pic1Data.flatten()), end=max(pic1Data.flatten()))))
        d, _ = np.histogram(atomCounts.flatten(), bins=200)
        countsHist.append(go.Scatter(x=[0, max(d) / 2], y=[np.mean([thresh.t for thresh in initThresholds]), np.mean([thresh.t for thresh in initThresholds])],
                                     showlegend=False, mode='lines', line={'color': avgColor, 'width': 1},
                                     hoverinfo='none', legendgroup='avg'))
        # format and arrange plots. large grid is mostly to precisely place the histogram.
        n = None
        r = 'rowspan'
        c = 'colspan'
        fig = make_subplots(
            rows=3, cols=24, print_grid=False, horizontal_spacing=0.03, vertical_spacing=0.05,
            specs=[[{r: 3, c: 18}, n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, {c: 4}, n, n, n,      {}, n],
                   [n,             n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, {c: 6}, n, n, n,      n,  n],
                   [n,             n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, {c: 3}, n, n, {c: 3}, n,  n]])
        traceSets = [mainPlot, avgFigs[0], avgFigs[1], initPopPlot, countsFig, countsHist]
        
        for mainLine in mainPlot:
            fig.add_trace(mainLine, 1, 1)
        for avgPart in avgFigs[0]:
            fig.add_trace(avgPart, 3, 19)
        for avgPart in avgFigs[1]:
            fig.add_trace(avgPart, 3, 22)
        for pop in initPopPlot:
            fig.add_trace(pop, 2, 19)
        for counts in countsFig:
            if not leanPlot:
                fig.add_trace(counts, 1, 19)
        for hist in countsHist:
            if not leanPlot:
                fig.add_trace(hist, 1, 23)
        fig['layout']['yaxis2'].update(title="Count", range=[min(pic1Data.flatten()), max(pic1Data.flatten())])
        fig['layout']['xaxis2'].update(range=[0, len(pic1Data[0].flatten())])
        fig['layout']['yaxis3'].update(range=[min(pic1Data.flatten()), max(pic1Data.flatten())],
                                       showticklabels=False)
        fig['layout']['xaxis3'].update(showticklabels=False)
        fig['layout']['yaxis4'].update(title="Initial Population %", range=[0, 1])
        fig['layout']['yaxis5'].update(title="Average Image")
    else:
        # format and arrange plots. large grid is mostly to precisely place the histogram.
        n = None
        r = 'rowspan'
        c = 'colspan'
        fig = make_subplots(
            rows=3, cols=24, print_grid=False, horizontal_spacing=0.03, vertical_spacing=0.05,
            specs=[[{r: 3, c: 18}, n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, {c: 4}, n, n, n,      {}, n],
                   [n,             n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, {c: 6}, n, n, n,      n,  n],
                   [n,             n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, {c: 3}, n, n, {c: 3}, n,  n]])
        for mainLine in mainPlot:
            fig.add_trace(mainLine, 1, 1)
        for avgPart in avgFigs[0]:
            fig.add_trace(avgPart, 3, 19)
        for avgPart in avgFigs[1]:
            fig.add_trace(avgPart, 3, 22)
        for pop in initPopPlot:
            fig.add_trace(pop, 2, 19)
        for hist in countsHist:
            if not leanPlot:
                fig.add_trace(hist, 1, 19)
        fig['layout']['yaxis3'].update(title="Initial Population %", range=[0, 1])
        fig['layout']['yaxis5'].update(title="Average Image")
    fig['layout'].update(barmode='overlay', plot_bgcolor="rgb(10,10,10)", paper_bgcolor="rgb(0,0,0)")
    fig['layout']['yaxis1'].update(title=scanType + " %", range=[0, 1])
    fig['layout']['xaxis1'].update(title=str(keyName))
    iplot(fig)
    if savePlotlyFig:
        plotlyplot(fig, filename='plotly_plot.html')
    return key, transferData, transferErrs, initPopulation, fits, avgFit, genAvgs, genErrs, fitCharacters
Example #51
0
    def figure(self, computed_traces=None):
        """Return a figure object compatible with plotly.graph_objs.

    Parameters:

    - computed_traces (dict; optional): The dendrogram traces from another
        (precomputed) Clustergram component.
        """
        dt, heatmap = None, None

        if computed_traces is None:
            (
                dt,
                self._data,
                self._row_labels,
                self._column_labels,
            ) = self._compute_clustered_data()
        else:
            # use, if available, the precomputed dendrogram and heatmap
            # traces (as well as the row and column labels)
            dt = computed_traces["dendro_traces"]
            heatmap = computed_traces["heatmap"]
            self._row_labels = computed_traces["row_labels"]
            self._column_labels = computed_traces["column_labels"]

        # this dictionary relates curve numbers (accessible from the
        # hoverData/clickData props) to cluster numbers
        cluster_curve_numbers = {}

        # initialize plot; GM is for group markers
        # [empty]      [col. dendro] [col. dendro] [empty]
        # [row dendro] [heatmap]     [heatmap]     [row GM]
        # [row dendro] [heatmap]     [heatmap]     [row GM]
        # [empty]      [col. GM]     [col. GM]     [empty]
        fig = tools.make_subplots(
            rows=4,
            cols=4,
            specs=[
                [{}, {
                    "colspan": 2
                }, None, {}],
                [{
                    "rowspan": 2
                }, {
                    "colspan": 2,
                    "rowspan": 2
                }, None, {
                    "rowspan": 2
                }],
                [None, None, None, None],
                [{}, {
                    "colspan": 2
                }, None, {}],
            ],
            vertical_spacing=0,
            horizontal_spacing=0,
            print_grid=False,
        )

        fig["layout"].update(hovermode="closest")

        # get the tick values; these will be at the leaves of the
        # dendrogram

        tickvals_col = []
        tickvals_row = []

        # for column dendrogram, leaves are at bottom (y=0)
        for i in range(len(dt["col"])):
            xs = dt["col"][i]["x"]
            ys = dt["col"][i]["y"]

            # during serialization (e.g., in a dcc.Store, the NaN
            # values become None and the arrays get turned into lists;
            # they must be converted back
            if isinstance(xs, list):
                xs = np.array(xs, dtype=np.float)
                dt["col"][i].update(x=xs)
            if isinstance(ys, list):
                ys = np.array(ys, dtype=np.float)
                dt["col"][i].update(y=ys)
            tickvals_col += [
                xs.flatten()[j] for j in range(len(xs.flatten()))
                if ys.flatten()[j] == 0.0 and xs.flatten()[j] % 10 == 5
            ]
        tickvals_col = list(set(tickvals_col))

        # for row dendrogram, leaves are at right(x=0, since we
        # horizontally flipped it)
        for i in range(len(dt["row"])):
            xs = dt["row"][i]["x"]
            ys = dt["row"][i]["y"]

            if isinstance(xs, list):
                xs = np.array(xs, dtype=np.float)
                dt["row"][i].update(x=xs)
            if isinstance(ys, list):
                ys = np.array(ys, dtype=np.float)
                dt["row"][i].update(y=ys)

            tickvals_row += [
                ys.flatten()[j] for j in range(len(ys.flatten()))
                if xs.flatten()[j] == 0.0 and ys.flatten()[j] % 10 == 5
            ]

        tickvals_row = list(set(tickvals_row))

        # sort so they are in the right order (lowest to highest)
        tickvals_col.sort()
        tickvals_row.sort()

        # update axis settings for dendrograms and heatmap
        axes = [
            "xaxis1",
            "xaxis2",
            "xaxis4",
            "xaxis5",
            "yaxis1",
            "yaxis2",
            "yaxis4",
            "yaxis5",
        ]

        for a in axes:
            fig["layout"][a].update(
                type="linear",
                showline=False,
                showgrid=False,
                zeroline=False,
                mirror=False,
                fixedrange=False,
                showticklabels=False,
            )

        (row_dendro_traces,
         col_dendro_traces) = self._sort_traces(dt["row"], dt["col"])

        for i in range(len(col_dendro_traces)):
            cdt = col_dendro_traces[i]
            cdt["name"] = "Col Cluster %d" % i
            cdt["line"] = dict(width=self._line_width[1])
            cdt["hoverinfo"] = "y+name"
            cluster_curve_numbers[len(fig.data)] = ["col", i]
            fig.append_trace(cdt, 1, 2)

        # row dendrogram (displays on left side)
        for i in range(len(row_dendro_traces)):
            rdt = row_dendro_traces[i]
            rdt["name"] = "Row Cluster %d" % i
            rdt["line"] = dict(width=self._line_width[0])
            rdt["hoverinfo"] = "x+name"
            cluster_curve_numbers[len(fig.data)] = ["row", i]
            fig.append_trace(rdt, 2, 1)

        # display row dendrogram sideways
        xaxis4 = fig["layout"]["xaxis4"]  # pylint: disable=invalid-sequence-index
        xaxis4.update(autorange="reversed")

        # ensure that everything is aligned properly
        # with the heatmap
        yaxis4 = fig["layout"]["yaxis4"]  # pylint: disable=invalid-sequence-index
        yaxis4.update(scaleanchor="y5")
        xaxis2 = fig["layout"]["xaxis2"]  # pylint: disable=invalid-sequence-index
        xaxis2.update(scaleanchor="x5")

        if len(tickvals_col) == 0:
            tickvals_col = [
                10 * i + 5 for i in range(len(self._column_labels))
            ]

        # add in all of the labels
        fig["layout"]["xaxis5"].update(  # pylint: disable=invalid-sequence-index
            tickmode="array",
            tickvals=tickvals_col,
            ticktext=self._column_labels,
            tickfont=self._tick_font,
            showticklabels=True,
            side="bottom",
            showline=False,
            range=[min(tickvals_col) - 5,
                   max(tickvals_col) + 5]
            # workaround for autoscale issues above; otherwise
            # the graph cuts off and must be scaled manually
        )

        if len(tickvals_row) == 0:
            tickvals_row = [10 * i + 5 for i in range(len(self._row_labels))]

        fig["layout"]["yaxis5"].update(  # pylint: disable=invalid-sequence-index
            tickmode="array",
            tickvals=tickvals_row,
            ticktext=self._row_labels,
            tickfont=self._tick_font,
            showticklabels=True,
            side="right",
            showline=False,
            range=[min(tickvals_row), max(tickvals_row)],
        )

        # hide labels, if necessary
        for l in self._hidden_labels:
            fig["layout"][l].update(ticks="", showticklabels=False)

        # recalculate the heatmap, if necessary
        if heatmap is None:

            # heatmap
            heat_data = self._data

            # symmetrize the heatmap about zero, if necessary
            if self._center_values:
                heat_data = np.subtract(heat_data, np.mean(heat_data))

            heatmap = go.Heatmap(
                x=tickvals_col,
                y=tickvals_row,
                z=heat_data,
                colorscale=self._color_map,
                colorbar={"xpad": 50},
            )

        fig.append_trace(heatmap, 2, 2)

        # hide all legends
        fig["layout"].update(showlegend=False, )

        # apply the display ratio
        row_ratio = 0
        col_ratio = 0

        # the argument can be either in list form or float form
        # first is ratio for row; second is ratio for column
        if self._display_ratio[0] != 0:
            row_ratio = 0.95 / float(1 + int(1 / self._display_ratio[0]))
        if self._display_ratio[1] != 0:
            col_ratio = 0.95 / float(1 + int(1 / self._display_ratio[1]))

        # the row/column labels take up 0.05 of the graph, and the rest
        # is taken up by the heatmap and dendrogram for each dimension

        # row: dendrogram, heatmap, row labels (left-to-right)
        # column: dendrogram, column labels, heatmap (top-to-bottom)

        # width adjustment for row dendrogram
        fig["layout"]["xaxis1"].update(  # pylint: disable=invalid-sequence-index
            domain=[0, 0.95])
        fig["layout"]["xaxis2"].update(  # pylint: disable=invalid-sequence-index
            domain=[row_ratio, 0.95],
            anchor="y4")
        fig["layout"]["xaxis4"].update(  # pylint: disable=invalid-sequence-index
            domain=[0, row_ratio])
        fig["layout"]["xaxis5"].update(  # pylint: disable=invalid-sequence-index
            domain=[row_ratio, 0.95])

        # height adjustment for column dendrogram
        fig["layout"]["yaxis1"].update(  # pylint: disable=invalid-sequence-index
            domain=[1 - col_ratio, 1])
        fig["layout"]["yaxis2"].update(  # pylint: disable=invalid-sequence-index
            domain=[1 - col_ratio, 1])
        fig["layout"]["yaxis4"].update(  # pylint: disable=invalid-sequence-index
            domain=[0, 0.95 - col_ratio])
        fig["layout"]["yaxis5"].update(  # pylint: disable=invalid-sequence-index
            domain=[0, 0.95 - col_ratio])

        fig["layout"]["legend"] = dict(  # pylint: disable=unsupported-assignment-operation
            x=0.7, y=0.7)

        # annotations

        # axis settings for subplots that will display group labels
        axes = ["xaxis6", "yaxis6", "xaxis8", "yaxis8"]

        for a in axes:
            fig["layout"][a].update(
                type="linear",
                showline=False,
                showgrid=False,
                zeroline=False,
                mirror=False,
                fixedrange=False,
                showticklabels=False,
            )

        # group labels for row dendrogram
        fig["layout"]["yaxis6"].update(  # pylint: disable=invalid-sequence-index
            domain=[0, 0.95 - col_ratio],
            scaleanchor="y5",
            scaleratio=1)
        if len(tickvals_row) > 0:
            fig["layout"]["yaxis6"].update(  # pylint: disable=invalid-sequence-index
                range=[min(tickvals_row), max(tickvals_row)])
        # padding between group label line and dendrogram
        fig["layout"]["xaxis6"].update(  # pylint: disable=invalid-sequence-index
            domain=[0.95, 1],
            range=[-5, 1])

        # group labels for column dendrogram
        fig["layout"]["xaxis8"].update(  # pylint: disable=invalid-sequence-index
            domain=[row_ratio, 0.95],
            scaleanchor="x5",
            scaleratio=1)
        if len(tickvals_col) > 0:
            fig["layout"]["xaxis8"].update(  # pylint: disable=invalid-sequence-index
                range=[min(tickvals_col), max(tickvals_col)])
        fig["layout"]["yaxis8"].update(  # pylint: disable=invalid-sequence-index
            domain=[0.95 - col_ratio, 1 - col_ratio],
            range=[-0.5, 0.5])

        # get group label annotations and label traces
        (
            row_group_labels,
            col_group_labels,
            row_annotations,
            col_annotations,
        ) = self._group_label_traces(row_dendro_traces, col_dendro_traces)
        # add annotations to graph
        fig["layout"].update(annotations=row_annotations + col_annotations)
        # add label traces to graph
        for rgl in row_group_labels:
            fig.append_trace(rgl, 2, 4)
        for cgl in col_group_labels:
            fig.append_trace(cgl, 4, 2)

        # set background colors
        fig["layout"].update(paper_bgcolor=self._paper_bg_color,
                             plot_bgcolor=self._plot_bg_color)

        # finally add height and width
        fig["layout"].update(height=self._height, width=self._width)

        computed_traces = {
            "dendro_traces": dt,
            "heatmap": heatmap,
            "row_labels": self._row_labels,
            "column_labels": self._column_labels,
        }

        return (fig, computed_traces, cluster_curve_numbers)
Example #52
0
dfframe = pd.read_csv('results.csv')

ecg = dfframe[dfframe['Conditions'] == 'ecg']
resp = dfframe[dfframe['Conditions'] == 'resp']
pace = dfframe[dfframe['Conditions'] == 'pace']

ecgdata = ecg['ch1'].append([ecg['ch2'], ecg['ch3'], ecg['ch4'], ecg['ch5']])
respdata = resp['ch1'].append(
    [resp['ch2'], resp['ch3'], resp['ch4'], resp['ch5']])
pacedata = pace['ch1'].append(
    [pace['ch2'], pace['ch3'], pace['ch4'], pace['ch5']])

trace1 = go.Histogram(x=ecgdata, )
trace2 = go.Histogram(x=respdata)
trace3 = go.Histogram(x=pacedata)

fig = tools.make_subplots(
    rows=2,
    cols=3,
    subplot_titles=('ECG', 'Resp', 'Pace'),
    shared_yaxes=True,  # this makes the hours appear only on the left
)
fig.append_trace(trace1, 1, 1)
fig.append_trace(trace2, 1, 2)
fig.append_trace(trace3, 1, 3)

fig['layout'].update(  # access the layout directly!
    title='Noise Data Conditions')
pyo.plot(fig, filename='AllThree.html')
Example #53
0
    def buildSubPlots(self, grid, row, column, ptrace, tit_lst):
        '''
        Draws plot in different plot canvases (not overlapping)

        params:
            grid (string): 'row' or 'col'. Plot are created in rows or columns
            row (int): number of rows (if row is selected)
            column (int): number of columns (if column is selected)
            ptrace (list of Plot Traces): list of all the different Plot Traces
            tit_lst (tuple): tuple containing the plot titles

        Returns the final html path containing the plot with the js_string for
        the interaction

        Console usage:
        # create the initial object
        p = Plot(plot_type, plot_properties, layout_properties)
        # call the methods to create the Trace and the Layout
        p.buildTrace()
        p.buildLayout()

        # finally create the Figure
        fig = p.buildSubPlots('row', 1, gr, pl, tt)
        '''

        if grid == 'row':

            fig = tools.make_subplots(rows=row,
                                      cols=column,
                                      subplot_titles=tit_lst)

            for i, itm in enumerate(ptrace):
                fig.append_trace(itm, row, i + 1)

        elif grid == 'col':

            fig = tools.make_subplots(rows=row, cols=column)

            for i, itm in enumerate(ptrace):
                fig.append_trace(itm, i + 1, column)

        # first lines of additional html with the link to the local javascript
        self.raw_plot = '<head><meta charset="utf-8" /><script src="{}"></script><script src="{}"></script></head>'.format(
            self.polyfillpath, self.plotlypath)
        # call the plot method without all the javascript code
        self.raw_plot += plotly.offline.plot(fig,
                                             output_type='div',
                                             include_plotlyjs=False,
                                             show_link=False)
        # insert callback for javascript events
        self.raw_plot += self.js_callback(self.raw_plot)

        # use regex to replace the string ReplaceTheDiv with the correct plot id generated by plotly
        match = re.search('Plotly.newPlot\("([^"]+)', self.raw_plot)
        substr = match.group(1)
        self.raw_plot = self.raw_plot.replace('ReplaceTheDiv', substr)

        self.plot_path = os.path.join(tempfile.gettempdir(),
                                      'temp_plot_name.html')
        with open(self.plot_path, "w") as f:
            f.write(self.raw_plot)

        return self.plot_path
Example #54
0
def get_ann_ret_plot(ret_series,
                     height = None,
                     width = None,
                     x2range = None,
                     orient = 'h',
                     dtime = 'monthly'):
    cum_series = get_eq_line(ret_series)
    if dtime == 'monthly':

        av_ann_mean = ret_series.resample('A').mean() * 12
        av_ann_std = ret_series.resample('A').std() * np.sqrt(12)

    elif dtime == 'daily':
       av_ann_mean = ret_series.resample('A').mean() * 252
       av_ann_std = ret_series.resample('A').std() * np.sqrt(252)


    annual_ret = get_ann_ret(ret_series)

    trace0 = Bar(x = np.round(annual_ret.values * 100,2),
                 y = annual_ret.index.year,
                 name = 'Total Annual Returns',
                 marker = dict(color = '#00FA9A',
                               line = dict(color = '#006400',
                                           width = 1),
                              ),
                 yaxis = 'y1',
                 orientation = 'h',
                 hoverinfo = 'x'
                )
    trace1 = Scatter(x = np.round(av_ann_mean.values * 100,2),
                     y = annual_ret.index.year,
                     name = 'Average Annual Returns',
                     mode = 'lines+markers',
                     line = dict(color = 'black',
                                 width = 1,
                                 dash = 'dashdot'),
                     hoverinfo = 'x'

                    )

    trace2 = Scatter(x = np.round(av_ann_std.values * 100,2),
                     y = annual_ret.index.year,
                     name = 'Annual Volatility',
                     mode = 'lines+markers',
                     line = dict(color = '#944bd2',
                                 width = 1,
                                 dash = 'longdashdot'
                                ),
                     hoverinfo = 'x'
                    )

    layout = dict(
        height = height,
        width = width,
        title='Average Annual Returns and Volatilty for {}'.format(ret_series.name),
        hovermode = 'closest',
        yaxis1=dict(
            showgrid=False,
            zeroline = False,
            showticklabels = True,
            showline=False,
            linewidth = 0.75,
            nticks = 30,
            domain=[0, 0.85],

        ),
        yaxis2=dict(
            showgrid=False,
            showline=True,
            showticklabels=True,
            linecolor='rgba(102, 102, 102, 0.8)',
            linewidth=2,
            tickangle = 90,
            domain=[0, 0.85],
        ),
        xaxis=dict(
            zeroline=False,
            showline=False,
            showticklabels= True,
            showgrid=True,
    #         side = 'top'
            domain=[0, 0.55],
        ),
        xaxis2=dict(
            zeroline=False,
            showline=False,
            showticklabels=True,
            showgrid=True,
            domain=[0.58, 1],
            range = x2range,
            side='top',
        ),
        legend=dict(
            x=0.029,
            y=1.038,
            font=dict(
                size=10,
            ),
        ),
        margin=dict(
            l=50,
            r=50,
            t=50,
            b=50,
        ),
        paper_bgcolor='rgb(248, 248, 255)',
        plot_bgcolor='rgb(248, 248, 255)',
    )
    x_s = np.round(annual_ret.values * 100,2)

    annots = []
    for xs, ys in zip(x_s, annual_ret.index.year):
        if xs > 0:
            x_loc = xs + 15
        else:
            x_loc = 15
        annots.append(dict(xref = 'x1',
                           yref = 'y1',
                           x = x_loc,
                           y = ys,
                           text = str(xs) + '%',
                           font = dict(family='Arial',
                                       size= 9,
                                       color='#006400'),
                            showarrow=False
                          )
                        )
    fig = tls.make_subplots(rows=1, cols=2, shared_xaxes=True,
                            shared_yaxes= False, vertical_spacing=0.001)
    fig.append_trace(trace0, 1, 1)
    fig.append_trace(trace1, 1, 2)
    fig.append_trace(trace2, 1, 2)


    layout['annotations'] = annots
    fig['layout'].update(layout)

    return fig
X = X_train[:n]
y = y_train[:n]

n_components = 2

pca = PCA(n_components).fit(X)

X_pca = pca.transform(X)

X1, X2 = np.linspace(-0.5, 0.5, 50), np.linspace(-0.5, 0.5, 50)
X_grid = np.array([np.ravel(X1), np.ravel(X2)]).T

fig = tools.make_subplots(
    rows=2,
    cols=2,
    subplot_titles=("Original space", "Projection by Kernel PCA",
                    "Projection by Fourier PCA", "Projection by Nystroem PCA"))

reds = y == 0
blues = y == 1

original_space1 = go.Scatter(x=X.iloc[reds, 0],
                             y=X.iloc[reds, 1],
                             mode='markers',
                             showlegend=False,
                             marker=dict(color='red',
                                         line=dict(color='black', width=1)))
original_space2 = go.Scatter(x=X.iloc[blues, 0],
                             y=X.iloc[blues, 1],
                             mode='markers',
Example #56
0
import plotly.plotly as py
import plotly.graph_objs as go
############################################
# PLOT_DOT = False
PLOT_DOT = True
############################################
layout = dict(title='Plotter CSV', xaxis=dict(), yaxis=dict())
#--------------------------------------------------
data = []
info = []
VISIBLE = True
path = "*.csv"
# path = "../../historial/*.csv"
fig = tools.make_subplots(rows=2,
                          cols=1,
                          subplot_titles=('Current', 'Voltage'),
                          shared_xaxes=True,
                          vertical_spacing=0.02)
for fname in glob.glob(path):
    # print(fname)
    with open(fname, 'rb') as f:
        reader = csv.DictReader(f, delimiter=',')
        holes = 0
        dot_listax = []
        dot_listay = []
        for row in reader:
            data.append(row)
        for row in data:
            ind = data.index(row) + 1
            if row['TIME'] == data[-1]['TIME']: break
            delta = int(data[ind]['TIME']) - int(row['TIME'])
Example #57
0
def update_output(stock_code_input):
    sk_df = []
    data=[]

    fig = make_subplots(specs=[[{"secondary_y": True}]])

    stock_name  = stock_mst.find_one({"단축코드" : stock_code_input})["종목명"]
    stock_code = stock_code_input

    stock_ratio = 0
    stock_vol = 0
    stock_price = 0
    stock_trd_vol = 0
    #stock_name, stock_ratio, stock_vol, stock_price, stock_trd_vol = check_data_2(stock_code_input, day)
    stock_name, stock_ratio, stock_price = check_data_2(stock_code_input, day)
    if (sk_data.count_documents({}) == 0):
        pass
    else:
        try:
            for i in sk_data.find({"단축코드": stock_code_input, "일자": day}, sort=[("체결시간", pymongo.ASCENDING)]):
                sk_df.append(i)
            sk_df = pd.DataFrame(sk_df)

            sk_df_x = sk_df["체결시간"].apply ( lambda x : string_to_datetime (day, x))
            '''print("sk_df_x   ")
            print(sk_df_x)
            print("sk_df_x   ")
            sk_df_for = sk_df["외국계순매수수량"].astype('int32')
            print("sk_df_for   ")
            print(sk_df_for)
            print("sk_df_for   ")
            #data.append( {'x' : sk_df_x , 'y': sk_df_for, 'type' :'line' , 'name' : '외국계순매수수량'})
            # Add traces
            fig.add_trace(
                go.Scatter(x=sk_df_x, y=sk_df_for, name="외국계순매수수량"),
                secondary_y=False,
            )'''
            sk_df_kor = sk_df["국내총순매수수량"].astype('int32')
            print("sk_df_kor   ")
            print(sk_df_kor)
            print("sk_df_kor   ")
            #data.append( {'x' : sk_df_x , 'y': sk_df_kor, 'type' :'line' , 'name' : '국내총순매수수량'})
            # Add traces
            fig.add_trace(
                go.Scatter(x=sk_df_x, y=sk_df_kor, name="국내총순매수수량"),
                secondary_y=False,
            )
            '''sk_df_all = sk_df["전체총매수수량"].astype('int32')
            print("sk_df_all   ")
            print(sk_df_all)
            print("sk_df_all   ")
            #data.append( {'x' : sk_df_x , 'y': sk_df_kor, 'type' :'line' , 'name' : '국내총순매수수량'})
            # Add traces
            fig.add_trace(
                go.Scatter(x=sk_df_x, y=sk_df_all, name="전체총매수수량"),
                secondary_y=False,
            )'''
        except:
            print("sk_df 다루던중 오류")

    if (real_TR_SCHART.count_documents({}) == 0):
        pass
    else:
        try:
            tr_schart_df = []
            for i in real_TR_SCHART.find({"단축코드": stock_code_input, "일자": day}, sort=[("체결시간", pymongo.ASCENDING)]):
                tr_schart_df.append(i)
            tr_schart_df = pd.DataFrame(tr_schart_df)

            tr_schart_df_x = tr_schart_df["시간"]
            tr_schart_df_x = tr_schart_df_x.apply(lambda x: string_to_datetime(day, x))

            tr_schart_df_price = tr_schart_df["종가"].astype('int32')
            # Add traces
            fig.add_trace(
                go.Scatter(x=tr_schart_df_x, y=tr_schart_df_price, name="5분 단위 현재가"),
                secondary_y=True,
            )

            tr_schart_df_vol = tr_schart_df["단위거래량"].astype('int32')
            # Add traces
            fig.add_trace(
                go.Scatter(x=tr_schart_df_x, y=tr_schart_df_vol, name="5분 단위거래량"),
                secondary_y=False,
            )

            #data.append({'x' : tr_schart_df_x , 'y': tr_schart_df_price, 'type' :'line' , 'name' : '5분 단위 현재가'})
        except:
            print("tr_schart_df 다루던중 오류")
        if (sp_data.count_documents({}) == 0):
            pass
        else:
            try:
                sp_df = []
                for i in sp_data.find({"단축코드": stock_code_input, "일자": day}, sort=[("시간", pymongo.ASCENDING)]):
                    sp_df.append(i)
                sp_df = pd.DataFrame(sp_df)


                sp_df_x = sp_df["시간"].apply(lambda x: string_to_datetime(day, x))

                sp_df_buy = sp_df["비차익매수위탁체결수량"].astype('int32')
                sp_df_sell = sp_df["비차익매도위탁체결수량"].astype('int32')
                sp_df_pure = sp_df_buy - sp_df_sell
                # Add traces
                fig.add_trace(
                    go.Scatter(x=sp_df_x, y=sp_df_pure, name="프로그램순매수수량"),
                    secondary_y=False,
                )
                #data.append({'x': sp_df_x, 'y': sp_df_pure, 'type': 'line', 'name': '프로그램순매수수량'})
            except:
                print("sp_df 다루던중 오류")
    if not data:
        data.append({'x' : [] , 'y': [], 'type' :'line' , 'name' : '데이터 없음'})

    # Set x-axis title
    fig.update_xaxes(title_text="분석 그래프 ")
    # Set y-axes titles
    fig.update_yaxes(
        tickformat=',',
        title_text="<b>primary</b> 거래량 수치 ",
        secondary_y=False)
    fig.update_yaxes(
        tickformat =',',
        title_text="<b>secondary</b> 가격 값 " ,
        secondary_y=True)
    return fig , stock_name, stock_code_input, stock_ratio, stock_vol, stock_price, stock_trd_vol
    '''figure = {
def Population(fileNum, atomLocations, showLoadingRate=True, showLoadingPic=False, plotCounts=False, countsMain=False,
            indvHist=True, histMain=False, simplePlot=False, showTotalHist=False, histBins=100, picsPerRep=1, whichPic=0, **StandardArgs):
    """
    Standard data analysis package for looking at loading rates throughout an experiment.
    return key, loadingRateList, loadingRateErr

    See standardLoadingAnalysis for valid standardArgs

    This routine is designed for analyzing experiments with only one picture per cycle. Typically
    These are loading exeriments, for example. There's no survival calculation.

    :param fileNum:
    :param atomLocations:
    :param showIndividualHist:
    :param showLoadingRate:
    :param showLoadingPic:
    :param StandardArgs:
    :param countsMain:
    :param plotCounts:
    :return:
    """

    res = standardPopulationAnalysis(fileNum, atomLocations, whichPic, picsPerRep, **StandardArgs)
    (pic1Data, thresholds, avgPic, key, loadingRateErr, loadingRateList, allLoadingRate, allLoadingErr, loadFits,
            fitModules, keyName, totalPic1AtomData, rawData, atomLocations, avgFits, atomImages, totalAvg, totalErr) = res 
    maxHeight = np.max(arr([np.histogram(data.flatten(), bins=histBins)[0] for data in pic1Data]).flatten())

    totalHist = []
    if showTotalHist:
        d, _ = np.histogram(pic1Data.flatten(), bins=100)
        totalHist.append(go.Histogram(x=pic1Data.flatten(), nbinsx=100, legendgroup='avg',
                                      showlegend=False, xbins=dict(start=min(pic1Data.flatten()),
                                                                   end=max(pic1Data.flatten())),
                                      marker=dict(color='#000000')))
        totalHist.append(go.Scatter(x=[np.mean(thresholds), np.mean(thresholds)], y=[0, max(d)],
                                    showlegend=False, mode='lines', line={'color': '#000000', 'width': 1},
                                    hoverinfo='none', legendgroup='avg'))
    colors, _ = getColors(len(atomLocations) + 1)
    countsFig = []
    if plotCounts:
        for atom, color in zip(atomLocations, colors):
            countsFig.append(go.Scatter(x=list(range(pic1Data[atom].flatten().size)), y=pic1Data[atom].flatten(),
                                        showlegend=False, mode='markers', line={'color': '#000000', 'width': 1},
                                        hoverinfo='none', legendgroup=str(atom), marker={'color': color, 'size':1}))
    indvHistFig = []
    alphaVal = 0.5
    if indvHist:
        for i, (atom, color, threshold) in enumerate(zip(atomLocations, colors, thresholds)):
            indvHistFig.append(go.Histogram(x=pic1Data[i].flatten(), legendgroup=str(atom), name=str(atom),
                                            nbinsx=histBins, showlegend=simplePlot, marker=dict(color=color),
                                            opacity=alphaVal))
            indvHistFig.append(go.Scatter(y=[0, maxHeight], x=[threshold, threshold], showlegend=False,
                                          mode='lines', line={'color': color, 'width': 1}, hoverinfo='none',
                                          legendgroup=str(atom)))
    if showLoadingPic:
        loadingPic = np.zeros(avgPic.shape)
        locFromKey = []
        minHor = min(transpose(key)[0])
        minVert = min(transpose(key)[1])
        for keyItem in key:
            locFromKey.append([int((keyItem[0] - minHor) / 9 * 2 + 2), int((keyItem[1] - minVert) / 9 * 2 + 2)])
        for i, loc in enumerate(locFromKey):
            loadingPic[loc[1]][loc[0]] = max(loadingRateList[i])
    if showLoadingRate:
        avgFig, mainPlot = [[] for _ in range(2)]
        avgFig.append(go.Heatmap(z=avgPic, colorscale='Viridis', colorbar=go.heatmap.ColorBar(x=1, y=0.15, len=0.3)))
        for err, loc, color, load, fitData in zip(loadingRateErr, atomLocations, colors, loadingRateList, loadFits):
            mainPlot.append(go.Scatter(x=key, y=load, error_y={'type': 'data', 'array': err, 'color': color},
                                       mode='markers', name=str(loc), legendgroup=str(loc),
                                       marker={'color': color}, opacity=alphaVal))
            avgFig.append(go.Scatter(x=[loc[1]], y=[loc[0]], mode='markers', hoverinfo='none',
                              showlegend=False, legendgroup=str(loc), marker={'size': 2, 'color': '#FF0000'}))
            if fitModules is not None:
                print(loc, errString(fitData['vals'][1], fitData['errs'][1], 4))
                mainPlot.append(go.Scatter(x=fitData['x'], y=fitData['nom'], line={'color': color},
                                           legendgroup=str(loc), showlegend=False, opacity=alphaVal))
                if fitData['std'] is not None:
                    mainPlot.append(go.Scatter(x=fitData['x'], y=fitData['nom'] + fitData['std'],
                                               opacity=alphaVal / 2, line={'color': color},
                                               legendgroup=str(loc), showlegend=False, hoverinfo='none'))
                    mainPlot.append(go.Scatter(x=fitData['x'], y=fitData['nom'] - fitData['std'],
                                               opacity=alphaVal / 2, line={'color': color},
                                               legendgroup=str(loc), fill='tonexty', showlegend=False,
                                               hoverinfo='none'))
        mainPlot.append(go.Scatter(x=key, y=allLoadingRate, marker={'color': '#000000'},
                                   error_y={'type': 'data', 'array': allLoadingErr, 'color': "#000000"},
                                   mode='markers', name='avg', legendgroup='avg'))
        if fitModules is not None:
            print('avg fit:', errString(avgFits['vals'][1], avgFits['errs'][1], 4))
            mainPlot.append(go.Scatter(x=avgFits['x'], y=avgFits['nom'], line={'color': '#000000'},
                                       legendgroup='avg', showlegend=False, opacity=alphaVal))
            if avgFits['std'] is not None:
                mainPlot.append(go.Scatter(x=avgFits['x'], y=avgFits['nom'] + avgFits['std'],
                                           opacity=alphaVal / 2, line={'color': '#000000'},
                                           legendgroup='avg', showlegend=False, hoverinfo='none'))
                mainPlot.append(go.Scatter(x=avgFits['x'], y=avgFits['nom'] - avgFits['std'],
                                           opacity=alphaVal / 2, line={'color': '#000000'},
                                           legendgroup='avg', fill='tonexty', showlegend=False,
                                           hoverinfo='none'))
        if simplePlot:
            if countsMain:
                plotData = countsFig
                layout = go.Layout(xaxis={'title': 'Pic #'}, yaxis={'title': 'Count #'})
            elif histMain:
                if showTotalHist:
                    histToShow = totalHist
                elif indvHist:
                    histToShow = indvHistFig
                else:
                    histToShow = []
                plotData = histToShow
                layout = go.Layout(xaxis={'title': 'Pic #'}, yaxis={'title': 'Count #'}, barmode='overlay')
            else:
                plotData = mainPlot
                layout = go.Layout(xaxis={'title': keyName}, yaxis={'title': 'Loading %', 'range': [0,1]})
            fig = go.Figure(data=plotData, layout=layout)
        else:
            fig = make_subplots(
                rows=3, cols=12, print_grid=False, horizontal_spacing=0, vertical_spacing=0.05,
                specs=[[{'rowspan': 3, 'colspan': 9}, None, None, None, None, None, None, None, None, {'colspan': 2},
                        None, {}],
                       [None, None, None, None, None, None, None, None, None, {'colspan': 3}, None, None],
                       [None, None, None, None, None, None, None, None, None, {'colspan': 3}, None, None]])
            if countsMain:
                mainLoc = (1, 10)
                mainNum = '2'
                countsNum = '1'
                countsLoc = (1, 1)
            else:
                mainLoc = (1, 1)
                mainNum = '1'
                countsNum = '2'
                countsLoc = (1, 10)
            for mainLine in mainPlot:
                fig.add_trace(mainLine, mainLoc[0], mainLoc[1])
            for avgPart in avgFig:
                fig.add_trace(avgPart, 3, 10)
            for counts in countsFig:
                fig.add_trace(counts, countsLoc[0], countsLoc[1])
            if showTotalHist:
                histToShow = totalHist
            elif indvHist:
                histToShow = indvHistFig
            else:
                histToShow = []
            for hist in histToShow:
                fig.add_trace(hist, 2, 10)
            layout = go.Layout(plot_bgcolor="rgb(182, 215, 168)", paper_bgcolor="rgb(182, 215, 168)")
            fig['layout'] = layout
            fig['layout'].update(barmode='overlay', plot_bgcolor="rgb(182, 215, 168)", paper_bgcolor="rgb(182, 215, 168)")
            fig['layout']['yaxis' + mainNum].update(title="Loading %", range=[0, 1])
            fig['layout']['xaxis' + mainNum].update(title=str(keyName))
            fig['layout']['yaxis' + countsNum].update(title="Count", range=[min(pic1Data.flatten()), max(pic1Data.flatten())])
            fig['layout']['xaxis' + countsNum].update(range=[0, len(pic1Data[0].flatten())])
            fig['layout']['yaxis3'].update(range=[min(pic1Data.flatten()), max(pic1Data.flatten())], showticklabels=False)
            fig['layout']['xaxis3'].update(showticklabels=False)
            # fig['layout']['yaxis4'].update(title="Loading %", range=[0,1])
            # fig['layout']['yaxis5'].update(title="Average Image")
        print('plotting figure...')
        iplot(fig)
    return key, loadingRateList, loadingRateErr, totalPic1AtomData, rawData, allLoadingRate
Example #59
0
def plotly_qc(fpath, saveto, sep=',', name=''):
    '''
    Generate a plotly html plot for QC of a scRNA-seq data.

    QC inlucdes:
    - number of total UMIs
    - number of detected genes
    - percent of MT expression

    Input:
    fpath: file path (CSV/TSV) to the expression file with genes/features as rows
    and cells/samples on columns. First column saves gene names.

    saveto: a html file to save the plots using Plot.ly

    sep: file sep. Default: ","
    '''

    bool_success = False
    if not is_nonempty_file(fpath):
        return bool_success

    if not name:
        name = base_name(fpath)

    expr = pd.read_csv(fpath, index_col=0, sep=sep)
    print_logger(('UMI count matrix: '
                  '{} genes x {} cells').format(expr.shape[0], expr.shape[1]))

    total_num_UMIs = expr.sum(axis=0)
    num_detected_genes = (expr > 0).sum(axis=0)
    mt_index = [
        x for x in expr.index if x.startswith('mt-') or x.startswith('MT-')
    ]
    if not mt_index:
        percent_mt = 0
    else:
        mt_umis = expr.loc[pd.Series(mt_index), :].sum(axis=0)
        percent_mt = mt_umis / total_num_UMIs
        percent_mt = percent_mt.replace(np.inf, 0)

    qc = pd.DataFrame(
        dict(total_num_UMIs=total_num_UMIs,
             num_detected_genes=num_detected_genes,
             percent_mt=percent_mt))

    # 1/5
    plotly_g_vs_umi = plotly_scatter(x=qc.total_num_UMIs,
                                     y=qc.num_detected_genes,
                                     xlab='#Total UMIs (median={})'.format(
                                         qc.total_num_UMIs.median()),
                                     ylab='#Detected Genes (median={})'.format(
                                         qc.num_detected_genes.median()),
                                     main=name,
                                     hover_text=qc.index.values)
    plotly_g_vs_umi.layout.yaxis.scaleanchor = None

    # 2/5
    plotly_mt_vs_umi = plotly_scatter(
        x=qc.total_num_UMIs,
        y=qc.percent_mt,
        xlab='#Total UMIs (median={})'.format(qc.total_num_UMIs.median()),
        ylab='MT Fraction (median={:6.4f})'.format(qc.percent_mt.median()),
        main=name,
        hover_text=qc.index.values)
    plotly_mt_vs_umi.layout.yaxis.scaleanchor = None

    # 3/5
    plotly_hist_umis = plotly_hist(vals=qc.total_num_UMIs,
                                   xlab='#Total UMIs (median={})'.format(
                                       qc.total_num_UMIs.median()))

    # 4/5
    plotly_hist_g = plotly_hist(vals=qc.num_detected_genes,
                                xlab=('#Detected Genes '
                                      '(median={})').format(
                                          qc.num_detected_genes.median()))
    # 5/5
    plotly_hist_percent_mt = plotly_hist(
        vals=qc.percent_mt,
        xlab='MT Fraction (median={:6.4f})'.format(qc.percent_mt.median()))

    # Merge the 5 figures together
    qc_fig = tools.make_subplots(rows=2,
                                 cols=3,
                                 specs=[[{}, {}, None], [{}, {}, {}]])
    qc_fig.append_trace(plotly_g_vs_umi.data[0], 1, 1)
    qc_fig.append_trace(plotly_mt_vs_umi.data[0], 1, 2)
    qc_fig.append_trace(plotly_hist_umis.data[0], 2, 1)
    qc_fig.append_trace(plotly_hist_g.data[0], 2, 2)
    qc_fig.append_trace(plotly_hist_percent_mt.data[0], 2, 3)

    qc_fig.layout.xaxis1 = {
        **qc_fig.layout.xaxis1,
        **plotly_g_vs_umi.layout.xaxis
    }
    qc_fig.layout.yaxis1 = {
        **qc_fig.layout.yaxis1,
        **plotly_g_vs_umi.layout.yaxis
    }

    qc_fig.layout.xaxis2 = {
        **qc_fig.layout.xaxis2,
        **plotly_mt_vs_umi.layout.xaxis
    }
    qc_fig.layout.yaxis2 = {
        **qc_fig.layout.yaxis2,
        **plotly_mt_vs_umi.layout.yaxis
    }

    qc_fig.layout.xaxis3 = {
        **qc_fig.layout.xaxis3,
        **plotly_hist_umis.layout.xaxis
    }
    qc_fig.layout.yaxis3 = {
        **qc_fig.layout.yaxis3,
        **plotly_hist_umis.layout.yaxis
    }

    qc_fig.layout.xaxis4 = {
        **qc_fig.layout.xaxis4,
        **plotly_hist_g.layout.xaxis
    }
    qc_fig.layout.yaxis4 = {
        **qc_fig.layout.yaxis4,
        **plotly_hist_g.layout.yaxis
    }

    qc_fig.layout.xaxis5 = {
        **qc_fig.layout.xaxis5,
        **plotly_hist_percent_mt.layout.xaxis
    }
    qc_fig.layout.yaxis5 = {
        **qc_fig.layout.yaxis5,
        **plotly_hist_percent_mt.layout.yaxis
    }

    qc_fig['layout'].update(height=800,
                            width=1000,
                            title=name,
                            showlegend=False)

    plot(qc_fig, filename=saveto, auto_open=False)

    bool_success = True
    return bool_success
 WH_S_2013=WH_S_2013.sort_values('Order_Demand', ascending=False)
 WH_S_2014=WH_S[WH_S['Year']==2014]
 WH_S_2014=pd.DataFrame(WH_S_2014.groupby('Product_Category', as_index=False)['Order_Demand'].mean())
 WH_S_2014=WH_S_2014.sort_values('Order_Demand', ascending=False)
 WH_S_2015=WH_S[WH_S['Year']==2015]
 WH_S_2015=pd.DataFrame(WH_S_2015.groupby('Product_Category', as_index=False)['Order_Demand'].mean())
 WH_S_2015=WH_S_2015.sort_values('Order_Demand', ascending=False)
 WH_S_2016=WH_S[WH_S['Year']==2016]
 WH_S_2016=pd.DataFrame(WH_S_2016.groupby('Product_Category', as_index=False)['Order_Demand'].mean())
 WH_S_2016=WH_S_2016.sort_values('Order_Demand', ascending=False)
 trace1 = go.Bar(x=WH_S_2012['Product_Category'],  y=WH_S_2012['Order_Demand'], name='Year_2012')
 trace2 = go.Bar(x=WH_S_2013['Product_Category'],  y=WH_S_2013['Order_Demand'], name='Year_2013')
 trace3 = go.Bar(x=WH_S_2014['Product_Category'],  y=WH_S_2014['Order_Demand'], name='Year_2014')
 trace4 = go.Bar(x=WH_S_2015['Product_Category'],  y=WH_S_2015['Order_Demand'], name='Year_2015')
 trace5 = go.Bar(x=WH_S_2016['Product_Category'],  y=WH_S_2016['Order_Demand'], name='Year_2016')
 fig = tools.make_subplots(rows=2, cols=5)
 fig.append_trace(trace5, 1, 1)
 fig.append_trace(trace4, 1, 2)
 fig.append_trace(trace3, 1, 3)
 fig.append_trace(trace2, 1, 4)
 fig.append_trace(trace1, 1, 5)
 layout=fig['layout'].update(height=500, width=1200, title='Order demand vs product category with respect to all years for '+ str (Warehouse[i]),xaxis=dict(
     title='Product Category',
     titlefont=dict(
         family='Courier New, monospace',
         size=18,
         color='#7f7f7f'
     )
 ),
 yaxis=dict(
     title='Order Demand',