Ejemplo n.º 1
0
def plot_scatter(positions, error, title, to_rescale=False):

    if to_rescale:
        trace = go.Scatter(x=positions[:, 0],
                           y=positions[:, 1],
                           mode='markers',
                           marker=dict(size=8,
                                       color=rescale(error),
                                       colorscale='Jet',
                                       colorbar=go.ColorBar(title='Colorbar',
                                                            yanchor='bottom'),
                                       showscale=True,
                                       opacity=0.8))
    else:
        trace = go.Scatter(x=positions[:, 0],
                           y=positions[:, 1],
                           mode='markers',
                           marker=dict(size=8,
                                       color=error,
                                       colorscale='Jet',
                                       showscale=True,
                                       colorbar=go.ColorBar(title='Colorbar',
                                                            yanchor='top'),
                                       opacity=0.8))

    return trace
def build_figure(layers_ls,
                 year,
                 colorscl=colorscl,
                 mapbox_access_token=None,
                 name='image.png'):
    data = go.Data([
        go.Scattermapbox(lat=[0],
                         lon=[0],
                         marker=go.Marker(cmax=5000,
                                          cmin=0,
                                          colorscale=colorscl,
                                          showscale=True,
                                          autocolorscale=False,
                                          color=range(0, 5000),
                                          colorbar=go.ColorBar(len=.89)),
                         mode='markers')
    ])

    layout = go.Layout(
        title='{}'.format(year),
        height=1050,
        width=800,
        autosize=True,
        hovermode='closest',
        mapbox=dict(layers=layers_ls,
                    accesstoken=mapbox_access_token,
                    bearing=0,
                    center=dict(lat=39.03, lon=-105.7),
                    pitch=0,
                    zoom=5.5,
                    style='light'),
    )

    fig = dict(data=data, layout=layout)
    py.image.save_as(fig, filename=name, width=750, height=575)
Ejemplo n.º 3
0
def plot_stress_meshed(mesh,stress):
    """
    Plots the stress on a real mesh using plotly
    :param mesh: The mesh
    :param stress: The stress
    """
    data = [
        go.Mesh3d(
            x=[0, 1, 2, 0],
            y=[0, 0, 1, 2],
            z=[0, 2, 0, 1],
            colorbar=go.ColorBar(
                title='z'
            ),
            colorscale=[[0, 'rgb(255, 0, 0)'],
                        [0.5, 'rgb(0, 255, 0)'],
                        [1, 'rgb(0, 0, 255)']],
            intensity=[0, 0.33, 0.66, 1],
            i=[0, 0, 0, 1],
            j=[1, 2, 3, 2],
            k=[2, 3, 1, 3],
            name='y',
            showscale=True
        )
    ]
    layout = go.Layout(
        xaxis=go.XAxis(
            title='x'
        ),
        yaxis=go.YAxis(
            title='y'
        )
    )
    fig = go.Figure(data=data, layout=layout)
    py.plot(fig, filename='/home/leon/Documents/RCI/TMA4220_NumPDE/3d-mesh-tetrahedron-python')
Ejemplo n.º 4
0
 def mesh_plot(self,
               file_name,
               data,
               x_label,
               y_label,
               z_label,
               options_dict=None,
               title_dict=None,
               x_range=None,
               y_range=None,
               z_range=None,
               **layout_kwargs):
     self.reset_graph_object()
     if options_dict is None:
         options_dict = {}
     options_dict = {
         **dict(colorbar=go.ColorBar(title=z_label)),
         **options_dict
     }
     self.data = [
         go.Mesh3d(x=data.loc[:, x_label].values,
                   y=data.loc[:, y_label].values,
                   z=data.loc[:, z_label].values,
                   **options_dict)
     ]
     self.plot_single_graph(file_name, title_dict, x_range, y_range,
                            z_range, **layout_kwargs)
Ejemplo n.º 5
0
def plot_plotly(x, y, uuids, clrs, title=None, xlabel=None, ylabel=None, clr_label=None):
    """Create plot using plot.ly

    Returns figure object
    """
    colorscale = 'Jet'
    colorbar=go.ColorBar(title=clr_label, titleside='right')

    marker = dict(size=10, line=dict(width=2), color=clrs, colorscale=colorscale, colorbar=colorbar)
    trace = go.Scatter(x=x, y=y, mode='markers', marker=marker, customdata=uuids)
    
    #N = len(x)
    ## workaround for links - for proper handling of click events use plot.ly dash
    #links = [ dict(x=x[i], y=y[i], text='<a href="{}/{}">o</a>'.format(rest_url, uuids[i]),
    #               showarrow=False, font=dict(color='#ffffff'), opacity=0.1) for i in range(N)]
    #
    #if with_links:
    #    layout = go.Layout(annotations=links, title=title, xaxis=dict(title=xlabel), yaxis=dict(title=ylabel))
    graph_layout.update(dict(
            title=title, 
            xaxis=dict(title=xlabel), 
            yaxis=dict(title=ylabel), 
            hovermode='closest'
    ))

    figure= dict(data = [trace], layout = graph_layout)
    return figure
Ejemplo n.º 6
0
def speed_plot(df, decimation=2, filename='speed_polar'):
    print("\nPlotting data")

    # - raw polar plot
    twa_rad = np.radians(df['wind_angle'][::decimation])
    labels = [
        'Wind: {}deg - {}kt ** Boat: {}kt ** Rudder: {}deg'.format(
            wa, ws, b, r) for wa, ws, b, r in
        zip(df['wind_angle'][::decimation], df['wind_speed'][::decimation],
            df['boat_speed'][::decimation], df['rudder_angle'][::decimation])
    ]

    speed = go.Scattergl(x=df['boat_speed'][::decimation] * np.sin(twa_rad),
                         y=df['boat_speed'][::decimation] * np.cos(twa_rad),
                         mode='markers',
                         marker=dict(size=6,
                                     color=df['wind_speed'][::decimation],
                                     colorscale='Portland',
                                     showscale=True,
                                     colorbar=go.ColorBar(title="Wind speed"),
                                     opacity=0.5),
                         text=labels)

    r_x = [
        rr * np.cos(i / 180. * np.pi) for rr in range(0, 20, 2)
        for i in range(0, 361)
    ]
    r_y = [
        rr * np.sin(i / 180. * np.pi) for rr in range(0, 20, 2)
        for i in range(0, 361)
    ]
    circle_labels = [
        str(i) + ' knots' for i in range(0, 20, 2) for ii in range(0, 361)
    ]

    # Create a trace
    iso_speed = go.Scattergl(x=r_x,
                             y=r_y,
                             mode='lines',
                             text=circle_labels,
                             line=dict(width=1, color='grey'))

    traces = [speed, iso_speed]

    layout = go.Layout(title='Speed vs True Wind',
                       orientation=90,
                       autosize=False,
                       width=800,
                       height=800,
                       hovermode='closest',
                       plot_bgcolor='rgb(245, 245, 245)')
    layout.xaxis.showticklabels = False
    layout.yaxis.showticklabels = False
    layout.xaxis.showgrid = False
    layout.yaxis.showgrid = False
    layout.xaxis.range = [-13, 13]
    layout.yaxis.range = [-13, 13]

    fig = go.Figure(data=traces, layout=layout)
    py.plot(fig, filename=filename + '.html', auto_open=False)
Ejemplo n.º 7
0
def create_figure(surface, colorscale='Viridis'):
    vertices, faces, colors = surface
    data = go.Data([
        go.Mesh3d(
            x=vertices[:, 0],
            y=vertices[:, 1],
            z=vertices[:, 2],
            colorbar=go.ColorBar(title=colors[0]),
            colorscale=colorscale,
            reversescale=True,
            intensity=colors[1],
            i=faces[:, 0],
            j=faces[:, 1],
            k=faces[:, 2],
            name='y',
            showscale=True,
        )
    ])
    axis_kws = dict(
        title='',
        showaxeslabels=False,
        showbackground=False,
        zeroline=False,
        ticks='',
        showticklabels=False,
        showgrid=False,
    )
    layout = go.Layout(scene=dict(
        xaxis=axis_kws,
        yaxis=axis_kws,
        zaxis=axis_kws,
    ), )
    return go.Figure(data=data, layout=layout)
Ejemplo n.º 8
0
def plotly_scatter(x, y, mask_by=None, hover_text=None,
                   xlab='', ylab='', main='',
                   colorscale='Viridis', mask_title=''):
    data = go.Scatter(
        x=x,
        y=y,
        mode='markers')
    if hover_text is not None:
        data['text'] = hover_text
    if mask_by is not None:
        data.marker = go.Marker(
            colorbar=go.ColorBar(
                title=mask_title,
                titleside='right'),
            color=mask_by,
            colorscale=colorscale,
            showscale=True, opacity=0.7)
    trace = [data]
    layout = go.Layout(
        title=main,
        xaxis=dict(title=xlab,
                   autorange=True),
        yaxis=dict(title=ylab, scaleanchor='x',
                   autorange=True),
        height=600,
        width=600)

    fig = go.Figure(data=trace, layout=layout)
    # ST's y should be reversed to match the H&E image
    fig.layout.yaxis.autorange = 'reversed'

    return fig
Ejemplo n.º 9
0
def traceStatsPoints(statPoints, dps, baselineDps = -1):
    if baselineDps == -1:
        baselineDps = max(dps)
    statCoords = np.array([np.dot(sp, COORDS) for sp in statPoints])
    x, y, z = statCoords.transpose()
    statTooltips = np.array([tooltipText(sp, v, max(dps)) for (sp, v) in zip(statPoints, dps)])
    sizes = 6 + 6 * (dps >= max(dps) - (max(dps) - min(dps)) * 0.05) + 6 * (dps == max(dps))
    colors = dps
    tickValues = generateTickValues(dps, generateTickStep(dps), baselineDps)
    tickTexts = generateTickTexts(dps, tickValues)
    return go.Scatter3d(
        x=x,
        y=y,
        z=z,
        text=statTooltips,
        hoverinfo='text',
        mode='markers',
        marker=dict(
            size=sizes,
            line=dict(
                color='rgba(32, 32, 32, 0.3)',
                width=0.5
            ),
            color=colors,
            colorbar=go.ColorBar(
                title='DPS',
                tickvals=tickValues,
                ticktext=tickTexts,
            ),
            colorscale=[[0., 'rgba(40,55,255, 0.3)'], [0.95, 'rgba(255, 60, 25, 0.7)'], [0.9501, 'rgba(255, 60, 25, 1)'], [0.9999, 'rgba(255, 60, 25, 1)'], [1., 'rgba(25, 225, 55, 1)']],
        ),
        showlegend=False,
    )
Ejemplo n.º 10
0
    def calc_graph(self):
        graph = []

        # This includes the point of today's emissions
        data_today = self.process_today_system_performance()
        data_today = self.normalize_data(data_today, self.normalization,
                                         self.objectives)
        xs = data_today[self.objectives[0]].values
        ys = data_today[self.objectives[1]].values
        name = "Today"
        trace = go.Scattergl(x=xs,
                             y=ys,
                             mode='markers',
                             name="Today's system",
                             text=name,
                             marker=dict(size='18',
                                         color='black',
                                         line=dict(color='black', width=2)))
        graph.append(trace)

        # PUT THE PARETO CURVE INSIDE
        data = self.process_generation_total_performance_pareto_with_multi()
        data = self.normalize_data(data, self.normalization, self.objectives)
        xs = data[self.objectives[0]].values
        ys = data[self.objectives[1]].values
        zs = data[self.objectives[2]].values

        individual_names = data['individual_name'].values

        trace = go.Scattergl(x=xs,
                             y=ys,
                             mode='markers',
                             name='Pareto optimal systems',
                             text=individual_names,
                             marker=dict(size='18',
                                         color=zs,
                                         colorbar=go.ColorBar(
                                             title=self.titlez,
                                             titleside='bottom'),
                                         colorscale='Jet',
                                         showscale=True,
                                         opacity=0.8))
        graph.append(trace)

        # This includes the points of the multicriteria assessment in here
        final_dataframe = calc_final_dataframe(data)
        xs = final_dataframe[self.objectives[0]].values
        ys = final_dataframe[self.objectives[1]].values
        name = final_dataframe["Attribute"].values
        trace = go.Scattergl(x=xs,
                             y=ys,
                             mode='markers',
                             name="Multi-criteria system",
                             text=name,
                             marker=dict(size='18',
                                         color='white',
                                         line=dict(color='black', width=2)))
        graph.append(trace)

        return graph
Ejemplo n.º 11
0
    def calc_graph(self):
        graph = []
        # #PUT THE HALL OF FAME INSIDE
        # data_HOF = self.process_generation_total_performance_halloffame()
        # data_HOF = self.normalize_data(data_HOF, self.normalization, self.objectives)
        # xs_HOF = data_HOF[self.objectives[0]].values
        # ys_HOF = data_HOF[self.objectives[1]].values
        # individual_names = data_HOF['individual_name'].values
        # trace = go.Scattergl(x=xs_HOF, y=ys_HOF, mode='markers', name='Hall of fame', text=individual_names,
        #                    marker=dict(size='12', color='grey_light'))
        # graph.append(trace)

        # PUT THE PARETO CURVE INSIDE
        data = self.process_generation_total_performance_pareto()
        data = self.normalize_data(data, self.normalization, self.objectives)
        xs = data[self.objectives[0]].values
        ys = data[self.objectives[1]].values
        zs = data[self.objectives[2]].values

        individual_names = data['individual_name'].values

        trace = go.Scattergl(x=xs,
                             y=ys,
                             mode='markers',
                             name='Pareto curve',
                             text=individual_names,
                             marker=dict(size='12',
                                         color=zs,
                                         colorbar=go.ColorBar(
                                             title=self.titlez,
                                             titleside='bottom'),
                                         colorscale='Jet',
                                         showscale=True,
                                         opacity=0.8))
        graph.append(trace)

        # This includes the points of the multicriteria assessment in here
        if self.multi_criteria:
            # Insert scatter points of MCDA assessment.
            final_dataframe = calc_final_dataframe(data)
            xs = final_dataframe[self.objectives[0]].values
            ys = final_dataframe[self.objectives[1]].values
            name = final_dataframe["Attribute"].values
            trace = go.Scattergl(x=xs,
                                 y=ys,
                                 mode='markers',
                                 name="Selected by Multi-criteria",
                                 text=name,
                                 marker=dict(size='20',
                                             color='white',
                                             line=dict(color='black',
                                                       width=2)))
            graph.append(trace)
        return graph
Ejemplo n.º 12
0
    def update_cell_pca_3d(feature):
        '''draw 3D PCA plot with selected feature'''
        if feature is None:
            color = ['rgba(' + str(1-a) + ', ' + str(b) + ', ' + str(c) + ')' \
                   for a,b,c in cells_adapt_pca_minmax[:,:3]]
        elif feature == 'experiment':
            color = ['rgba(' + str(a-0.001) + ', ' + str(b-0.001) + ', ' + str(c-0.001) + ')' \
                   for a,b,c in idx_color_mapping]
            print(color)
        else:
            i = list(feature_names.keys()).index(feature)
            color = cells_adapt_scaled[:,i]

        return {
            'data': [go.Scatter3d(
                x=cells_adapt_pca[:,0],
                y=cells_adapt_pca[:,2],
                z=cells_adapt_pca[:,1],
                mode='markers',
                text=text_labels,
                hoverinfo=[x for x in cells_ap['recording']],
                marker=dict(
                    size=8,
                    # color=['rgb(' + ', '.join(list(map(str, x))) + ')' for x in idx_color_mapping],
                    color=color,
                    colorbar=go.ColorBar(len=0.25,showticklabels=False, thickness=15, outlinewidth=0),
                    colorscale='RdYlBu',
                    # color='rgb(' + ', '.join(list(map(str, idx_color_mapping[0]))) + ')',
                    opacity=0.8
                )
            )],
            'layout': go.Layout(
                title="PCA",
                scene = dict(
                    xaxis=dict(title="PC-1 (%0.0f%%)" % (pca.explained_variance_ratio_[0]*100),
                              titlefont=dict(size=22)),
                    yaxis=dict(title="PC-3 (%0.0f%%)" % (pca.explained_variance_ratio_[2]*100),
                              titlefont=dict(size=22)),
                    zaxis=dict(title="PC-2 (%0.0f%%)" % (pca.explained_variance_ratio_[1]*100),
                              titlefont=dict(size=22)),
                    camera = dict(
                    up=dict(x=0, y=0, z=1),
                    center=dict(x=0, y=0, z=0),
                    eye=dict(x=-1, y=2, z=0.7)
                    )
                ),
                margin=dict(
                    l=0,
                    r=0,
                    b=0,
                    t=0
                ),
            )
        }
def plot_projection_on_two_components_gapstructure(plot_dict, plot_out):
    data = []
    for plot_data in plot_dict['data']:
        if plot_data['name'] == "Pfam":

            percent_gaps = [len(np.where(seq == 20)[0]) / float(plot_data['L']) for seq in plot_data['seq']]

            seq_nr  = ["seq no " + str(n) for n in range(1, plot_data['N'] + 1)]
            seq = ["".join(["<br>"+io.AMINO_ACIDS[plot_data['seq'][n][l]] if (l+1)% 50 == 0 else io.AMINO_ACIDS[plot_data['seq'][n][l]] for l in range(plot_data['L'])]) for n in range(plot_data['N'])]
            text = [seq_nr[n] + "<br>fraction of gaps: " + str(np.round(percent_gaps[n], decimals=3)) + "<br>" + seq[n] for n in range(plot_data['N'])]

            data.append(
                go.Scatter(
                    x=plot_data['x'],
                    y=plot_data['y'],
                    name=plot_data['name'],
                    mode='markers',
                    marker=dict(
                        color=percent_gaps,
                        colorbar=go.ColorBar(
                            title='Fraction of Gaps'
                        ),
                        colorscale='Bluered'
                    ),
                    text=text, #list(range(1, len(plot_data['x']) + 1)),
                    showlegend=False
                )
            )

    plot = {
        "data": data,
        "layout": go.Layout(
            font=dict(size=18),
            title="",
            margin=dict(t=10),
            hovermode='closest',
            yaxis=dict(
                title="principal component 2",
                exponentformat="e",
                showexponent='All'
            ),
            xaxis=dict(
                title="principal component 1",
                exponentformat="e",
                showexponent='All'
            )
        )
    }

    plotly_plot(plot, filename=plot_out, auto_open=False)
Ejemplo n.º 14
0
    def plot(self, showticklabels=False, scale=None):
        title = self.titlestring % (self.DS.name)
        D = self.DS.D.as_matrix().T
        d, n = D.shape
        D = (D - np.mean(D, axis=1).reshape(d, 1)) / np.std(
            D, axis=1).reshape(d, 1)
        D = np.nan_to_num(D)  # only nan if std all 0 -> all values 0

        num_bins = int(np.sqrt(2 * n))
        if num_bins > 20:
            num_bins = 20
        min_val = np.floor(np.min(D))
        if min_val < -5:
            min_val = -5
        max_val = np.ceil(np.max(D))
        if max_val > 5:
            max_val = 5
        bins = np.linspace(min_val, max_val,
                           (max_val - min_val) * num_bins + 1)
        bin_centers = (bins[1:] + bins[:-1]) / 2
        H = []
        for i in range(D.shape[0]):
            hist = np.histogram(D[i, :], bins=bins)[0]
            H.append(hist)
        z = np.vstack(H).astype(np.float)

        if scale == 'log':
            z[z > 0] = np.log(z[z > 0], dtype=np.float)

        trace = go.Heatmap(
            y=self.DS.D.columns,
            z=z,
            x=bins,
            colorscale=self.Reds,
            colorbar=go.ColorBar(title='Counts'))
        data = [trace]
        xaxis = go.XAxis(
            title="Normalized Value",
            ticks="outside",
            showticklabels=True,
        )
        yaxis = go.YAxis(
            title="Dimensions",
            ticks="",
            showticklabels=showticklabels,
            mirror=True)
        layout = self._get_layout(title, xaxis, yaxis)
        fig = dict(data=data, layout=layout)
        return self.makeplot(fig, "agg/" + self.shortname)
Ejemplo n.º 15
0
def calc_graph(data, objectives, table):
    xs = data[objectives[0]].values
    ys = data[objectives[1]].values
    zs = data[objectives[2]].values
    individual_names = data['individual'].values

    xmin = min(xs)
    ymin = min(ys)
    zmin = min(zs)
    xmax = max(xs)
    ymax = max(ys)
    zmax = max(zs)
    ranges_some_room_for_graph = [[
        xmin - ((xmax - xmin) * 0.1), xmax + ((xmax - xmin) * 0.1)
    ], [ymin - ((ymax - ymin) * 0.1), ymax + ((ymax - ymin) * 0.1)],
                                  [zmin, zmax]]

    graph = []
    trace = go.Scatter(x=xs,
                       y=ys,
                       mode='markers',
                       name='data',
                       text=individual_names,
                       marker=dict(size='12',
                                   color=zs,
                                   colorbar=go.ColorBar(
                                       title='Primary Energy [TJ]',
                                       titleside='bottom'),
                                   colorscale='Jet',
                                   showscale=True,
                                   opacity=0.8))
    graph.append(trace)

    #Insert scatter points of MCDA assessment.
    xs = table[objectives[0]].values
    ys = table[objectives[1]].values
    name = table["Attribute"].values
    trace = go.Scatter(x=xs,
                       y=ys,
                       mode='markers',
                       name="multi-criteria-analysis",
                       text=name,
                       marker=dict(size='20',
                                   color='white',
                                   line=dict(color='black', width=2)))
    graph.append(trace)

    return graph, ranges_some_room_for_graph
Ejemplo n.º 16
0
def plotly_trisurf(x,
                   y,
                   z,
                   colors,
                   simplices,
                   colormap=cm.RdBu,
                   plot_edges=False):
    points3D = np.vstack((x, y, z)).T
    tri_vertices = map(lambda index: points3D[index], simplices)

    ncolors = (colors - np.min(colors)) / (np.max(colors) - np.min(colors))

    I = simplices[:, 0]
    J = simplices[:, 1]
    K = simplices[:, 2]
    triangles = go.Mesh3d(x=x,
                          y=y,
                          z=z,
                          intensity=ncolors,
                          colorscale='Viridis',
                          i=I,
                          j=J,
                          k=K,
                          name='',
                          showscale=True,
                          colorbar=go.ColorBar(
                              tickmode='array',
                              tickvals=[np.min(z), np.max(z)],
                              ticktext=[
                                  '{:.3f}'.format(np.min(colors)),
                                  '{:.3f}'.format(np.max(colors))
                              ]))

    if plot_edges is False:  # the triangle sides are not plotted
        return Data([triangles])
    else:
        lists_coord = [[[T[k % 3][c] for k in range(4)] + [None]
                        for T in tri_vertices] for c in range(3)]
        Xe, Ye, Ze = [
            reduce(lambda x, y: x + y, lists_coord[k]) for k in range(3)
        ]
        lines = go.Scatter3d(x=Xe,
                             y=Ye,
                             z=Ze,
                             mode='lines',
                             line=go.Line(color='rgb(50,50,50)', width=1.5))
        return go.Data([triangles, lines])
Ejemplo n.º 17
0
    def _plot_plotly(self, layout):
        from plotly import graph_objs

        colorbar_plotly = graph_objs.ColorBar(
            thickness=15,  # color bar thickness in px
            ticks='outside',  # tick outside colorbar
            title='value')
        data = [{
            'type': 'heatmap',
            'z': self.z,
            'y': map(str, list(self.y[:, 0])),
            'x': map(str, list(self.x[0, :])),
            'colorscale': self.cmap,
            'colorbar': colorbar_plotly
        }]

        fig = graph_objs.Figure(data=graph_objs.Data(data), layout=layout)
        return fig
Ejemplo n.º 18
0
    def render(self, time, height):

        df = self._get_df(time, height)
        trace = go.Contour(
            z=df.values,
            colorbar=go.ColorBar(title='Pascals'),
        )
        data = [trace]

        layout = go.Layout(
            title='Pressure for t={} and Altitude (z={}): {:.1f} km'.format(
                str(time).lstrip('0'), height, height * 0.2),
            xaxis=dict(title='Longitude'),
            yaxis=dict(title='Latitude'),
        )

        fig = go.Figure(data=data, layout=layout)
        plotly.offline.iplot(fig)
Ejemplo n.º 19
0
def draw_plot(x, y, z, colors):

    #colorscale
    max = 8
    # max = int(zmax / 10)

    colorscale = [[0, colors[0]]]

    for i in range(0, max):
        colorscale.append([i / max, colors[i]])
        colorscale.append([(i + 1) / max, colors[i]])

    #hovertext
    hovertext = list()
    for yi, yy in enumerate(y):
       hovertext.append(list())
       for xi, xx in enumerate(x):
           if xi < len(z[yi]):
               xx = int(xx)
               hovertext[-1].append(
                   'Dos PLs tramitados de {} a {} meses na Câmara,<br>'.format(xx, xx + 10) + ' {0:.3f}% '.format(
                       z[yi][xi]) + 'do tempo foi em {}'.format(yy))
           else:
               hovertext[-1].append('Não houveram PLs tramitados na Câmara por {} meses'.format(xx))

    trace = [go.Heatmap(z=z,
                        y=y,
                        x=x,
                        text=hovertext,
                        hoverinfo='text',
                        colorscale=colorscale,
                        zmin=0,
                        zmax=80,
                        colorbar=go.ColorBar(title='Tempo no órgão (%)', titleside='top'),
                        showscale=True)]

    layout = dict(
            margin=dict(l=150, r=50, b=50, t=100, pad=4),
            xaxis=dict(title='Tempo de tramitação na Câmara (meses)'))

    figure = dict(data=trace, layout=layout)
    return figure
    def calc_graph(self):
        data = self.process_generation_total_performance()
        xs = data[self.objectives[0]].values
        ys = data[self.objectives[1]].values
        zs = data[self.objectives[2]].values
        individual_names = data['individual_name'].values

        graph = []
        trace = go.Scattergl(x=xs,
                             y=ys,
                             mode='markers',
                             name='data',
                             text=individual_names,
                             marker=dict(size='12',
                                         color=zs,
                                         colorbar=go.ColorBar(
                                             title='Primary Energy [MJoil]',
                                             titleside='bottom'),
                                         colorscale='Jet',
                                         showscale=True,
                                         opacity=0.8))
        graph.append(trace)

        # This includes the points of the multicriteria assessment in here
        if self.multi_criteria:
            # Insert scatter points of MCDA assessment.
            final_dataframe = calc_final_dataframe(data)
            xs = final_dataframe[self.objectives[0]].values
            ys = final_dataframe[self.objectives[1]].values
            name = final_dataframe["Attribute"].values
            trace = go.Scattergl(x=xs,
                                 y=ys,
                                 mode='markers',
                                 name="multi-criteria-analysis",
                                 text=name,
                                 marker=dict(size='20',
                                             color='white',
                                             line=dict(color='black',
                                                       width=2)))
            graph.append(trace)

        return graph
Ejemplo n.º 21
0
def plot_data(data):
    trace = go.Heatmap(x=np.arange(5),
                       y=np.arange(6),
                       z=data.T,
                       colorscale='Jet')

    trace = go.Scatter(
        #x=np.tile(np.arange(6), 5),
        #y=np.repeat(np.arange(5),6),
        x=np.tile(np.linspace(-2.5, 2.5, 6), 5),
        y=np.repeat(np.linspace(-1.6, 1.6, 5), 6),
        mode='markers',
        marker=dict(
            color=data.T.ravel(),
            colorscale='Jet',
            size=20,
            colorbar=go.ColorBar(title='Colorbar', ),
        ))

    return trace
Ejemplo n.º 22
0
def draw_plot(x, y, z, hm_col):
    trace = [
        go.Heatmap(
            z=z,
            y=y,
            x=x,
            # text=hovertext,
            # hoverinfo='text',
            colorscale=[
                [0, hm_col[6]],
                [1 / 7, hm_col[6]],

                # Let values between 10-20% of the min and max of z
                # have color rgb(20, 20, 20)
                [1 / 7, hm_col[5]],
                [2 / 7, hm_col[5]],

                # Values between 20-30% of the min and max of z
                # have color rgb(40, 40, 40)
                [2 / 7, hm_col[4]],
                [3 / 7, hm_col[4]],
                [3 / 7, hm_col[3]],
                [4 / 7, hm_col[3]],
                [4 / 7, hm_col[2]],
                [5 / 7, hm_col[2]],
                [5 / 7, hm_col[1]],
                [6 / 7, hm_col[1]],
                [6 / 7, hm_col[0]],
                [1.0, hm_col[0]]
            ],
            zmin=0,
            zmax=70,
            colorbar=go.ColorBar(title='Tempo no órgão (%)', titleside='top'),
            showscale=True)
    ]

    layout = dict(margin=dict(l=150, r=50, b=50, t=100, pad=4),
                  xaxis=dict(title='Tempo de tramitação na Câmara (meses)'))

    figure = dict(data=trace, layout=layout)
    return figure
Ejemplo n.º 23
0
    def _plot_plotly(self, layout):
        from plotly import graph_objs

        colorbar_plotly = graph_objs.ColorBar(
            thickness=15,  # color bar thickness in px
            ticks='outside',  # tick outside colorbar
        )
        data = [{
            'type': 'heatmap',
            'z': self.matrix,  # link 2D array
            'x': self.labels,  # link x-axis labels
            'y': self.labels,  # link y-axis labels
            'colorscale': self.cmap,  # (!) select pre-defined colormap
            'colorbar': colorbar_plotly,
            'zmin': self.vmin,
            'zmax': self.vmax,
            'zauto': False
        }]
        layout['xaxis'].update(tickangle=-90)

        fig = graph_objs.Figure(data=graph_objs.Data(data), layout=layout)
        return fig
Ejemplo n.º 24
0
    def _plot_plotly(self, layout):
        from plotly import graph_objs

        colorbar_plotly = graph_objs.ColorBar(
            thickness=15,  # color bar thickness in px
            ticks='outside',  # tick outside colorbar
            title='value'
        )
        X, Y = self.data
        data = [{'type': 'histogram2d',
                 'y': Y,
                 'x': X,
                 'colorscale': self.cmap,
                 'colorbar': colorbar_plotly,
                }]
        if self.vmin is not None:
            data[0]['zmin'] = self.vmin
            data[0]['zauto'] = False
        if self.vmax is not None:
            data[0]['zmax'] = self.vmax
            data[0]['zauto'] = False
        if self.range is None:
            data[0]['nbinsx'] = self.binsX
            data[0]['nbinsy'] = self.binsY
        else:
            start, end = self.range[1]
            size = 1. * (end - start) / self.binsY
            data[0]['ybins']= {'start': start, 'end': end, 'size': size}
            data[0]['autobiny'] =False
            start, end = self.range[0]
            size = 1. * (end - start) / self.binsX
            data[0]['xbins']= {'start': start, 'end': end, 'size': size}
            data[0]['autobinx'] =False
        if self.normed:
            data[0]['histnorm'] = 'probability'


        fig = graph_objs.Figure(data=graph_objs.Data(data), layout=layout)
        return fig
Ejemplo n.º 25
0
def make_plotly(times, rawvolts, power):
    """Optionally, make an interactive online plot at plot.ly"""

    plotly_times = times / 365.2422

    trace = go.Scatter(
        x=plotly_times,
        y=power,
        name='HRCBusPower',
        mode='markers',
        marker=dict(
            size='8',
            color=rawvolts,  #set color equal to a variable
            colorscale='Viridis',
            showscale=True,
            colorbar=go.ColorBar(title='Voltage (Volts)'),
        ))

    layout = go.Layout(
        title='Chandra / HRC Primary Bus Power over Mission Lifetime',
        hovermode='closest',
        xaxis=dict(
            title='Year',
            ticklen=5,
            zeroline=False,
            gridwidth=2,
        ),
        yaxis=dict(
            title='Power (Watts)',
            ticklen=5,
            gridwidth=2,
        ),
        showlegend=False)

    data = [trace]

    fig = go.Figure(data=data, layout=layout)
    py.plot(fig, filename='HRC Primary Bus Power', sharing='public')
Ejemplo n.º 26
0
    def _plot_plotly(self, layout):
        from plotly import graph_objs

        colorbar_plotly = graph_objs.ColorBar(
            thickness=15,  # color bar thickness in px
            ticks='outside',  # tick outside colorbar
            title='value'
        )
        data = [{'type': 'heatmap',
                 'z': self.z,
                 'y': map(str, list(self.y[:, 0])),
                 'x': map(str, list(self.x[0, :])),
                 'colorscale': self.cmap,
                 'colorbar': colorbar_plotly,
                }]
        if self.vmin is not None:
            data[0]['zmin'] = self.vmin
            data[0]['zauto'] = False
        if self.vmax is not None:
            data[0]['zmax'] = self.vmax
            data[0]['zauto'] = False

        fig = graph_objs.Figure(data=graph_objs.Data(data), layout=layout)
        return fig
Ejemplo n.º 27
0
    def get_figure(self, **kwargs):
        """Get a plotly figure of the heatmap."""

        emin = kwargs.pop('emin', -1.0)
        emax = kwargs.pop('emax', 1.0)
        width = kwargs.pop('width', 800)
        height = kwargs.pop('height', 600)
        margin_left = kwargs.pop('margin_left', 100)
        margin_bottom = kwargs.pop('margin_bottom', 60)
        margin_top = kwargs.pop('margin_top', 30)
        margin_right = kwargs.pop('margin_right', 0)
        colorbar_size = kwargs.pop('colorbar_size', 0.4)

        xaxis_label = kwargs.pop('xaxis_label', None)
        yaxis_label = kwargs.pop('yaxis_label', None)
        xticks_angle = kwargs.pop('xaxis_angle', 30)
        font = kwargs.pop('font', '"Droid Serif", "Open Serif", serif')
        font_size = kwargs.pop('font_size', 12)
        title = kwargs.pop('title', None)
        title_font_size = kwargs.pop('title_font_size', None)
        annotation_font_size = kwargs.pop('annotation_font_size', None)
        show_sample_labels = kwargs.pop('show_sample_labels', 'x')

        if show_sample_labels not in ['none', 'x', 'y', 'both']:
            raise ValueError('"show_sample_labels" must be "none", "x", "y", '
                             'or "both".')

        padding_top = kwargs.pop('padding_top', 0.1)
        padding_right = kwargs.pop('padding_top', 0.1)

        xaxis_nticks = kwargs.pop('xaxis_nticks', None)
        #yaxis_nticks = kwargs.pop('yaxis_nticks', None)

        if title_font_size is None:
            title_font_size = font_size

        if annotation_font_size is None:
            annotation_font_size = font_size

        colorbar_label = self.colorbar_label or 'Pearson Correlation'

        ### set up heatmap

        colorbar = go.ColorBar(
            lenmode='fraction',
            len=colorbar_size,
            title=colorbar_label,
            titlefont=dict(size=title_font_size, ),
            titleside='right',
            xpad=0,
            ypad=0,
            outlinewidth=0,  # no border
            thickness=20,  # in pixels
            # outlinecolor = '#000000',
        )

        def fix_plotly_label_bug(labels):
            """
            This fixes a bug whereby plotly treats labels that look
            like numbers (integers or floats) as numeric instead of
            categorical, even when they are passed as strings. The fix consists
            of appending an underscore to any label that looks like a number.
            """
            assert isinstance(labels, Iterable)
            fixed_labels = []
            for l in labels:
                try:
                    float(l)
                except ValueError:
                    fixed_labels.append(str(l))
                else:
                    fixed_labels.append(str(l) + '_')
            return fixed_labels

        x = fix_plotly_label_bug(self.corr_matrix.samples)
        y = x

        data = [
            go.Heatmap(z=self.corr_matrix.X,
                       x=x,
                       y=y,
                       zmin=emin,
                       zmax=emax,
                       colorscale=self.colorscale,
                       colorbar=colorbar,
                       hoverinfo='x+y+z',
                       **kwargs),
        ]

        xshowticklabels = False
        yshowticklabels = False

        ### set up layout
        if show_sample_labels == 'x':
            xshowticklabels = True
        elif show_sample_labels == 'y':
            yshowticklabels = True
        elif show_sample_labels == 'both':
            xshowticklabels = True
            yshowticklabels = True

        xticks = 'outside'
        yticks = 'outside'

        if xaxis_label is None:
            if self.corr_matrix.samples.name is not None:
                xaxis_label = self.corr_matrix.samples.name
            else:
                xaxis_label = 'Samples'
            xaxis_label = xaxis_label + ' (n = %d)' % self.corr_matrix.n

        if yaxis_label is None:
            yaxis_label = xaxis_label

        layout = go.Layout(
            width=width,
            height=height,
            title=title,
            titlefont=go.Font(size=title_font_size),
            font=go.Font(size=font_size, family=font),
            xaxis=go.XAxis(
                title=xaxis_label,
                titlefont=dict(size=title_font_size),
                showticklabels=xshowticklabels,
                ticks=xticks,
                nticks=xaxis_nticks,
                tickangle=xticks_angle,
                #range=[-0.5, self.corr_matrix.n-0.5],
                showline=True,
                zeroline=False,
                showgrid=False,
            ),
            yaxis=go.YAxis(
                title=yaxis_label,
                titlefont=dict(size=title_font_size),
                showticklabels=yshowticklabels,
                ticks=xticks,
                nticks=xaxis_nticks,
                autorange='reversed',
                showline=True,
                zeroline=False,
                showgrid=False,
            ),
            margin=go.Margin(l=margin_left,
                             t=margin_top,
                             b=margin_bottom,
                             r=margin_right,
                             pad=0),
        )

        ### add annotations

        # we need separate, but overlaying, axes to place the annotations
        layout['xaxis2'] = go.XAxis(
            overlaying='x',
            showline=False,
            tickfont=dict(size=0),
            autorange=False,
            #range=[-0.5, self.corr_matrix.n-0.5],
            range=[-0.5, self.corr_matrix.n - 0.5],
            ticks='',
            showticklabels=False,
            zeroline=False,
            showgrid=False,
        )

        layout['yaxis2'] = go.YAxis(
            overlaying='y',
            showline=False,
            tickfont=dict(size=0),
            autorange=False,
            range=[self.corr_matrix.n - 0.5, -0.5],
            ticks='',
            showticklabels=False,
            zeroline=False,
            showgrid=False,
        )

        # generate coordinates and labels for the block annotations
        k = len(self.block_annotations)
        block_coords = np.zeros((k, 2), dtype=np.float64)
        block_labels = []
        for i, ann in enumerate(self.block_annotations):
            block_coords[i, :] = [ann.start_index - 0.5, ann.end_index + 0.5]
            block_labels.append(ann.label)

        # this produces the squares for the block annotations
        for i in range(k):
            mn = block_coords[i, 0]
            mx = block_coords[i, 1]
            data.append(
                go.Scatter(
                    x=[mn, mx, mx, mn, mn],
                    y=[mn, mn, mx, mx, mn],
                    mode='lines',
                    hoverinfo='none',
                    showlegend=False,
                    line=dict(color='black'),
                    xaxis='x2',
                    yaxis='y2',
                ))

        # - this produces the square labels for the block annotations
        # - we use plotly annotations, so that the labels are not limited
        #   by the plotting area
        for i in range(k):
            mn = block_coords[i, 0]
            mx = block_coords[i, 1]
            layout.annotations.append(
                dict(
                    x=mx,
                    y=(mn + mx) / 2.0,
                    text=block_labels[i],
                    xref='x2',
                    yref='y2',
                    showarrow=False,
                    #ax=20,
                    #ay=-20,
                    xanchor='left',
                    yanchor='middle',
                    font=dict(
                        color='black',
                        size=annotation_font_size,
                        family='serif bold',
                    ),
                    bgcolor='white',
                    opacity=0.7,
                    #textanchor='top right'
                ))

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

        return fig
Ejemplo n.º 28
0
def mu2e_plot3d(df,
                x,
                y,
                z,
                conditions=None,
                mode='mpl',
                info=None,
                save_dir=None,
                save_name=None,
                df_fit=None,
                ptype='3d',
                aspect='square',
                cmin=None,
                cmax=None,
                ax=None,
                do_title=True,
                title_simp=None,
                do2pi=False,
                units='mm',
                show_plot=True):
    """Generate 3D plots, x and y vs z.

    Generate a 3D surface plot for a given DF and three columns. An optional selection string is
    applied to the data via the :func:`pandas.DataFrame.query` interface, and is added to the title.
    Extra processing is done to plot cylindrical coordinates if that is detected from `conditions`.
    This function supports matplotlib and various plotly plotting modes.  If `df_fit` is specified,
    `df` is plotted as a a scatter plot, and `df_fit` as a wireframe plot.

    Args:
        df (pandas.DataFrame): The input DF, must contain columns with labels corresponding to the
            'x', 'y', and 'z' args.
        x (str): Name of the first independent variable.
        y (str): Name of the second independent variable.
        z (str): Name of the dependent variable.
        conditions (str, optional): A string adhering to the :mod:`numexpr` syntax used in
            :func:`pandas.DataFrame.query`.
        mode (str, optional): A string indicating which plotting package and method should be used.
            Default is 'mpl'. Valid values: ['mpl', 'plotly', 'plotly_html', 'plotly_nb']
        info (str, optional): Extra information to add to the title.
        save_dir (str, optional): If not `None`, the plot will be saved to the indicated path. The
            file name is automated, based on the input args.
        save_name (str, optional): If `None`, the plot name will be generated based on the 'x', 'y',
            'z', and 'condition' args.
        df_fit (bool, optional): If the input df contains columns with the suffix '_fit', plot a
            scatter plot using the normal columns, and overlay a wireframe plot using the '_fit'
            columns.  Also generate a heatmap showing the residual difference between the two plots.
        ptype (str, optional): Choose between '3d' and 'heat'.  Default is '3d'.

    Returns:
        Name of saved image/plot or None.
    """

    # _modes = ['mpl', 'mpl_none', 'plotly', 'plotly_html', 'plotly_nb']
    _modes = [
        'mpl', 'mpl_none', 'plotly', 'plotly_html', 'plotly_nb',
        'plotly_html_img'
    ]

    if mode not in _modes:
        raise ValueError(mode + ' not one of: ' + ', '.join(_modes))

    if conditions:
        df, conditions_title = conditions_parser(df, conditions, do2pi)

    # Format the coordinates
    piv = df.pivot(x, y, z)
    X = piv.index.values
    Y = piv.columns.values
    Z = np.transpose(piv.values)
    Xi, Yi = np.meshgrid(X, Y)
    if df_fit:
        piv_fit = df.pivot(x, y, z + '_fit')
        Z_fit = np.transpose(piv_fit.values)
        data_fit_diff = (Z - Z_fit)
        Xa = np.concatenate(([X[0]], 0.5 * (X[1:] + X[:-1]), [X[-1]]))
        Ya = np.concatenate(([Y[0]], 0.5 * (Y[1:] + Y[:-1]), [Y[-1]]))

    # Prep save area
    if save_dir:
        if not os.path.exists(save_dir):
            os.makedirs(save_dir)
        if save_name is None:
            save_name = '{0}_{1}{2}_{3}'.format(
                z, x, y, '_'.join(
                    [i for i in conditions_title.split(', ') if i != 'and']))
            save_name = re.sub(r'[<>=!\s]', '', save_name)

            if df_fit:
                save_name += '_fit'

    # Start plotting
    if 'mpl' in mode:
        if not ax:
            if ptype.lower() == '3d' and not df_fit:
                # fig = plt.figure().gca(projection='3d')
                fig = plt.figure()
            elif ptype.lower() == 'heat':
                fig = plt.figure()
            else:
                fig = plt.figure(figsize=plt.figaspect(0.4),
                                 constrained_layout=True)
                fig.set_constrained_layout_pads(hspace=0., wspace=0.15)
                # fig.set_constrained_layout_pads(hpad=0.5, wpad=0.5, hspace=0., wspace=0.15)

        if df_fit:
            ax = fig.add_subplot(1, 2, 1, projection='3d')
            ax.plot(Xi.ravel(),
                    Yi.ravel(),
                    Z.ravel(),
                    'ko',
                    markersize=2,
                    zorder=100)
            ax.plot_wireframe(Xi, Yi, Z_fit, color='green', zorder=99)
        elif ptype.lower() == '3d':
            if not ax:
                ax = fig.gca(projection='3d')
            ax.plot_surface(Xi,
                            Yi,
                            Z,
                            rstride=1,
                            cstride=1,
                            cmap=plt.get_cmap('viridis'),
                            linewidth=0,
                            antialiased=False)
        elif ptype.lower() == 'heat':
            if ax:
                pcm = ax.pcolormesh(Xi, Yi, Z, cmap=plt.get_cmap('viridis'))
            else:
                plt.pcolormesh(Xi, Yi, Z, cmap=plt.get_cmap('viridis'))
        else:
            raise KeyError(ptype +
                           ' is an invalid type!, must be "heat" or "3D"')

        plt.xlabel(f'{x} ({units})', fontsize=18)
        plt.ylabel(f'{y} ({units})', fontsize=18)
        if ptype.lower() == '3d':
            ax.set_zlabel(z + ' (G)', fontsize=18)
            ax.ticklabel_format(style='sci', axis='z')
            ax.zaxis.labelpad = 20
            ax.zaxis.set_tick_params(direction='out', pad=10)
            ax.xaxis.labelpad = 20
            ax.yaxis.labelpad = 20
        elif do_title:
            if ax:
                cb = plt.colorbar(pcm)
            else:
                cb = plt.colorbar()
            cb.set_label(z + ' (G)', fontsize=18)
        if do_title:
            if title_simp:
                plt.title(title_simp)
            elif info is not None:
                plt.title(
                    f'{info} {z} vs {x} and {y} for DS\n{conditions_title}',
                    fontsize=20)
            else:
                plt.title('{0} vs {1} and {2} for DS\n{3}'.format(
                    z, x, y, conditions_title),
                          fontsize=20)
        if ptype.lower() == '3d':
            # ax.view_init(elev=35., azim=30)
            ax.view_init(elev=30., azim=30)
        if save_dir:
            plt.savefig(save_dir + '/' + save_name + '.png')

        if df_fit:
            ax2 = fig.add_subplot(1, 2, 2)
            max_val = np.max(data_fit_diff)
            min_val = np.min(data_fit_diff)
            abs_max_val = max(abs(max_val), abs(min_val))
            # if (abs_max_val) > 2:
            #     heat = ax2.pcolormesh(Xa, Ya, data_fit_diff, vmin=-1, vmax=1,
            #                           cmap=plt.get_cmap('viridis'))
            #     cb = plt.colorbar(heat, aspect=7)
            #     cb_ticks = cb.ax.get_yticklabels()
            #     cb_ticks[0] = '< -2'
            #     cb_ticks[-1] = '> 2'
            #     cb_ticks = cb.ax.set_yticklabels(cb_ticks)
            # else:
            heat = ax2.pcolormesh(Xa,
                                  Ya,
                                  data_fit_diff,
                                  cmap=plt.get_cmap('viridis'),
                                  vmin=-abs_max_val,
                                  vmax=abs_max_val)
            cb = plt.colorbar(heat, aspect=20)
            plt.title('Residual, Data-Fit', fontsize=20)
            cb.set_label('Data-Fit (G)', fontsize=18)
            ax2.set_xlabel(f'{x} ({units})', fontsize=18)
            ax2.set_ylabel(f'{y} ({units})', fontsize=18)
            # datacursor(heat, hover=True, bbox=dict(alpha=1, fc='w'))
            ax.dist = 11  # default 10
            if save_dir:
                # fig.tight_layout()
                plt.savefig(save_dir + '/' + save_name + '_heat.pdf')

    elif 'plotly' in mode:
        if z == 'Bz':
            z_fancy = '$B_z$'
        elif z == 'Br':
            z_fancy = '$B_r$'
        elif z == 'Bphi':
            z_fancy = r'$B_{\theta}$'
        if aspect == 'square':
            ar = dict(x=1, y=1, z=1)
            width = 800
            height = 650
        elif aspect == 'rect':
            ar = dict(x=1, y=4, z=1)
        elif aspect == 'rect2':
            ar = dict(x=1, y=2, z=1)
            width = 900
            height = 750
        axis_title_size = 25
        axis_tick_size = 16
        if title_simp:
            title = title_simp
        elif info is not None:
            title = '{0} {1} vs {2} and {3} for DS<br>{4}'.format(
                info, z, x, y, conditions_title)
        else:
            title = '{0} vs {1} and {2} for DS<br>{3}'.format(
                z, x, y, conditions_title)

        if ptype == 'heat':
            layout = go.Layout(
                title=title,
                # ticksuffix is a workaround to add a bit of padding
                width=height,
                height=width,
                autosize=False,
                xaxis=dict(
                    title=f'{x} ({units})',
                    titlefont=dict(size=axis_title_size, family='Arial Black'),
                    tickfont=dict(size=axis_tick_size),
                ),
                yaxis=dict(
                    title=f'{y} ({units})',
                    titlefont=dict(size=axis_title_size, family='Arial Black'),
                    tickfont=dict(size=axis_tick_size),
                ),
            )

        elif ptype == '3d':
            layout = go.Layout(
                title=title,
                titlefont=dict(size=30),
                autosize=False,
                width=width,
                height=height,
                scene=dict(
                    xaxis=dict(
                        title=f'{x} ({units})',
                        titlefont=dict(size=axis_title_size,
                                       family='Arial Black'),
                        tickfont=dict(size=axis_tick_size),
                        # dtick=400,
                        gridcolor='rgb(255, 255, 255)',
                        zerolinecolor='rgb(255, 255, 255)',
                        showbackground=True,
                        backgroundcolor='rgb(230, 230,230)',
                    ),
                    yaxis=dict(
                        title=f'{y} ({units})',
                        titlefont=dict(size=axis_title_size,
                                       family='Arial Black'),
                        tickfont=dict(size=axis_tick_size),
                        gridcolor='rgb(255, 255, 255)',
                        zerolinecolor='rgb(255, 255, 255)',
                        showbackground=True,
                        backgroundcolor='rgb(230, 230,230)',
                    ),
                    zaxis=dict(
                        # title='B (G)',
                        title='',
                        titlefont=dict(size=axis_title_size,
                                       family='Arial Black'),
                        tickfont=dict(size=axis_tick_size),
                        gridcolor='rgb(255, 255, 255)',
                        zerolinecolor='rgb(255, 255, 255)',
                        showbackground=True,
                        backgroundcolor='rgb(230, 230,230)',
                        showticklabels=False,
                        showaxeslabels=False,
                    ),
                    aspectratio=ar,
                    aspectmode='manual',
                    camera=dict(
                        center=dict(x=0, y=0, z=-0.3),
                        eye=dict(x=3.4496546255787175 / 1.6,
                                 y=2.4876029142395506 / 1.6,
                                 z=1.5875472335683052 / 1.6)
                        # eye=dict(x=2.4496546255787175/1.6,
                        #          y=2.4876029142395506/1.6,
                        #          z=2.5875472335683052/1.6)
                    ),
                ),
                showlegend=True,
                legend=dict(x=0.8,
                            y=0.9,
                            font=dict(size=18, family='Overpass')),
            )
        if df_fit:
            scat = go.Scatter3d(
                x=Xi.ravel(),
                y=Yi.ravel(),
                z=Z.ravel(),
                mode='markers',
                marker=dict(
                    size=3,
                    color='rgb(0, 0, 0)',
                    line=dict(color='rgb(0, 0, 0)'),
                    opacity=1,
                    # colorbar=go.ColorBar(title='Tesla',
                    #                      titleside='right',
                    #                      titlefont=dict(size=20),
                    #                      tickfont=dict(size=18),
                    #                      ),
                ),
                name='Data',
            )
            lines = [scat]
            line_marker = dict(color='green', width=2)
            do_leg = True
            for i, j, k in zip(Xi, Yi, Z_fit):
                if do_leg:
                    lines.append(
                        go.Scatter3d(x=i,
                                     y=j,
                                     z=k,
                                     mode='lines',
                                     line=line_marker,
                                     name='Fit',
                                     legendgroup='fitgroup'))
                else:
                    lines.append(
                        go.Scatter3d(x=i,
                                     y=j,
                                     z=k,
                                     mode='lines',
                                     line=line_marker,
                                     name='Fit',
                                     legendgroup='fitgroup',
                                     showlegend=False))
                do_leg = False

            # For hovertext
            # z_offset = (np.min(Z)-abs(np.min(Z)*0.3))*np.ones(Z.shape)
            # textz = [['x: '+'{:0.5f}'.format(Xi[i][j])+'<br>y: '+'{:0.5f}'.format(Yi[i][j]) +
            #           '<br>z: ' + '{:0.5f}'.format(data_fit_diff[i][j]) for j in
            #           range(data_fit_diff.shape[1])] for i in range(data_fit_diff.shape[0])]

            # For heatmap
            # projection in the z-direction
            # def proj_z(x, y, z):
            #     return z
            # colorsurfz = proj_z(Xi, Yi, data_fit_diff)
            # tracez = go.Surface(
            #     z=z_offset, x=Xi, y=Yi, colorscale='Viridis', zmin=-2, zmax=2, name='residual',
            #     showlegend=True, showscale=True, surfacecolor=colorsurfz, text=textz,
            #     hoverinfo='text',
            #     colorbar=dict(
            #         title='Data-Fit (G)',
            #         titlefont=dict(size=18),
            #         tickfont=dict(size=20),
            #         xanchor='center'),
            # )
            # lines.append(tracez)

        else:
            if ptype == '3d':
                surface = go.Surface(x=Xi,
                                     y=Yi,
                                     z=Z,
                                     colorbar=go.ColorBar(
                                         title='Gauss',
                                         titleside='right',
                                         titlefont=dict(size=25),
                                         tickfont=dict(size=18),
                                     ),
                                     colorscale='Viridis')
                lines = [surface]
            elif ptype == 'heat':
                heat = go.Heatmap(x=X,
                                  y=Y,
                                  z=Z,
                                  colorbar=go.ColorBar(
                                      title='{0} (G)'.format(z),
                                      titleside='top',
                                      titlefont=dict(size=18),
                                      tickfont=dict(size=20),
                                  ),
                                  colorscale='Viridis',
                                  showscale=True,
                                  zmin=cmin,
                                  zmax=cmax)
                lines = [heat]

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

        # Generate Plot
        if show_plot:
            if mode == 'plotly_nb':
                init_notebook_mode()
                iplot(fig)
                return fig
            elif mode == 'plotly_html_img':
                if save_dir:
                    plot(fig,
                         filename=save_dir + '/' + save_name + '.html',
                         image='jpeg',
                         image_filename=save_name)
                else:
                    plot(fig)
            elif mode == 'plotly_html':
                if save_dir:
                    plot(fig,
                         filename=save_dir + '/' + save_name + '.html',
                         auto_open=False)
                else:
                    plot(fig)

    # return save_name
    return fig
Ejemplo n.º 29
0
def plot_clust_dist_vectors(select_l1,
                            N_view=15,
                            N=400,
                            P=40,
                            Distances={},
                            Centre_distances={},
                            coeff_list={},
                            const_list={},
                            data={},
                            Dr='',
                            Fst=False):
    #select_l2= [1,7]

    selected1 = [
        x for x in range(Distances.shape[0])
        if data[Dr]['labels_l1'][x] + 1 in select_l1
    ]

    Focus_dist = np.array(Distances[selected1])
    Focus_center = np.array(Centre_distances[selected1])

    if Fst:
        Focus_dist = [
            coeff_list[selected1[x]] * np.log(Focus_dist[x]) +
            const_list[selected1[x]] for x in range(len(Focus_dist))
        ]
        Focus_dist = np.nan_to_num(np.array(Focus_dist))
        Focus_center = [
            coeff_list[selected1[x]] * np.log(Focus_center[x]) +
            const_list[selected1[x]] for x in range(len(Focus_dist))
        ]
        Focus_center = np.nan_to_num(np.array(Focus_center))

        print(Focus_center.shape)

    #Centre_distances= (Centre_distances - np.mean(Focus_center,axis= 0)) / np.std(Focus_center,axis= 0)

    print('{} of clusters selected'.format(len(selected1)))

    Distances_mean = np.mean(Distances[selected1], axis=0)

    range_distances = [
        np.percentile(Focus_dist, 1),
        np.percentile(Focus_dist, 99), N
    ]
    range_cdist = [
        np.percentile(Focus_center, 1),
        np.percentile(Focus_center, 99), N
    ]

    params = {'bandwidth': np.linspace(.05, .3, 10)}
    grid = GridSearchCV(KernelDensity(algorithm="ball_tree",
                                      breadth_first=False),
                        params,
                        verbose=0)
    distances_dens = []
    Cdist_dens = []

    i_coords, j_coords = np.meshgrid(np.linspace(range_distances[0],
                                                 range_distances[1], P),
                                     np.linspace(range_cdist[0],
                                                 range_cdist[1], P),
                                     indexing='ij')
    traces = [x for x in it.product(range(P), range(P))]

    params_unique = {'bandwidth': np.linspace(.1, .3, 20)}
    grid_unique = GridSearchCV(KernelDensity(algorithm="ball_tree",
                                             breadth_first=False),
                               params_unique,
                               verbose=0)

    params_dens = {'bandwidth': np.linspace(.4, .8, 20)}
    grid_dens = GridSearchCV(KernelDensity(algorithm="ball_tree",
                                           breadth_first=False),
                             params_dens,
                             verbose=0)

    traces = [x for x in it.product(range(P), range(P))]

    background = np.array([i_coords, j_coords])

    background = [background[:, c[0], c[1]] for c in traces]
    background = np.array(background)

    for karl in np.random.choice(list(range(len(Focus_center))),
                                 N_view,
                                 replace=False):
        """
        kde = KernelDensity(kernel='gaussian', bandwidth=0.2).fit(np.array(Distances[karl,:])[:,np.newaxis])
        scores= kde.score_samples(np.linspace(*range_distances)[:,np.newaxis])


        distances_dens.append(scores)

        kde = KernelDensity(kernel='gaussian', bandwidth=0.2).fit(np.array(Centre_distances[karl,:])[:,np.newaxis])
        scores= kde.score_samples(np.linspace(*range_cdist)[:,np.newaxis])

        Cdist_dens.append(scores)


        ### Density measure
        datum = np.array([[Distances[karl,x],Centre_distances[karl,x]] for x in range(Distances.shape[1])])
        grid_dens.fit(datum)
        kde = grid_dens.best_estimator_

        P_dist= kde.score_samples(datum)
        scores= kde.score_samples(background)

        scores= np.exp(scores)
        scores= np.array([x for x in scipy.stats.norm(np.mean(scores),np.std(scores)).cdf(scores)])

        """
        ### haplotypes measure
        dotum = np.array([[Focus_dist[karl, x], Focus_center[karl, x]]
                          for x in range(Focus_dist.shape[1])])

        datum = np.unique(dotum, axis=0)
        if len(datum) < 3:
            datum = dotum

        grid_unique.fit(datum)
        kde = grid_unique.best_estimator_

        P_dist = kde.score_samples(datum)
        scores_haps = kde.score_samples(background)
        scores_haps = np.exp(scores_haps)

        #scores= scores_haps
        scores = np.hstack((scores_haps))
        #
        Cdist_dens.append(scores)

    #distances_dens= np.array(distances_dens)
    Cdist_dens = np.array(Cdist_dens)
    #coords= {i:[x for x in range(Distances.shape[0]) if data['labels_l1'][x] == i] for i in list(set(data['labels_l1']))}

    ### Cdist_dens must have been calculated.

    i_coords, j_coords = np.meshgrid(np.linspace(range_distances[0],
                                                 range_distances[1], P),
                                     np.linspace(range_cdist[0],
                                                 range_cdist[1], P),
                                     indexing='ij')

    traces = [x for x in it.product(range(P), range(P))]

    background = np.array([i_coords, j_coords])

    background = [background[:, c[0], c[1]] for c in traces]
    background = np.array(background)

    Xs = []
    Ys = []
    Zs = []
    scores = []

    for target in range(N_view):
        Xs.extend(background[:, 0])
        Ys.extend(background[:, 1])
        scores.extend(Cdist_dens[target, :P**2])
        Zs.extend([target] * background.shape[0])

    thresho = .005
    tie = [x for x in range(len((scores))) if scores[x] < thresho]

    win = np.array([Xs, Ys, Zs, scores]).T
    unmasked = win[[x for x in range(win.shape[0]) if x not in tie], :]

    fig = [
        go.Scatter3d(x=unmasked[:, 0],
                     y=unmasked[:, 1],
                     z=unmasked[:, 2],
                     mode='markers',
                     marker={
                         'color': unmasked[:, 3],
                         'colorbar': go.ColorBar(title='ColorBar'),
                         'colorscale': 'Viridis',
                         'line': {
                             'width': 0
                         },
                         'size': 4,
                         'symbol': 'circle',
                         "opacity": .8
                     })
    ]

    fig = go.Figure(data=fig)
    iplot(fig)
Ejemplo n.º 30
0
def plot_accessions(selected_column,
                    df={},
                    vectors={},
                    orderCore={},
                    allow_geo=[],
                    allow_sbgp=[],
                    color_ref=[],
                    opac=.8,
                    cols_text=["ID", "NAME", "COUNTRY", "Initial_subpop"]):

    layout = go.Layout(margin=dict(l=0, r=0, b=0, t=0),
                       xaxis=dict(title='PC1', ),
                       showlegend=True)
    names_index = [[f for f in orderCore.ID].index(x)
                   for x in [str(y) for y in df.iloc[:, 0]]]
    opac = .8
    if selected_column == -2:
        scheme = [orderCore.loc[x, 'COUNTRY'] for x in names_index]
        allow = allow_geo
        scheme = [['Other', x][int(x in allow)] for x in scheme]
        coords = {
            y: [
                x for x in range(len(scheme))
                if scheme[x] == y and x in names_index
            ]
            for y in list(set(scheme))
        }

        fig = [
            go.Scatter3d(
                x=df.iloc[coords[i], 1],
                y=df.iloc[coords[i], 2],
                z=df.iloc[coords[i], 3],
                #type='scatter3d',
                mode="markers",
                text=orderCore.iloc[[names_index[x] for x in coords[i]], :]
                [cols_text].apply(
                    lambda lbgf: ("<b>{}</b><br>Name: {}<br>Country: {}<br>{}".
                                  format(lbgf[0], lbgf[1], lbgf[2], lbgf[3])),
                    axis=1),
                marker={
                    'line': {
                        'width': 0
                    },
                    'size': 4,
                    'symbol': 'circle',
                    "opacity": opac
                },
                name=i) for i in coords.keys() if coords[i]
        ]
    if selected_column == -1:
        scheme = [orderCore.loc[x, 'Initial_subpop'] for x in names_index]
        allow = allow_sbgp
        scheme = [['Other', x][int(x in allow)] for x in scheme]
        coords = {
            y: [
                x for x in range(len(scheme))
                if scheme[x] == y and x in names_index
            ]
            for y in list(set(scheme))
        }

        color_here = color_ref

        fig = [
            go.Scatter3d(
                x=df.iloc[coords[i], 1],
                y=df.iloc[coords[i], 2],
                z=df.iloc[coords[i], 3],
                #type='scatter3d',
                mode="markers",
                text=orderCore.iloc[[names_index[x] for x in coords[i]], :]
                [cols_text].apply(
                    lambda lbgf: ("<b>{}</b><br>Name: {}<br>Country: {}<br>{}".
                                  format(lbgf[0], lbgf[1], lbgf[2], lbgf[3])),
                    axis=1),
                marker={
                    'line': {
                        'width': 0
                    },
                    'size': 4,
                    'symbol': 'circle',
                    "opacity": opac
                },
                name=i) for i in coords.keys() if coords[i]
        ]
    if selected_column == 0:

        scheme = [int(orderCore.sNMF_K3[x]) for x in names_index]
        coords = {
            y: [
                x for x in range(len(scheme))
                if scheme[x] == y and x in names_index
            ]
            for y in list(set(scheme))
        }

        pop_refs = ["Indica", "cAus", "Japonica", "GAP", "cBasmati", "Admix"]
        color_here = color_ref

        fig = [
            go.Scatter3d(
                x=df.iloc[coords[i], 1],
                y=df.iloc[coords[i], 2],
                z=df.iloc[coords[i], 3],
                #type='scatter3d',
                mode="markers",
                text=orderCore.iloc[[names_index[x] for x in coords[i]], :]
                [cols_text].apply(
                    lambda lbgf: ("<b>{}</b><br>Name: {}<br>Country: {}<br>{}".
                                  format(lbgf[0], lbgf[1], lbgf[2], lbgf[3])),
                    axis=1),
                marker={
                    #        'color': scheme,
                    'color': color_here[i],
                    'line': {
                        'width': 0
                    },
                    'size': 4,
                    'symbol': 'circle',
                    "opacity": opac
                },
                name=pop_refs[i]) for i in coords.keys() if coords[i]
        ]

    if selected_column > 0:
        fig = [
            go.Scatter3d(
                x=df.iloc[:, 1],
                y=df.iloc[:, 2],
                z=df.iloc[:, 3],
                #type='scatter3d',
                mode="markers",
                text=orderCore.iloc[names_index, :][[
                    "ID", "NAME", "COUNTRY", "Initial_subpop"
                ]].apply(lambda lbgf:
                         ("<b>{}</b><br>Name: {}<br>Country: {}<br>{}".format(
                             lbgf[0], lbgf[1], lbgf[2], lbgf[3])),
                         axis=1),
                marker={
                    'color':
                    vectors.iloc[:, selected_column - 1],
                    'colorbar':
                    go.ColorBar(title='ColorBar'),
                    'colorscale': [[0.0, 'rgb(165,0,38)'],
                                   [0.1111111111111111, 'rgb(215,48,39)'],
                                   [0.2222222222222222, 'rgb(244,109,67)'],
                                   [0.3333333333333333, 'rgb(253,174,97)'],
                                   [0.4444444444444444, 'rgb(254,224,144)'],
                                   [0.5555555555555556, 'rgb(224,243,248)'],
                                   [0.6666666666666666, 'rgb(171,217,233)'],
                                   [0.7777777777777778, 'rgb(116,173,209)'],
                                   [0.8888888888888888, 'rgb(69,117,180)'],
                                   [1.0, 'rgb(49,54,149)']],
                    'line': {
                        'width': 0
                    },
                    'size':
                    4,
                    'symbol':
                    'circle',
                    "opacity":
                    opac
                })
        ]

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