示例#1
0
    def generate_rt_shift(self) -> Figure:
        """
        Creates a chromatogram plot showing the sift produced by retention time correction

            Returns:
                fig: A plotly.graph_objs.Figure consisting of two subplots: rt vs intensity on top and ri vs intensity
                on the bottom.
                Raw data is plotted as a bar chart and standards are overlaid as a scatter plot.

        """
        fig = Figure()
        height = 100 + 50 * (len(self.rawdata_dfs) // 7)
        # chromatogram plot
        for rawdata_df in self.rawdata_dfs:
            sample_color = next(self.sample_colors)
            sample_name = rawdata_df.columns.unique(level=0).values[0]
            rawdata_df = rawdata_df.xs(sample_name, axis=1, drop_level=True)

            # rt v intensity
            rawdata_summed_rt = rawdata_df.groupby(self.COLUMNS['RT'])[
                self.COLUMNS['INTENSITY']].sum().reset_index()
            fig.add_trace(
                Scattergl(x=rawdata_summed_rt[self.COLUMNS['RT']],
                          y=rawdata_summed_rt[self.COLUMNS['INTENSITY']],
                          legendgroup=sample_name,
                          marker_color=sample_color,
                          name='RT ' + sample_name,
                          mode='lines',
                          **self.scatter_shared_attrs))

            # ri v intensity
            rawdata_summed_ri = rawdata_df.groupby(self.COLUMNS['RI'])[
                self.COLUMNS['INTENSITY']].sum().reset_index()
            fig.add_trace(
                Scattergl(x=rawdata_summed_ri[self.COLUMNS['RI']],
                          y=rawdata_summed_ri[self.COLUMNS['INTENSITY']],
                          legendgroup=sample_name,
                          marker_color=sample_color,
                          line={'dash': 'dash'},
                          name='RI ' + sample_name,
                          mode='lines',
                          **self.scatter_shared_attrs))

            self.max_x = max(self.max_x, rawdata_df[self.COLUMNS['RI']].max(),
                             rawdata_df[self.COLUMNS['RT']].max())
            self.max_y = max(
                self.max_y, rawdata_summed_ri[self.COLUMNS['INTENSITY']].max(),
                rawdata_summed_rt[self.COLUMNS['INTENSITY']].max())

        # fig = self._add_zones_and_std_lines(fig)

        fig.update_layout(**self.scatter_layout,
                          xaxis_showticklabels=True,
                          height=height,
                          legend=dict(x=0.5,
                                      y=-0.1,
                                      xanchor='center',
                                      yanchor='top',
                                      orientation='h'))

        fig.update_xaxes(title_text='RT (solid) - RI (dashed)',
                         range=[0, self.max_x * 1.1])
        fig.update_yaxes(title_text='Intensity', tickformat='e')
        fig.update_layout(title_text="Retention Time correction comparison")
        return fig
示例#2
0
def draw_traces(traces,
                on_map=False,
                map_style='basic',
                showlegend=True,
                figsize=1,
                aspectratio='auto'):
    """
    plots all the traces (which can be created using :func:`create_bus_trace`, :func:`create_line_trace`,
    :func:`create_trafo_trace`)
    to PLOTLY (see https://plot.ly/python/)

    INPUT:
        **traces** - list of dicts which correspond to plotly traces
        generated using: `create_bus_trace`, `create_line_trace`, `create_trafo_trace`

    OPTIONAL:
        **on_map** (bool, False) - enables using mapbox plot in plotly

        **map_style** (str, 'basic') - enables using mapbox plot in plotly

            - 'streets'
            - 'bright'
            - 'light'
            - 'dark'
            - 'satellite'

        **showlegend** (bool, 'True') - enables legend display

        **figsize** (float, 1) - aspectratio is multiplied by it in order to get final image size

        **aspectratio** (tuple, 'auto') - when 'auto' it preserves original aspect ratio of the network geodata
        any custom aspectration can be given as a tuple, e.g. (1.2, 1)

    """

    if on_map:
        try:
            on_map = _on_map_test(traces[0]['x'][0], traces[0]['y'][0])
        except:
            logger.warning(
                "Test if geo-data are in lat/long cannot be performed using geopy -> "
                "eventual plot errors are possible.")

        if on_map is False:
            logger.warning(
                "Existing geodata are not real lat/lon geographical coordinates. -> "
                "plot on maps is not possible.\n"
                "Use geo_data_to_latlong(net, projection) to transform geodata from specific projection."
            )

    if on_map:
        # change traces for mapbox
        # change trace_type to scattermapbox and rename x to lat and y to lon
        for trace in traces:
            trace['lat'] = trace.pop('x')
            trace['lon'] = trace.pop('y')
            trace['type'] = 'scattermapbox'

    # setting Figure object
    fig = Figure(
        data=Data(traces),  # edge_trace
        layout=Layout(
            titlefont=dict(size=16),
            showlegend=showlegend,
            autosize=True if aspectratio is 'auto' else False,
            hovermode='closest',
            margin=dict(b=5, l=5, r=5, t=5),
            # annotations=[dict(
            #     text="",
            #     showarrow=False,
            #     xref="paper", yref="paper",
            #     x=0.005, y=-0.002)],
            xaxis=XAxis(showgrid=False, zeroline=False, showticklabels=False),
            yaxis=YAxis(showgrid=False, zeroline=False, showticklabels=False),
            # legend=dict(x=0, y=1.0)
        ),
    )

    # check if geodata are real geographycal lat/lon coordinates using geopy

    if on_map:
        try:
            mapbox_access_token = _get_mapbox_token()
        except Exception:
            logger.exception(
                'mapbox token required for map plots. '
                'Get Mapbox token by signing in to https://www.mapbox.com/.\n'
                'After getting a token, set it to pandapower using:\n'
                'pandapower.plotting.plotly.mapbox_plot.set_mapbox_token(\'<token>\')'
            )
            raise MapboxTokenMissing

        fig['layout']['mapbox'] = dict(
            accesstoken=mapbox_access_token,
            bearing=0,
            center=dict(lat=pd.Series(traces[0]['lat']).dropna().mean(),
                        lon=pd.Series(traces[0]['lon']).dropna().mean()),
            style=map_style,
            pitch=0,
            zoom=11)

    # default aspectratio: if on_map use auto, else use 'original'
    aspectratio = 'original' if not on_map and aspectratio is 'auto' else aspectratio

    if aspectratio is not 'auto':
        if aspectratio is 'original':
            # TODO improve this workaround for getting original aspectratio
            xs = []
            ys = []
            for trace in traces:
                xs += trace['x']
                ys += trace['y']
            x_dropna = pd.Series(xs).dropna()
            y_dropna = pd.Series(ys).dropna()
            xrange = x_dropna.max() - x_dropna.min()
            yrange = y_dropna.max() - y_dropna.min()
            ratio = xrange / yrange
            if ratio < 1:
                aspectratio = (ratio, 1.)
            else:
                aspectratio = (1., 1 / ratio)

        aspectratio = np.array(aspectratio) / max(aspectratio)
        fig['layout']['width'], fig['layout']['height'] = ([
            ar * figsize * 700 for ar in aspectratio
        ])

    # check if called from ipynb or not in order to consider appropriate plot function
    if _in_ipynb():
        from plotly.offline import init_notebook_mode, iplot as plot
        init_notebook_mode()
    else:
        from plotly.offline import plot as plot

    plot(fig)
示例#3
0
    def graph_10(self):
        legend = {"title":"Número de acessos, postagens e curtidas agrupados por estudante",
                    "xaxis":"",
                    "yaxis":"",
                    "columns":{1:"Curtidas", 2:"Postagens", 3:"Acessos"},
                    'hovertext':{1:' curtida(s)',2:' postagem(s)',3:' acesso(s)'}
                }
        if (self._language == "en"):
            legend = {"title":"Number of access, posts and likes grouped by student",
                        "xaxis":"",
                        "yaxis":"",
                        "columns":{1:"Likes", 2:"Posts", 3:"Access"},
                        'hovertext':{1:' like(s)',2:' post(s)',3:' access'}
                    }
        df = self.DATASET.sort_values(by=[self.DATASET.columns[0]])
        z = []
        hovervalue=[]
        max_value = 0

        for i in range (1, len(df.columns)-1):
            values = df.iloc[:,i].values.tolist()
            z.append(values)
            hovervalue.append(['<b>'+df.iloc[j,0]+'</b><br>'+str(df.iloc[j,i])+legend['hovertext'][i] for j in range(len(values))])
            max_local = max(values)
            max_value = max(max_local,max_value)
        
        trace = Heatmap(z=z,
                        y=[legend["columns"][i] for i in range (1,4)],
                        x=df.iloc[:,0], #Students
                        hovertext = hovervalue,
                        hoverinfo='text',
                        colorscale=[[0, 'rgb(255,255,255)'], [1, 'rgb(0,0,255)']],
                        showscale = True
                    )

        annotations=[]
        for i in range(1,len(df.columns)-1):
            for j in range(0,len(df)):
                color = 'rgb(0,0,0)'
                if df.iloc[j,i] > max_value/2:
                    color = 'rgb(255,255,255)'
                annotations.append({
                    "text":str(df.iloc[j,i]),
                    # "y":df.columns.values[i],
                    "y":legend["columns"][i],
                    "x":df.iloc[j,0],
                    "xref":'x1', 
                    "yref":'y1',
                    "showarrow":False,
                    "font":{
                        # family='Courier New, monospace',
                        # size=16,
                        "color":color
                    }
                })
        
        layout = Layout(
                title = legend["title"],
                # autosize=False,
                # width=950,
                # height=350,
                hovermode = "closest",
                xaxis=dict(
                    title = legend["xaxis"],
                    titlefont=dict(
                        # family='Arial, sans-serif',
                        # size=18,
                        color='rgb(180,180,180)',
                    ),
                ),
                yaxis=dict(
                    title = legend["xaxis"],
                    titlefont=dict(
                        # family='Arial, sans-serif',
                        # size=18,
                        color='rgb(180,180,180)',
                    ),
                    showticklabels=True,
                    type="category",
                    tick0=0,
                    dtick=1,
                    exponentformat='e',
                    showexponent='all',
                    gridcolor='#bdbdbd',
                ),
                margin = dict(
                    b=150,
                ),
                annotations = annotations
            )

        data = [trace]
        fig = Figure(data=data, layout=layout)
        if self._type_result == "jupyter-notebook":
            iplot(fig, filename='Heatmap')
        elif self._type_result == "dash":            
            return dcc.Graph(
                id='V003@10',
                figure=fig
            )
        elif self._type_result == "flask":            
            modeBarButtonsToRemove = ['toImage', 'sendDataToCloud', 'hoverCompareCartesian', 'lasso2d', 'hoverClosestCartesian', 'toggleHover', 'hoverClosest3d', 'hoverClosestGeo', 'hoverClosestGl2d', 'hoverClosestPie']
            config = {"displaylogo": False, "responsive": True, "displayModeBar": True, "modeBarButtonsToRemove": modeBarButtonsToRemove}
            return {"id":"V003@10","layout":json.dumps({"data": data, "layout": layout, "config": config}, cls=PlotlyJSONEncoder)}
示例#4
0
def plotAllStates(fileNameP, dictP):
    import plotly.plotly as py
    from plotly.graph_objs import Bar, Marker, Data, Layout, XAxis, YAxis, Figure, Margin, Font
    #import plotly.graph_objs as gObjLib
    #####
    dirToSave = "plotsesscat/"
    #barSize = 6
    listForNormalTrace = []
    listForHeavyTrace = []
    listForSuperHeavyTrace = []
    allCatList = ['None', 'Idle', 'System', 'Code', 'Debug', 'Build', 'Navi']
    for it_ in allCatList:
        #print "ZZZZZZZ", dictP[it_]
        listForNormalTrace.append(dictP[it_][0])
        listForHeavyTrace.append(dictP[it_][1])
        listForSuperHeavyTrace.append(dictP[it_][2])

    #print "length - Normal ... ", listForNormalTrace
    #print "length - Heavy ... ", listForHeavyTrace
    #print "length - SuperHeavy ... ", listForSuperHeavyTrace
    normalTrace = Bar(
        x=['None', 'Idle', 'System', 'Code', 'Debug', 'Build', 'Navi'],
        #x=[ 'Idle', 'System', 'Code', 'Debug', 'Build'],
        y=listForNormalTrace,
        name='Normal')

    heavyTrace = Bar(
        x=['None', 'Idle', 'System', 'Code', 'Debug', 'Build', 'Navi'],
        #x=[ 'Idle', 'System', 'Code', 'Debug', 'Build'],
        y=listForHeavyTrace,
        name='Heavy')

    superHeavyTrace = Bar(
        x=['None', 'Idle', 'System', 'Code', 'Debug', 'Build', 'Navi'],
        #x=[ 'Idle', 'System', 'Code', 'Debug', 'Build'],
        y=listForSuperHeavyTrace,
        name='Super Heavy')

    normalTrace['marker'] = Marker(color='green')
    heavyTrace['marker'] = Marker(color='yellow')
    superHeavyTrace['marker'] = Marker(color='red')
    dataToPlot = Data([normalTrace, heavyTrace, superHeavyTrace])

    layoutToUse = Layout(
        title='Distribution of Different Session Categories',
        titlefont=Font(family='Times New Roman', size=22, color='#000000'),
        barmode='group',
        xaxis=XAxis(showgrid=True,
                    showline=True,
                    title='Types of Sessions',
                    titlefont=Font(family='Times New Roman',
                                   size=18,
                                   color='#000000'),
                    tickwidth=1.5),
        yaxis=YAxis(
            showgrid=True,
            showline=True,
            title='Count',
            #range=[0, 35],
            autorange=True,
            titlefont=Font(family='Times New Roman', size=18,
                           color='#000000')),
        width=800,
        height=600,
        margin=Margin(l=140, r=40, b=50, t=80),
    )
    fileToSave = dirToSave + fileNameP + '.png'
    figToPlot = Figure(data=dataToPlot, layout=layoutToUse)
    #plot_url = py.plot(figToPlot, filename=fileToSave)
    py.image.save_as(figToPlot, fileToSave)
示例#5
0
def plotCorrelations(fileNameP, listP):
    import plotly.plotly as py
    #import plotly
    from plotly.graph_objs import Bar, Marker, Data, Layout, XAxis, YAxis, Figure, Margin, Font
    #import plotly.graph_objs as gObjLib
    ### authentication stuff ....
    #py = plotly.plotly(username='******', key='9ebth53cdo')
    #barSize = 5
    fileTrace = [listP[0], listP[1], listP[2]]
    projectTrace = [listP[3], listP[4], listP[5]]
    stateTrace = [listP[6], listP[7], listP[8]]
    namespaceTrace = [listP[9], listP[10], listP[11]]

    fileCorrTrace = Bar(
        x=['Corr. >= 0.50', 'Corr. within 0.0 & 0.499', 'Corr. < 0.0'],
        #x=[ 'Idle', 'System', 'Code', 'Debug', 'Build'],
        y=fileTrace,
        name='FileInterleaving')

    projCorrTrace = Bar(
        x=['Corr. >= 0.50', 'Corr. within 0.0 & 0.499', 'Corr. < 0.0'],
        #x=[ 'Idle', 'System', 'Code', 'Debug', 'Build'],
        y=projectTrace,
        name='ProjetInterleaving')

    nspaceCorrTrace = Bar(
        x=['Corr. >= 0.50', 'Corr. within 0.0 & 0.499', 'Corr. < 0.0'],
        #x=[ 'Idle', 'System', 'Code', 'Debug', 'Build'],
        y=namespaceTrace,
        name='NamespaceInterleaving')

    stateTrace = Bar(
        x=['Corr. >= 0.50', 'Corr. within 0.0 & 0.499', 'Corr. < 0.0'],
        #x=[ 'Idle', 'System', 'Code', 'Debug', 'Build'],
        y=stateTrace,
        name='StateInterleaving')

    #fileCorrTrace['marker'] = Marker(color='blue', size=barSize)
    #projCorrTrace['marker'] = Marker(color='green', size=barSize)
    #nspaceCorrTrace['marker'] = Marker(color='yellow', size=barSize)
    #stateTrace['marker'] = Marker(color='red', size=barSize)
    fileCorrTrace['marker'] = Marker(color='blue')
    projCorrTrace['marker'] = Marker(color='green')
    nspaceCorrTrace['marker'] = Marker(color='yellow')
    stateTrace['marker'] = Marker(color='red')
    dataToPlot = Data(
        [fileCorrTrace, projCorrTrace, nspaceCorrTrace, stateTrace])

    layoutToUse = Layout(
        title='Distribution of Correlation for 339 Datasets : ' + fileNameP,
        titlefont=Font(family='Times New Roman', size=18, color='#000000'),
        barmode='group',
        xaxis=XAxis(showgrid=True,
                    showline=True,
                    title='Interleaving Correlations',
                    titlefont=Font(family='Times New Roman',
                                   size=12,
                                   color='#000000'),
                    tickwidth=1.5),
        yaxis=YAxis(
            showgrid=True,
            showline=True,
            title='Count',
            #range=[0, 35],
            autorange=True,
            titlefont=Font(family='Times New Roman', size=12,
                           color='#000000')),
        width=800,
        height=600,
        margin=Margin(l=140, r=40, b=50, t=80),
    )
    fileToSave = 'plots/' + fileNameP + '.png'
    figToPlot = Figure(data=dataToPlot, layout=layoutToUse)
    py.image.save_as(figToPlot, fileToSave)
def main(is_oblasts_filled: bool, is_roads_visible: bool):
    """
    Головна функція програми, яка виконує малювання (рендеринг) карти. Вона
    почергово створює усі необхідні рівні в порядку накладання - області,
    міста, річки й дороги. Після цього кожен рівень розміщує на загальному
    "полотні" дані з файлів й текстові анотації (для міст). В кінці полотно
    масштабується за віссю Y, аби результуюча картинка не була розтягнена.
    Варто зазначити, що бібліотека для малювання дозволяє інтерактивно
    взаємодіяти з отриманою картою, але в дуже мінімальних межах -
    переважно через те, що карта відмальовує надто велику кількість
    багатокутників і точок. Дана бібліотека не призначена для малювання ГІС,
    для цього куди краще підійдуть спеціалізовані графічні двигуни й дані в
    більш специфічному форматі - наприклад, shapefile або KML, але це
    виходить за межі даної лабораторної.
    """
    layers = [
        Layer(
            'oblasts',
            is_filled=is_oblasts_filled,
            outer_fill_color='#ebf2e7',
            outer_line_color='#b46198',
            outer_line_width=2
        ),
        Layer(
            'cities',
            is_named=True,
            outer_fill_color='#a1a0a0',
            outer_line_color='#656464',
            outer_line_width=1,
            inner_fill_color='#ebf2e7',
            inner_line_color='#ebf2e7'
        ),
        Layer(
            'rivers',
            outer_fill_color='#9fcee5',
            outer_line_color='#2a5eea',
            outer_line_width=1,
            inner_fill_color='#ebf2e7',
            inner_line_color='#2a5eea',
            inner_line_width=1
        ),
        Layer(
            'roads',
            is_visible=is_roads_visible,
            outer_line_color='#ffb732',
            outer_line_width=2
        )
    ]
    figure = Figure()
    for layer in layers:
        scatters, annotations = layer.render2d()
        figure.add_traces(scatters)
        for annotation in annotations:
            figure.add_annotation(**annotation)
    figure.update_layout(plot_bgcolor='rgba(0,0,0,0)', showlegend=False)
    figure.update_xaxes(
        showline=True,
        linewidth=2,
        linecolor='#8b8b8b',
        mirror=True
    )
    figure.update_yaxes(
        scaleanchor='x',
        scaleratio=1.5,
        showline=True,
        linewidth=2,
        linecolor='#8b8b8b',
        mirror=True
    )
    figure.show()
示例#7
0
    def create_country_figure(
        self,
        graph_axis_type: GraphAxisType = GraphAxisType.Linear,
        graph_type: GraphType = GraphType.SinglePrediction,
    ):
        def adjust_xlabel(date: datetime.date):
            # Due to plotly limitations, we can only have graphs with dates on the x-axis when we
            # x-axis isn't log-scale.
            if graph_axis_type != GraphAxisType.LogLog:
                return date
            else:
                return (date - self.log_xaxis_date_since).days

        def color_and_opacity_by_event(event: PredictionEvent, count: int):
            orange = "rgb(255, 123, 37)"
            blue = "rgb(0, 121, 177)"
            green = "rgb(43, 161, 59)"
            # Matplotlib colors.
            if event == BK_20200329:
                return orange, 1.0
            if event == BK_20200411:
                return green, 1.0

            if graph_type == GraphType.MultiPredictions:
                # GraphType.MultiPredictions doesn't contain green BK_20200411, so it can have green
                return green, count / len(self.trace_by_event)
            elif graph_type == GraphType.BayesPredictions:
                return blue, min(1.0, 10.0 / len(self.trace_by_event))
            else:
                return blue, 1.0

        traces = []
        visibility = graph_type != GraphType.Slider
        count = 0
        for event, trace in self.trace_by_event.items():
            prediction_date_str = event.prediction_date.strftime("%b %d")
            data_until_idx = trace.xs.index(event.last_data_date)

            count += 1
            color, opacity = color_and_opacity_by_event(event, count)

            if graph_type == GraphType.MultiPredictions:
                # Last data date mark.
                data_until_y = trace.ys[data_until_idx]
                traces.append(
                    Scatter(
                        x=[
                            adjust_xlabel(event.last_data_date),
                            adjust_xlabel(event.last_data_date),
                        ],
                        y=[1.0, data_until_y],
                        mode="markers",
                        name="Data cutoff",
                        marker=dict(size=15, symbol="circle", color=color),
                        line=dict(width=3, color=color),
                        opacity=opacity,
                        legendgroup=event.name,
                        showlegend=False,
                        hoverinfo="skip",
                        visible=True,
                    ))
            elif graph_type != GraphType.BayesPredictions or count == 1:
                # For Bayesian predictions we only want to draw one rectangle
                rect_x = [
                    adjust_xlabel(self.cropped_dates[0]),
                    adjust_xlabel(event.last_data_date),
                ]
                rect_x = rect_x + rect_x[::-1]
                traces.append(
                    Scatter(
                        x=rect_x,
                        y=[0, 0, self.max_value, self.max_value],
                        mode="none",
                        fill="tozerox",
                        fillcolor="rgba(144, 238, 144, 0.4)",
                        opacity=0.4,
                        line=dict(color="rgba(255,255,255,0)"),
                        showlegend=False,
                        hoverinfo="skip",
                        legendgroup=event.name,
                        visible=visibility,
                    ))

            traces.append(
                Scatter(
                    x=list(map(adjust_xlabel, trace.xs[:data_until_idx + 1])),
                    y=trace.ys[:data_until_idx + 1],
                    text=trace.xs[:data_until_idx + 1],
                    mode="lines",
                    # TODO(lukas): we should have a better API then '.replace'
                    name=trace.label.replace("%PREDICTION_DATE%",
                                             prediction_date_str),
                    line=dict(width=2, color=color),
                    opacity=opacity,
                    legendgroup=event.name,
                    visible=visibility,
                ))
            traces.append(
                Scatter(
                    x=list(map(adjust_xlabel, trace.xs[data_until_idx:])),
                    y=trace.ys[data_until_idx:],
                    text=trace.xs[data_until_idx:],
                    mode="lines",
                    name=trace.label.replace("%PREDICTION_DATE%",
                                             prediction_date_str),
                    line=dict(width=2, dash="dot", color=color),
                    opacity=opacity,
                    legendgroup=event.name,
                    showlegend=False,
                    visible=visibility,
                ))

            if graph_type != GraphType.BayesPredictions:
                # Maximum mark.
                traces.append(
                    Scatter(
                        mode="markers",
                        x=[
                            adjust_xlabel(trace.max_value_date),
                            adjust_xlabel(trace.max_value_date),
                        ],
                        y=[1.0, trace.max_value],
                        name="Peak",
                        line=dict(color=color),
                        opacity=opacity,
                        marker=dict(size=15, symbol="star"),
                        legendgroup=event.name,
                        showlegend=False,
                        hoverinfo="skip",
                        visible=visibility,
                    ))

        # Add cumulated active cases trace.
        traces.append(
            Scatter(
                x=list(map(adjust_xlabel, self.cropped_dates)),
                y=self.cropped_cumulative_active,
                mode="lines+markers",
                name="Active cases",
                marker=dict(size=8),
                line=dict(width=3, color="rgb(239, 85, 59)"),
            ))

        sliders = []
        if graph_type == GraphType.Slider and len(self.trace_by_event) > 0:
            sliders.append(
                self._create_slider(traces, list(self.trace_by_event.keys())))

        layout = Layout(
            title=dict(text=f"Active cases in {self.long_name}",
                       x=0.5,
                       xanchor="center",
                       y=1),
            xaxis=dict(
                autorange=True,
                fixedrange=True,
                showgrid=False,
            ),
            yaxis=dict(
                tickformat=",.0f",
                gridcolor="LightGray",
                fixedrange=True,
                zerolinecolor="Gray",
            ),
            height=700,
            margin=dict(r=40),
            hovermode="x"
            if graph_type != GraphType.MultiPredictions else "closest",
            font=dict(size=18),
            legend=dict(x=0.01, y=0.99, borderwidth=1),
            showlegend=graph_type != GraphType.BayesPredictions,
            plot_bgcolor="White",
            sliders=sliders,
        )

        self.figure = Figure(data=traces, layout=layout)
        return self.update_graph_axis_type(graph_axis_type)
    def create_scatter_plot(self, graph_type=GraphType.Cumulative):
        if graph_type == GraphType.Cumulative:
            title_text = (
                r"$\text{Total COVID-19 cases for }b_0=_b0, \_param=_alpha, prefix=_prefix$"
            )
        else:
            title_text = (
                r"$\text{Daily new COVID-19 cases for }b_0=_b0, \_param=_alpha, prefix=_prefix$"
            )
        title_text = (title_text.replace("_b0", f"{self.best_b0}").replace(
            "_alpha",
            f"{self.best_param}").replace("_param", self.param_name).replace(
                "_prefix", f"{self.prefix_length}"))

        layout = Layout(
            title=title_text,
            xaxis=dict(autorange=True, title="Days"),
            yaxis=dict(autorange=True, title="COVID-19 cases in Slovakia"),
            hovermode="x",
            font={"size": 15},
            legend=dict(x=0.01, y=0.99, borderwidth=1),
        )

        figure = Figure(layout=layout)

        def transform(list_2d, graph_type=GraphType.Cumulative):
            if graph_type == GraphType.Cumulative:
                return list(chain.from_iterable(
                    accumulate(a) for a in list_2d))
            else:
                return list(chain.from_iterable(list_2d))

        figure.add_trace(
            Scatter(
                x=create_dates(self.simulated_positive, self.date_list),
                y=transform(self.simulated_positive, graph_type),
                mode="markers",
                name="Simulated confirmed cases",
                line={"width": 3},
                marker=dict(size=10, opacity=0.04),
            ))

        figure.add_trace(
            Scatter(
                x=self.date_list,
                y=transform([self.daily_positive], graph_type),
                text=self.date_list,
                mode="lines",
                name=r"Real confirmed cases",
            ))

        figure.add_trace(
            Scatter(
                x=create_dates(self.simulated_infected, self.date_list),
                y=transform(self.simulated_infected, graph_type),
                mode="markers",
                name="Simulated total infected",
                line={"width": 3},
                marker=dict(size=10, opacity=0.04),
            ))

        figure.add_trace(
            Scatter(
                x=self.date_list,
                y=transform([self.deltas], graph_type),
                mode="lines",
                name="Expected infected",
            ))

        return figure
示例#9
0
def plot_predictions(date_range=("2017-01-01", "2017-12-31"), df=preds_df):
    """


    Params:
    --------
        df : DataFrame
            DataFrame to use for plotting.

        date_range : tuple, optional
            The default is ("2017-01-01", "2017-12-31") which the based on
            the test DataFrame and the predictions are made for these dates.

    Returns:
    --------
        fig : Plotly Figure


    """
    # # get plot for predictions
    # df_new = make_sample_df(df)

    # time-series prediction data
    df_new = make_time_series(df)

    # define x and y
    y = df_new['meter_reading'].values
    x = df_new.index

    # define axis params to re-use
    xy_axis = dict(gridcolor='rgb(225, 225, 225)',
                   gridwidth=0.25,
                   linecolor='rgb(100, 100, 100)',
                   linewidth=2,
                   showticklabels=True,
                   color='black')
    # update x-axis params
    x_axis = xy_axis.copy()
    x_axis.update(
        dict(ticks='outside',
             tickfont=dict(
                 family='Arial',
                 color='rgb(82, 82, 82)',
             )))

    # new figure
    fig = Figure([
        Scatter(x=x,
                y=y,
                name='Predictions (Daily Meter Reading)',
                line=dict(color='royalblue', width=2))
    ])

    # Edit layout
    fig.update_layout(
        title='Predictions',
        yaxis_title='kWh',
        plot_bgcolor='white',
        yaxis=xy_axis,
        xaxis=x_axis,
    )
    # Use date string to set xaxis range
    fig.update_layout(xaxis_range=[date_range[0], date_range[1]], )

    return fig
示例#10
0
def _set_axis(self,traces,on=None,side='right',title=''):
	"""
	Sets the axis in which each trace should appear
	If the axis doesn't exist then a new axis is created

	Parameters:
	-----------
		traces : list(str)
			List of trace names
		on : string
			The axis in which the traces should be placed.
			If this is not indicated then a new axis will be
			created
		side : string
			Side where the axis will be placed
				'left'
				'right'
		title : string
			Sets the title of the axis
			Applies only to new axis
	"""
	fig={}
	fig_cpy=fig_to_dict(self).copy()
	fig['data']=fig_cpy['data']
	fig['layout']=fig_cpy['layout']
	fig=Figure(fig)
	traces=make_list(traces)

	def update_data(trace,y):
		anchor=fig.axis['def'][y]['anchor'] if 'anchor' in fig.axis['def'][y] else 'x1'
		idx=fig.trace_dict[trace] if isinstance(trace,str) else trace
		fig['data'][idx]['xaxis']=anchor
		fig['data'][idx]['yaxis']=y

	for trace in traces:
		if on:
			if on not in fig.axis['def']:
				raise Exception('"on" axis does not exists: {0}'.format(on))
			update_data(trace,y=on)
		else:
			curr_x,curr_y=fig.axis['ref'][trace]
			domain='[0.0, 1.0]' if 'domain' not in fig.axis['def'][curr_y] else str(fig.axis['def'][curr_y]['domain'])
			try:
				new_axis=fig.axis['dom']['y'][domain][side]
			except KeyError:
				axis=fig.axis['def'][curr_y].copy()
				### check overlaying values
				axis.update(title=title,overlaying=curr_y,side=side,anchor=curr_x)
				axis_idx=str(fig.axis['len']['y']+1)
				fig['layout']['yaxis{0}'.format(axis_idx)]=axis
				new_axis='y{0}'.format(axis_idx)
			update_data(trace,y=new_axis)


	for k in list(fig.axis['def'].keys()):
		id='{0}axis{1}'.format(k[0],k[-1:])
		if k not in fig.axis['ref_axis']:
			try:
				del fig['layout'][id]
			except KeyError:
				pass

	return fig
示例#11
0
def plotData(data_dict):
    '''
    Plots the data on the Plotly Framework.
    '''
    pData = data_dict['data']
    pData = sorted(pData, key=lambda x: x[0])

    processed_data = Scatter(
        x=[x[1] for x in pData],
        y=[y[2] for y in pData],
        mode='lines + text',
        text=list(range(1,
                        len(pData) + 1)),
        name=mac,
        marker=Marker(color="red"),
        opacity="0.5",
        legendgroup=mac,
    )

    startAndEndData = Scatter(
        x=[pData[0][1], pData[-1][1]],
        y=[pData[0][2], pData[-1][2]],
        mode='markers',
        marker=Marker(color="red", size="6"),
        showlegend=False,
        name=mac,
        text=["Start point", "End point"],
        legendgroup=mac,
    )

    py.sign_in(plotly_username, plotly_api_key)
    tls.set_credentials_file(username=plotly_username, api_key=plotly_api_key)
    layout = Layout(showlegend=True,
                    autosize=True,
                    height=800,
                    width=800,
                    title="MAP",
                    xaxis=XAxis(zerolinewidth=4,
                                gridwidth=1,
                                showgrid=True,
                                zerolinecolor="#969696",
                                gridcolor="#bdbdbd",
                                linecolor="#636363",
                                mirror=True,
                                zeroline=False,
                                showline=True,
                                linewidth=6,
                                type="linear",
                                range=[0, data_dict["length"]],
                                autorange=False,
                                autotick=False,
                                dtick=15,
                                tickangle=-45,
                                title="X co-ordinate"),
                    yaxis=YAxis(zerolinewidth=4,
                                gridwidth=1,
                                showgrid=True,
                                zerolinecolor="#969696",
                                gridcolor="#bdbdbd",
                                linecolor="#636363",
                                mirror=True,
                                zeroline=False,
                                showline=True,
                                linewidth=6,
                                type="linear",
                                range=[data_dict["width"], 0],
                                autorange=False,
                                autotick=False,
                                dtick=15,
                                tickangle=-45,
                                title="Y co-ordinate"))
    data = Data([processed_data, startAndEndData])
    fig = Figure(data=data, layout=layout)
    py.plot(fig, filename='Sample Code For History Of Clients ')
示例#12
0
        camera=Camera(
            eye=dict(x=2, y=2, z=0)  # todo revisit
        )
    ),
        width=800,
        autosize=False,
        height=800,
        margin=dict(r=20, l=10, b=10, t=10),
        updatemenus=[{'type': 'buttons',
                      'buttons': [{'label': 'Play',
                                   'method': 'animate',
                                   'args': [None]}]}]
    )
    frames = [dict(data=[
        Scatter3d(x=x[:k], y=y[:k], z=z[:k], mode='markers',
                  marker=dict(color=df['color'].iloc[:k], colorscale='Jet', size=6))]) for k in
        range(0, periods, slice_size)]

    fig = Figure(data=data, layout=layout, frames=frames)

    plot(fig, filename='../output/plotly_3d_animation.html', auto_open=False)

    logger.info('done')

    finish_time = time()
    elapsed_hours, elapsed_remainder = divmod(finish_time - start_time, 3600)
    elapsed_minutes, elapsed_seconds = divmod(elapsed_remainder, 60)
    logger.info('Time: {:0>2}:{:0>2}:{:05.2f}'.format(int(elapsed_hours), int(elapsed_minutes), elapsed_seconds))
    console_handler.close()
    logger.removeHandler(console_handler)
示例#13
0
def uplift_bar(outcome: list, treat: list, score: list, task="classification", output=False, file_name=False) -> Bar:
    """create a DataFrame that is used to evaluate uplift-model for classification settings.

    :param outcome: list of integer which represent the target class for each suject in the test data.
    :param treat: list of bools which represent whether a sunject is in the treatment group or not.
    :param score: list of floats which represent computed uplift-scores for each subject.
    :param output: whether output the scatter file or not.
    :param file_name: the name of the output file.
    :return: pd.DataFrame which contains experimental result of a given uplift modleing approach.
    """
    # sorting by uplift socre
    test_list = DataFrame(list(zip(outcome, treat, uplift_score)), columns=["outcome", "treat", "uplift_score"]).sort_values(by="uplift_score", ascending=False)
    qdf = DataFrame(columns=("y_treat", "y_control"))

    # sort by uplift-score and divid into 10 groups
    for n in range(10):
        start = int(n * len(test_list) / 10)
        end = int((n + 1) * len(test_list) / 10) - 1
        quantiled_result = test_list[start:end]

        # count the num of subjects in treatment and control group at each decile
        treat_uu = list(map(lambda item: item[1], quantiled_result)).count(True)
        control_uu = list(map(lambda item: item[1], quantiled_result)).count(False)

        # calc the avg outcome for treatment and control group at each decile
        if task == "classification":
            treat_cv_list = []
            control_cv_list = []

            for item in quantiled_result:
                if item[1]:
                    treat_cv_list.append(item[0])
                else:
                    control_cv_list.append(item[0])

            treat_cv = treat_cv_list.count(True)
            control_cv = control_cv_list.count(True)

            y_treat = treat_cv / treat_uu
            y_control = control_cv / control_uu

        elif task == "regression":
            y_treat_list = []
            y_control_list = []

            for item in quantiled_result:
                if item[1]:
                    y_treat_list.append(item[0])
                else:
                    y_control_list.append(item[0])

            y_treat = mean(y_treat_list)
            y_control = mean(y_control_list)

        label = "{}%~{}%".format(n * 10, (n + 1) * 10)
        qdf.loc[label] = [y_treat, y_control]

    trace1 = Bar(x=qdf.index.tolist(),
                 y=qdf.y_treat.values.tolist(), name="treat")
    trace2 = Bar(x=qdf.index.tolist(),
                 y=qdf.y_control.values.tolist(), name="control")

    layout = Layout(barmode="group", yaxis={"title": "Mean Outcome"}, xaxis={"title": "Uplift Score Percentile"})
    fig = Figure(data=[trace1, trace2], layout=layout)
    iplot(fig)

    if output:
        plot(fig, filename=file_name)
示例#14
0
def uplift_curve(df: pd.DataFrame, score_name: string, output=[False, False, False, False], file_names=None) -> Scatter:
    """plot uplift_curves for the given uplift modeling approach.

    :param df: pd.DataFrame which contains experimental result of a given uplift modleing approach.
               this is the output of the functin "uplift_frame".
    :param score_name: the name of the used uplift-socre.
    :param output: whether output each scatter file or not.
    :param file_names: list of the names of the output file.
    :return: computed AUUC and uplift_curve.

    """

    # calc Area Under Uplift Curve
    auuc = round(((np.array(df.lift) - np.array(df.base_line)).sum()) / len(df.lift), 3)

    # Total Outcome sorted by Uplift Rank
    trace1 = Scatter(x=np.arange(df.shape[0]), y=df.y_treat.T.values[0], name="treat")
    trace2 = Scatter(x=np.arange(df.shape[0]), y=df.y_control.T.values[0], name="control")
    data = [trace1, trace2]b
    layout = Layout(title="AUUC = {}".format(auuc), yaxis={"title": "Total Outcome"}, xaxis={"title": "{} Rank".format(score_name)})
    fig = Figure(data=data, layout=layout)
    iplot(fig)

    if output[0]:
        plot(fig, filename=file_names[0])

    # Avg Outcome sorted by Uplift Rank
    trace1 = Scatter(x=np.arange(df.shape[0]), y=df.y_treat_avg.T.values[0], name="treat")
    trace2 = Scatter(x=np.arange(df.shape[0]), y=df.y_control_avg.T.values[0], name="control")
    data = [trace1, trace2]
    layout = Layout(title="AUUC = {}".format(auuc), yaxis={"title": "Mean Outcome"}, xaxis={"title": "{} Rank".format(score_name)})
    fig = Figure(data=data, layout=layout)
    iplot(fig)

    if output[1]:
        plot(fig, filename=file_names[1])

    # Lift Curve sorted by Uplift Rank
    trace1 = Scatter(x=np.arange(df.shape[0]),
                     y=df.lift.T.values[0],
                     name="treat")
    trace2 = Scatter(x=np.arange(df.shape[0]), y=df.base_line.T.values[0], name="baseline")
    data = [trace1, trace2]
    layout = Layout(title="AUUC = {}".format(auuc), yaxis={"title": "Uplift"}, xaxis={"title": "{} Rank".format(score_name)})
    fig = Figure(data=data, layout=layout)
    iplot(fig)

    if output[2]:
        plot(fig, filename=file_names[2])

    # Lift Curve sorted by Uplift Score
    trace1 = Scatter(x=df.score.T.values[0], y=df.lift.T.values[0], name="treat")
    trace2 = Scatter(x=df.score.T.values[0], y=df.base_line.T.values[0], name="baseline")
    data = [trace1, trace2]
    layout = Layout(title="AUUC = {}".format(auuc), yaxis={"title": "Lift".format(score_name)},
                    xaxis={"title": "{}".format(score_name), "autorange": "reversed"})
    fig = Figure(data=data, layout=layout)
    iplot(fig)

    if output[3]:
        plot(fig, filename=file_names[3])
示例#15
0
def plotTemplate(start_x, start_y, length, width):
    '''
    Plots the data on the Plotly Framework.
    '''
    global scatter_write_stream, end_write_stream
    py.sign_in(plotly_username, plotly_api_key)
    tls.set_credentials_file(username=plotly_username,
                                 api_key=plotly_api_key)
    
    tls.set_credentials_file(stream_ids=plotly_streaming_keys)
    stream_ids = tls.get_credentials_file()['stream_ids']
    scatter_stream_id = stream_ids[0]
    scatter_data_stream = Stream(
        token=scatter_stream_id  # (!) link stream id to 'token' key
    )
    
    end_stream_id = stream_ids[1]
    end_data_stream = Stream(
        token=end_stream_id  # (!) link stream id to 'token' key
    )
    
    
    # Prepares the layout of the axis
    layout = Layout(
                showlegend=True,
                autosize=True,
                height=800,
                width=800,
                title="MAP",
                xaxis=XAxis(
                    zerolinewidth=4,
                    gridwidth=1,
                    showgrid=True,
                    zerolinecolor="#969696",
                    gridcolor="#bdbdbd",
                    linecolor="#636363",
                    mirror=True,
                    zeroline=False,
                    showline=True,
                    linewidth=6,
                    type="linear",
                    range=[0, length],
                    autorange=False,
                    autotick=False,
                    dtick=15,
                    tickangle=-45,
                    title="X co-ordinate"
                    ),
                yaxis=YAxis(
                    zerolinewidth=4,
                    gridwidth=1,
                    showgrid=True,
                    zerolinecolor="#969696",
                    gridcolor="#bdbdbd",
                    linecolor="#636363",
                    mirror=True,
                    zeroline=False,
                    showline=True,
                    linewidth=6,
                    type="linear",
                    range=[width, 0],
                    autorange=False,
                    autotick=False,
                    dtick=15,
                    tickangle=-45,
                    title="Y co-ordinate"
                    )
                )
    
    # Prepares the startPoint plot
    startData = Scatter(
        x=[start_x],
        y=[start_y],
        mode='markers',
        marker=Marker(color="red", size="10", symbol="triangle-left"),
        showlegend=False,
        name=mac,
        text=["Start point"],
        legendgroup=mac,
    )
    
    # Prepares the endPoint plot
    endData = Scatter(
        x=[],
        y=[],
        mode='markers',
        marker=Marker(color="red", size="10"),
        showlegend=False,
        name=mac,
        text=["End point"],
        legendgroup=mac,
        stream=end_data_stream
    )
    
    # Prepares the scatter data plot
    scatter_data = Scatter(
        x=[],
        y=[],
        mode='lines + text',
        text=[],
        name=mac,
        marker=Marker(color="red"),
        opacity="0.5",
        legendgroup=mac,
        stream=scatter_data_stream
    )
    
    data = Data([scatter_data, startData, endData])
    fig = Figure(data=data, layout=layout)
    py.plot(fig, filename='Sample Code For History Of Clients ')
    
    # Sets the stream to the specific scatters
    scatter_write_stream = py.Stream(scatter_stream_id)
    end_write_stream = py.Stream(end_stream_id)
def heatmap_iplot(self, **kwargs):
    """
    Plots heatmap in plotly interactive plot
    Parameters
    ----------
        traces: dict
            Data for the plot, with the format:
            "traces":  {"1": {"devices": '8019043',
                             "channel" : "PM_10"}
                        }      
        options: dict 
            Options including data processing prior to plot. Defaults in config.plot_def_opt
        formatting: dict
            Name of auxiliary electrode found in dataframe. Defaults in config.heatmap_def_fmt
    Returns
    -------
        Plotly figure
    """
    if config.framework == 'jupyterlab': renderers.default = config.framework

    if 'traces' not in kwargs:
        std_out('No traces defined', 'ERROR')
        return None
    else:
        traces = kwargs['traces']

    if 'options' not in kwargs:
        std_out('Using default options')
        options = config.plot_def_opt
    else:
        options = dict_fmerge(config.plot_def_opt, kwargs['options'])

    if 'formatting' not in kwargs:
        std_out('Using default formatting')
        formatting = config.heatmap_def_fmt['plotly']
    else:
        formatting = dict_fmerge(config.heatmap_def_fmt['plotly'],
                                 kwargs['formatting'])

    # Make it standard
    for trace in traces:
        if 'subplot' not in trace: traces[trace]['subplot'] = 1

    # Get dataframe
    df, subplots = prepare_data(self, traces, options)
    n_subplots = len(subplots)

    gskwags = {'frequency_hours': formatting['frequency_hours']}

    dfgb, labels, yaxis, channel = groupby_session(df, **gskwags)
    xticks = [
        i.strftime("%Y-%m-%d")
        for i in dfgb.resample(formatting['session']).mean().index
    ]

    # Data
    data = [
        Heatmap(
            z=dfgb[channel],
            x=dfgb.index.date,
            y=dfgb['session'],
            # colorscale=colorscale
        )
    ]

    layout = Layout(title=formatting['title'],
                    xaxis=dict(ticks=''),
                    yaxis=dict(ticks='',
                               categoryarray=labels,
                               autorange='reversed'))

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

    if options['show']: iplot(figure)

    return figure
示例#17
0
jvm_parser = ProfileParser(combined_file)
jvm_parser.manually_set_profiler('JVMProfiler')

pyspark_parser = ProfileParser(combined_file)
pyspark_parser.manually_set_profiler('pyspark')

jvm_maxima: Dict[str, float] = jvm_parser.get_maxima()
pyspark_maxima: Dict[str, float] = pyspark_parser.get_maxima()

print('JVM max values:')
print(jvm_maxima)
print('\nPySpark max values:')
print(pyspark_maxima)

# Plotting the graphs:
# Seeing all available metrices:
# print(jvm_parser.get_available_metrics())
# ['epochMillis', 'ScavengeCollTime', 'MarkSweepCollTime', 'MarkSweepCollCount', 'ScavengeCollCount', 'systemCpuLoad', 'processCpuLoad', 'nonHeapMemoryTotalUsed', 'nonHeapMemoryCommitted', 'heapMemoryTotalUsed', 'heapMemoryCommitted']
# print(pyspark_parser.get_available_metrics())
# ['epochMillis', 'pmem_rss', 'pmem_vms', 'cpu_percent']

data_points = list()
data_points.extend(jvm_parser.get_metrics(['systemCpuLoad', 'processCpuLoad']))
# Records from different profilers are in a single file so these IDs
data_points.extend(pyspark_parser.get_metrics(['cpu_percent_931'], id='931'))  # are used to collect to collect all PySpark records. They were the
                                                                               # process IDs when the code was profiled, present in every JSON records
                                                                               # outputted by PySpark profiler as value in 'pid' fields
fig = Figure(data=data_points)
plot(fig, filename='slacker-cpu.html')
示例#18
0
def Generate_3DModel(idList, labelList, percentage):

    network = pd.read_csv("network.csv")
    L = len(network['TF'])

    #Values=[network['importance'][k] for k in range(L)]

    #G=ig.Graph(Edges, directed=False)
    #layt=G.layout('kk', dim=3)

    G = Create_Graph(idList, labelList, percentage, netthreshold)
    N = len(list(G.node()))  #--> Numero de nodos
    V = list(G.node())  # lista con todos los nodos

    #Edges=[(network['TF'][k], network['target'][k]) for k in range(L)]
    Edges = list(G.edges())

    #layt=nx.spectral_layout(G,dim=3)

    #layt=nx.spring_layout(G, dim=3)
    #layt=nx.fruchterman_reingold_layout(G,dim=3)
    #layt=laytdict.values()

    #g=nx.Graph()
    #g.add_nodes_from(V)
    #g.add_edges_from(Edges)

    layt = nx.fruchterman_reingold_layout(G, dim=3)
    #layt = nx.circular_layout(G,scale=10,dim=3)
    #layt=nx.spring_layout(G,dim=3)
    laytN = list(layt.values())

    Xn = [laytN[k][0] for k in range(N)]  # x-coordinates of nodes
    Yn = [laytN[k][1] for k in range(N)]  # y-coordinates
    Zn = [laytN[k][2] for k in range(N)]  # z-coordinates
    Xe = []
    Ye = []
    Ze = []
    for e in Edges:
        Xe += [layt[e[0]][0], layt[e[1]][0],
               None]  # x-coordinates of edge ends
        Ye += [layt[e[0]][1], layt[e[1]][1], None]
        Ze += [layt[e[0]][2], layt[e[1]][2], None]

    trace1 = Scatter3d(x=Xe,
                       y=Ye,
                       z=Ze,
                       mode='lines',
                       line=Line(color='rgb(125,125,125)', width=1),
                       hoverinfo='none')
    trace2 = Scatter3d(x=Xn,
                       y=Yn,
                       z=Zn,
                       mode='markers+text',
                       textposition='top',
                       name='genes',
                       marker=Marker(symbol='dot',
                                     size=6,
                                     color='#6959CD',
                                     colorscale='Viridis',
                                     line=Line(color='rgb(50,50,50)',
                                               width=1)),
                       text=V,
                       hoverinfo='text')

    #for node, adjacencies in enumerate(G.adjacency()):
    #trace2['marker']['color'].append(len(adjacencies))
    #node_info = 'Number of connections: '+str(len(adjacencies))
    #trace2['text'].append(node_info)

    axis = dict(showbackground=False,
                showline=False,
                zeroline=False,
                showgrid=False,
                showticklabels=False,
                title='')

    fig = Figure(data=Data([trace1, trace2]),
                 layout=Layout(
                     title="Network (3D visualization)",
                     width=1000,
                     height=1000,
                     showlegend=False,
                     scene=Scene(
                         xaxis=XAxis(axis),
                         yaxis=YAxis(axis),
                         zaxis=ZAxis(axis),
                     ),
                     margin=Margin(t=100),
                     hovermode='closest',
                     annotations=Annotations([
                         Annotation(showarrow=False,
                                    text="",
                                    xref='paper',
                                    yref='paper',
                                    x=0,
                                    y=0.1,
                                    xanchor='left',
                                    yanchor='bottom',
                                    font=Font(size=14))
                     ]),
                 ))

    py.iplot(fig, filename='networkx3D')
示例#19
0
BLANK_SUBPLOTS = Figure(
    data=Data([
        Scatter(x=[0.0, 1.0],
                y=[1.0, 2.0],
                name='_line0',
                mode='lines',
                line=Line(dash='solid', color='#0000FF', width=1.0, opacity=1),
                xaxis='x1',
                yaxis='y1')
    ]),
    layout=Layout(
        width=640,
        height=480,
        autosize=False,
        margin=Margin(l=168, r=63, b=47, t=47, pad=0),
        hovermode='closest',
        showlegend=False,
        xaxis1=XAxis(domain=[0.0, 0.13513513513513517],
                     range=[0.0, 1.0],
                     type='linear',
                     showline=True,
                     ticks='inside',
                     nticks=6,
                     showgrid=False,
                     zeroline=False,
                     tickfont=Font(size=12.0),
                     anchor='y1',
                     side='bottom',
                     mirror='ticks'),
        xaxis2=XAxis(domain=[0.0, 0.13513513513513517],
                     range=[0.0, 1.0],
                     type='linear',
                     showline=True,
                     ticks='inside',
                     nticks=6,
                     showgrid=False,
                     zeroline=False,
                     tickfont=Font(size=12.0),
                     anchor='y2',
                     side='bottom',
                     mirror='ticks'),
        xaxis3=XAxis(domain=[0.0, 0.13513513513513517],
                     range=[0.0, 1.0],
                     type='linear',
                     showline=True,
                     ticks='inside',
                     nticks=6,
                     showgrid=False,
                     zeroline=False,
                     tickfont=Font(size=12.0),
                     anchor='y3',
                     side='bottom',
                     mirror='ticks'),
        xaxis4=XAxis(domain=[0.2162162162162162, 1.0],
                     range=[0.0, 1.0],
                     type='linear',
                     showline=True,
                     ticks='inside',
                     nticks=6,
                     showgrid=False,
                     zeroline=False,
                     tickfont=Font(size=12.0),
                     anchor='y4',
                     side='bottom',
                     mirror='ticks'),
        xaxis5=XAxis(domain=[0.2162162162162162, 0.56756756756756754],
                     range=[0.0, 1.0],
                     type='linear',
                     showline=True,
                     ticks='inside',
                     nticks=6,
                     showgrid=False,
                     zeroline=False,
                     tickfont=Font(size=12.0),
                     anchor='y5',
                     side='bottom',
                     mirror='ticks'),
        xaxis6=XAxis(domain=[0.2162162162162162, 0.78378378378378377],
                     range=[0.0, 1.0],
                     type='linear',
                     showline=True,
                     ticks='inside',
                     nticks=6,
                     showgrid=False,
                     zeroline=False,
                     tickfont=Font(size=12.0),
                     anchor='y6',
                     side='bottom',
                     mirror='ticks'),
        xaxis7=XAxis(domain=[0.64864864864864857, 1.0],
                     range=[0.0, 1.0],
                     type='linear',
                     showline=True,
                     ticks='inside',
                     nticks=6,
                     showgrid=False,
                     zeroline=False,
                     tickfont=Font(size=12.0),
                     anchor='y7',
                     side='bottom',
                     mirror='ticks'),
        xaxis8=XAxis(domain=[0.8648648648648648, 1.0],
                     range=[0.0, 1.0],
                     type='linear',
                     showline=True,
                     ticks='inside',
                     nticks=6,
                     showgrid=False,
                     zeroline=False,
                     tickfont=Font(size=12.0),
                     anchor='y8',
                     side='bottom',
                     mirror='ticks'),
        yaxis1=YAxis(domain=[0.82758620689655182, 1.0],
                     range=[1.0, 2.2000000000000002],
                     type='linear',
                     showline=True,
                     ticks='inside',
                     nticks=8,
                     showgrid=False,
                     zeroline=False,
                     tickfont=Font(size=12.0),
                     anchor='x1',
                     side='left',
                     mirror='ticks'),
        yaxis2=YAxis(domain=[0.55172413793103459, 0.72413793103448276],
                     range=[0.0, 1.0],
                     type='linear',
                     showline=True,
                     ticks='inside',
                     nticks=6,
                     showgrid=False,
                     zeroline=False,
                     tickfont=Font(size=12.0),
                     anchor='x2',
                     side='left',
                     mirror='ticks'),
        yaxis3=YAxis(domain=[0.0, 0.44827586206896558],
                     range=[0.0, 1.0],
                     type='linear',
                     showline=True,
                     ticks='inside',
                     nticks=6,
                     showgrid=False,
                     zeroline=False,
                     tickfont=Font(size=12.0),
                     anchor='x3',
                     side='left',
                     mirror='ticks'),
        yaxis4=YAxis(domain=[0.82758620689655182, 1.0],
                     range=[0.0, 1.0],
                     type='linear',
                     showline=True,
                     ticks='inside',
                     nticks=6,
                     showgrid=False,
                     zeroline=False,
                     tickfont=Font(size=12.0),
                     anchor='x4',
                     side='left',
                     mirror='ticks'),
        yaxis5=YAxis(domain=[0.27586206896551724, 0.72413793103448276],
                     range=[0.0, 1.0],
                     type='linear',
                     showline=True,
                     ticks='inside',
                     nticks=6,
                     showgrid=False,
                     zeroline=False,
                     tickfont=Font(size=12.0),
                     anchor='x5',
                     side='left',
                     mirror='ticks'),
        yaxis6=YAxis(domain=[0.0, 0.17241379310344834],
                     range=[0.0, 1.0],
                     type='linear',
                     showline=True,
                     ticks='inside',
                     nticks=6,
                     showgrid=False,
                     zeroline=False,
                     tickfont=Font(size=12.0),
                     anchor='x6',
                     side='left',
                     mirror='ticks'),
        yaxis7=YAxis(domain=[0.27586206896551724, 0.72413793103448276],
                     range=[0.0, 1.0],
                     type='linear',
                     showline=True,
                     ticks='inside',
                     nticks=6,
                     showgrid=False,
                     zeroline=False,
                     tickfont=Font(size=12.0),
                     anchor='x7',
                     side='left',
                     mirror='ticks'),
        yaxis8=YAxis(domain=[0.0, 0.17241379310344834],
                     range=[0.0, 1.0],
                     type='linear',
                     showline=True,
                     ticks='inside',
                     nticks=6,
                     showgrid=False,
                     zeroline=False,
                     tickfont=Font(size=12.0),
                     anchor='x8',
                     side='left',
                     mirror='ticks')))
示例#20
0
文件: graph.py 项目: JLoureir0/ARSI
    node_trace['y'].append(y)

for node, adjacencies in G.adjacency():
    node_trace['marker']['color'].append(len(adjacencies))
    node_info = '# of connections: ' + str(len(adjacencies))
    node_trace['text'].append(node_info)

fig = Figure(data=Data([edge_trace, node_trace]),
             layout=Layout(title='<br>Soccer players that played together',
                           titlefont=dict(size=16),
                           showlegend=False,
                           hovermode='closest',
                           margin=dict(b=20, l=5, r=5, t=40),
                           annotations=[
                               dict(text="",
                                    showarrow=False,
                                    xref="paper",
                                    yref="paper",
                                    x=0.005,
                                    y=-0.002)
                           ],
                           xaxis=XAxis(showgrid=False,
                                       zeroline=False,
                                       showticklabels=False),
                           yaxis=YAxis(showgrid=False,
                                       zeroline=False,
                                       showticklabels=False)))

py.plot(fig, filename='networkx.html')

soccer_db.close()
示例#21
0
        node_trace['marker']['color'].append(len(adjacencies))
        node_info = '# of connections: ' + str(len(adjacencies))
        node_trace['text'].append(node_info)
elif markers_are_names:
    nodes = G.nodes()
    for node, adjacencies in enumerate(G.adjacency_list()):
        node_trace['marker']['color'].append(len(adjacencies))
        node_info = nodes[node]
        node_trace['text'].append(node_info)

fig = Figure(
    data=Data([edge_trace, node_trace]),
    layout=Layout(
        title='<br>Network graph made with Python',
        titlefont=dict(size=16),
        showlegend=False,
        hovermode='closest',
        margin=dict(b=20, l=5, r=5, t=40),
        # annotations=[dict(
        #     text="Python code: <a href='https://plot.ly/ipython-notebooks/network-graphs/'> https://plot.ly/ipython-notebooks/network-graphs/</a>",
        #     showarrow=False,
        #     xref="paper", yref="paper",
        #     x=0.005, y=-0.002)],
        xaxis=XAxis(showgrid=False, zeroline=False, showticklabels=False),
        yaxis=YAxis(showgrid=False, zeroline=False, showticklabels=False)))

offline.plot(fig, filename='networkx.html')

elapsed_time = time.time() - start_time
logger.debug('elapsed time %d seconds', elapsed_time)
示例#22
0
def describe_proximity(window_seconds=5):
    """
    Poll the DB at a given interval to get the minimum proximity, average proximity
    and the variance in the mean (to get a feel for range sampled).

    Args:
        interval (int): Inteval at which to update
        window_ms (int): Window range for averaging (in milliseconds)
    """
    dt = timedelta(seconds=window_seconds)
    now_ = datetime.now()
    start = (now_ - dt).strftime(dtfmt)
    stop = now_.strftime(dtfmt)
    conn = Connection(config['hbase'], port=int(config['thrift']))
    tab = conn.table(str.encode(config['prox_table']))
    #dct = {k: v for k, v in tab.scan(row_start=pk01+start, row_stop=pk01+stop)}
    avg_ = []
    min_ = 0
    for pk in pks:
        dct = {
            k: v
            for k, v in tab.scan(row_start=pk + start, row_stop=pk + stop)
        }
        if len(dct) > 0:
            df = pd.DataFrame.from_dict(dct, orient="index").reset_index()
            df[b'spatial:dr'] = df[b'spatial:dr'].astype(float)
            avg_.append(df[b'spatial:dr'].mean())
            min_ += df[df[b'spatial:dr'] < 10].shape[0]

    time.append(str(now_))
    miny.append(min_)
    try:
        avgy.append(sum(avg_) / len(avg_))
    except Exception:
        avgy.append(np.nan)
    avgline = Scatter(x=list(time),
                      y=list(avgy),
                      type='scatter',
                      mode='lines',
                      name='Mean')
    minline = Scatter(x=list(time),
                      y=list(miny),
                      type='scatter',
                      mode='lines',
                      name='< 10',
                      yaxis="y2")
    #trace = [{'x': time, 'y': avgy, 'type': "scatter", 'mode': "lines", 'name': 'Avg'},
    #         {'x': time, 'y': miny, 'type': "scatter", 'mode': "lines", 'name': 'Min'}]
    layout = {
        'height': 620,
        'yaxis': {
            'title': "Average Proximity (m)",
            'side': "left"
        },
        'yaxis2': {
            'title': 'Within 10 Meters (count)',
            'side': "right",
            'overlaying': "y"
        }
    }
    return Figure(data=[avgline, minline], layout=layout)
示例#23
0
def plotLinesForBaseline(fileNameParam):
    import csv
    import plotly.plotly as py
    import plotly.graph_objs as go
    from plotly.graph_objs import Layout, YAxis, Figure, Margin, Font, XAxis

    xList = []
    baselineAct = []
    optimizedAct = []
    baselinePass = []
    optimizedPass = []

    fileToRead = open(fileNameParam, "rU")
    try:
        fileReader = csv.reader(fileToRead,
                                delimiter=',',
                                dialect=csv.excel_tab)
        next(fileReader, None)
        for rowVar in fileReader:
            xList.append(rowVar[0])
            baselineAct.append(rowVar[1])
            optimizedAct.append(rowVar[3])
            baselinePass.append(rowVar[2])
            optimizedPass.append(rowVar[4])
    finally:
        fileToRead.close()

    baselineActTrace = go.Scatter(x=xList,
                                  y=baselineAct,
                                  mode='lines+markers',
                                  name='Baseline-Undetected Active Errors')
    optimizedActTrace = go.Scatter(x=xList,
                                   y=optimizedAct,
                                   mode='lines+markers',
                                   name='Optimized-Undetected Active Errors')
    baselinePassTrace = go.Scatter(x=xList,
                                   y=baselinePass,
                                   mode='lines+markers',
                                   name='Baseline-Undetected Passive Errors')
    optimizedPassTrace = go.Scatter(x=xList,
                                    y=optimizedPass,
                                    mode='lines+markers',
                                    name='Optimized-Undetected Passive Errors')
    ActTraces = [baselineActTrace, optimizedActTrace]
    PassTraces = [baselinePassTrace, optimizedPassTrace]
    layout_ActError = Layout(
        title=
        'Comparing Optimized Values with Baseline Values : Undetected Active Errors',
        titlefont=Font(family='Times New Roman', size=18, color='#000000'),
        xaxis=XAxis(showgrid=True,
                    showline=True,
                    title='Iterations of DE',
                    autorange=False,
                    range=[0, 1100],
                    titlefont=Font(family='Times New Roman',
                                   size=12,
                                   color='#000000')),
        yaxis=YAxis(showgrid=True,
                    showline=True,
                    title='Optimized Value',
                    range=[0, 1500],
                    autorange=False,
                    titlefont=Font(family='Times New Roman',
                                   size=12,
                                   color='#000000')),
        width=800,
        height=600,
        margin=Margin(l=140, r=40, b=50, t=80),
    )

    layout_PassError = Layout(
        title=
        'Comparing Optimized Values with Baseline Values : Undetected Passive Errors',
        titlefont=Font(family='Times New Roman', size=18, color='#000000'),
        xaxis=XAxis(showgrid=True,
                    showline=True,
                    title='Iterations of DE',
                    autorange=False,
                    range=[0, 1100],
                    titlefont=Font(family='Times New Roman',
                                   size=12,
                                   color='#000000')),
        yaxis=YAxis(showgrid=True,
                    showline=True,
                    title='Optimized Value',
                    range=[0, 5],
                    autorange=False,
                    titlefont=Font(family='Times New Roman',
                                   size=12,
                                   color='#000000')),
        width=800,
        height=600,
        margin=Margin(l=140, r=40, b=50, t=80),
    )

    fig_UnActErr = Figure(data=ActTraces, layout=layout_ActError)
    fileNameParam = "UnActError.png"
    py.image.save_as(fig_UnActErr, fileNameParam)

    fig_UnPassErr = Figure(data=PassTraces, layout=layout_PassError)
    fileNameParam = "UnPassError.png"
    py.image.save_as(fig_UnPassErr, fileNameParam)

    return "DONE!"
示例#24
0
def to_templated(fig, skip=("title", "text")):
    """
    Return a copy of a figure where all styling properties have been moved
    into the figure's template.  The template property of the resulting figure
    may then be used to set the default styling of other figures.

    Parameters
    ----------
    fig
        Figure object or dict representing a figure
    skip
        A collection of names of properties to skip when moving properties to
        the template. Defaults to ('title', 'text') so that the text
        of figure titles, axis titles, and annotations does not become part of
        the template

    Examples
    --------
    Imports
    >>> import plotly.graph_objs as go
    >>> import plotly.io as pio

    Construct a figure with large courier text
    >>> fig = go.Figure(layout={'title': 'Figure Title',
    ...                         'font': {'size': 20, 'family': 'Courier'}})
    >>> fig
    Figure({
        'data': [],
        'layout': {'title': 'Figure Title',
                   'font': {'family': 'Courier', 'size': 20}}
    })

    Convert to a figure with a template. Note how the 'font' properties have
    been moved into the template property.
    >>> templated_fig = pio.to_templated(fig)
    >>> templated_fig
    Figure({
        'data': [],
        'layout': {'title': 'Figure Title',
                   'template': {'layout': {'font': {'family': 'Courier',
                                                    'size': 20}}}}
    })

    Next create a new figure with this template

    >>> fig2 = go.Figure(layout={
    ...     'title': 'Figure 2 Title',
    ...     'template': templated_fig.layout.template})
    >>> fig2
    Figure({
        'data': [],
        'layout': {'title': 'Figure 2 Title',
                   'template': {'layout': {'font': {'family': 'Courier',
                                                    'size': 20}}}}
    })

    The default font in fig2 will now be size 20 Courier.

    Next, register as a named template...
    >>> pio.templates['large_courier'] = templated_fig.layout.template

    and specify this template by name when constructing a figure.

    >>> go.Figure(layout={
    ...     'title': 'Figure 3 Title',
    ...     'template': 'large_courier'})
    Figure({
        'data': [],
        'layout': {'title': 'Figure 3 Title',
                   'template': {'layout': {'font': {'family': 'Courier',
                                                    'size': 20}}}}
    })

    Finally, set this as the default template to be applied to all new figures

    >>> pio.templates.default = 'large_courier'
    >>> go.Figure(layout={'title': 'Figure 4 Title'})
    Figure({
        'data': [],
        'layout': {'title': 'Figure 4 Title',
                   'template': {'layout': {'font': {'family': 'Courier',
                                                    'size': 20}}}}
    })

    Returns
    -------
    figure
    """

    # process fig
    from plotly.basedatatypes import BaseFigure
    from plotly.graph_objs import Figure

    if not isinstance(fig, BaseFigure):
        fig = Figure(fig)

    # Process skip
    if not skip:
        skip = set()
    else:
        skip = set(skip)

    # Always skip uids
    skip.add("uid")

    # Initialize templated figure with deep copy of input figure
    templated_fig = copy.deepcopy(fig)

    # Handle layout
    walk_push_to_template(templated_fig.layout,
                          templated_fig.layout.template.layout,
                          skip=skip)

    # Handle traces
    trace_type_indexes = {}
    for trace in list(templated_fig.data):
        template_index = trace_type_indexes.get(trace.type, 0)

        # Extend template traces if necessary
        template_traces = list(templated_fig.layout.template.data[trace.type])
        while len(template_traces) <= template_index:
            # Append empty trace
            template_traces.append(trace.__class__())

        # Get corresponding template trace
        template_trace = template_traces[template_index]

        # Perform push properties to template
        walk_push_to_template(trace, template_trace, skip=skip)

        # Update template traces in templated_fig
        templated_fig.layout.template.data[trace.type] = template_traces

        # Update trace_type_indexes
        trace_type_indexes[trace.type] = template_index + 1

    # Remove useless trace arrays
    for trace_type in templated_fig.layout.template.data:
        traces = templated_fig.layout.template.data[trace_type]
        is_empty = [
            trace.to_plotly_json() == {
                "type": trace_type
            } for trace in traces
        ]
        if all(is_empty):
            templated_fig.layout.template.data[trace_type] = None

    return templated_fig
示例#25
0
"""
Created on Fri Nov 16 08:41:06 2018

@author: Ping-Chen Lin 
"""

import numpy as np
import plotly.offline as pyo
from plotly.graph_objs import Figure, Data
#設定網頁離線的畫圖介面
pyo.offline.init_notebook_mode()
#設定樣本數
N = 1000
x = np.linspace(0, 1, N)
y = np.random.randn(N) + 5
#設定x, y資料與線上的格式
trace1 = {
    'type': 'scatter',
    'x': x,
    'y': y,
    'name': 'simple trace',
    'mode': 'markers'
}
#設定x,y軸與title名稱
layout = {'title': '分佈圖', 'xaxis': {'title': '亂數'}, 'yaxis': {'title': '個數'}}
#設定trace資料到data屬性
data = Data([trace1])
#帶入Figure畫圖函數包含資料與版面配置
fig = Figure(data=data, layout=layout)
#畫圖
pyo.plot(fig)
import plotly.plotly as py
from plotly.graph_objs import Scatter, Layout, Figure
import time
import readadc

username = '******'
api_key = 'd6k9457d4a'
stream_token = 'lx3pcmffpp'

py.sign_in(username, api_key)

trace1 = Scatter(x=[], y=[], stream=dict(token=stream_token, maxpoints=200))

layout = Layout(title='Raspberry Pi Streaming Sensor Data')

fig = Figure(data=[trace1], layout=layout)

print py.plot(fig, filename='Raspberry Pi Streaming Example Values')

# temperature sensor connected channel 0 of mcp3008
sensor_pin = 0
readadc.initialize()

i = 0
stream = py.Stream(stream_token)
stream.open()

#the main sensor reading loop
while True:
    sensor_data = readadc.readadc(sensor_pin, readadc.PINS.SPICLK,
                                  readadc.PINS.SPIMOSI, readadc.PINS.SPIMISO,
示例#27
0
    def graph_08(self):
        legend = {"title":"Número de acessos, postagens e curtidas agrupados por estudante",
                    "xaxis":"",
                    "yaxis":"",
                    "columns":{1:"Curtidas", 2:"Postagens", 3:"Acessos"},
                    'hovertext':{1:' curtida(s)',2:' postagem(s)',3:' acesso(s)'}
                }
        if (self._language == "en"):
            legend = {"title":"Number of access, posts and likes grouped by student",
                        "xaxis":"",
                        "yaxis":"",
                        "columns":{1:"Likes", 2:"Posts", 3:"Access"},
                        'hovertext':{1:' like(s)',2:' post(s)',3:' access'}
                    }
        # https://plot.ly/python/bubble-charts/
        # https://plot.ly/python/reference/#layout-xaxis
        # https://plot.ly/python/axes/#subcategory-axes
        df = self.DATASET.sort_values(by=[self.DATASET.columns[0]])
        max_value=0
        for i in range(0, len(df)): #Take the max value in whole dataframe
            if max(df.iloc[:,1:len(df.columns)].values[i]) > max_value:
                max_value = max(df.iloc[:,1:len(df.columns)].values[i])
        
        sizeref = 0.2

        trace = []        
        
        for i in range(0, len(df)):
            trace.append(
                Scatter(
                    x=[df.iloc[i,0]]*(len(df.columns[1:])), #student
                    y=[legend["columns"][k] for k in range (1,4)],
                    hovertext=['<b>'+df.iloc[i,0]+'</b><br>'+str(df.iloc[i,j])+legend['hovertext'][j] for j in range(1,len(df.columns)-1)],
                    hoverinfo='text',
                    mode='markers',
                    # name=df.iloc[i,0], #student name
                    # text = df.iloc[i,1:len(df.columns)].values.tolist(),
                    marker=dict(
                        symbol='circle',
                        sizemode='area',
                        sizeref=sizeref,
                        size=df.iloc[i,1:len(df.columns)-1].values.tolist(),
                        color = 'rgb(0,0,255)',
                        line=dict(
                            width=2
                        )
                    )
                )
            )

        layout = Layout(
            title = legend["title"],
            hovermode = "closest",
            showlegend = False,
            xaxis = dict(
                title = legend["xaxis"],
                titlefont=dict(
                    # family='Arial, sans-serif',
                    # size=18,
                    color='rgb(180,180,180)',
                ),
                autorange = False,
                # categoryorder = "category ascending",
                # domain = [0, 1],
                fixedrange = False,
                range = [-1, len(self.DATASET)],
                rangemode = "normal",
                showline = True,                
                type = "category"
            ),
            yaxis = dict(
                title = legend["yaxis"],
                titlefont=dict(
                    # family='Arial, sans-serif',
                    # size=18,
                    color='rgb(180,180,180)',
                ),
                autorange = False,
                # categoryorder = "category ascending",
                # domain = [0, 1],
                fixedrange = False,
                range = [-1, len(self.DATASET.columns[1:])],
                rangemode = "normal",
                showline = True,
                type = "category"
            )
        )

        data = trace
        fig=Figure(data=data, layout=layout)        
        if self._type_result == "jupyter-notebook":
            iplot(fig, filename='Scatter')
        elif self._type_result == "dash":            
            return dcc.Graph(
                id='V003@8',
                figure=fig
            )
        elif self._type_result == "flask":            
            modeBarButtonsToRemove = ['toImage', 'sendDataToCloud', 'hoverCompareCartesian', 'lasso2d', 'hoverClosestCartesian', 'toggleHover', 'hoverClosest3d', 'hoverClosestGeo', 'hoverClosestGl2d', 'hoverClosestPie']
            config = {"displaylogo": False, "responsive": True, "displayModeBar": True, "modeBarButtonsToRemove": modeBarButtonsToRemove}
            return {"id":"V003@8","layout":json.dumps({"data": data, "layout": layout, "config": config}, cls=PlotlyJSONEncoder)}
示例#28
0
    def GET(self):
        super(TempHumiGraph, self).GET()
        get_data = web.input(range_days='7', action='read')
        range_days = get_data.range_days
        coop = Coop()
        db = get_db(coop)
        data = Data([
            scatter_graph_timeseries(db,
                                     'AMBIENT_TEMP',
                                     'Air Temperature',
                                     'red',
                                     range_days=range_days),
            scatter_graph_timeseries(db,
                                     'WATER_TEMP',
                                     'Water Temperature',
                                     'blue',
                                     range_days=range_days),
            scatter_graph_timeseries(db,
                                     'AMBIENT_HUMI',
                                     'Humidity',
                                     'green',
                                     width=1,
                                     yaxis2=True,
                                     range_days=range_days),
        ])
        min_x = min(min(data[0].x), min(data[1].x), min(data[2].x))
        max_x = max(max(data[0].x), max(data[1].x), max(data[2].x))
        temp_min = coop.config['Water']['HEATER_TEMP_RANGE'][1]
        temp_max = coop.config['AmbientTempHumi']['TEMP_FAN']
        layout = Layout(title='Temperature & Humidity',
                        height=600,
                        xaxis={
                            'title': 'Date',
                        },
                        yaxis={
                            'title': 'Temperature (F)',
                        },
                        yaxis2={
                            'title': 'Humidity (%)',
                            'type': 'linear',
                            'range': [0, 100],
                            'fixedrange': True,
                            'overlaying': 'y',
                            'side': 'right',
                            'showgrid': False,
                        },
                        shapes=[
                            {
                                'type': 'line',
                                'x0': min_x,
                                'y0': temp_max,
                                'x1': max_x,
                                'y1': temp_max,
                                'line': {
                                    'color': 'red',
                                    'width': 1,
                                    'dash': 'dot',
                                },
                            },
                            {
                                'type': 'line',
                                'x0': min_x,
                                'y0': temp_min,
                                'x1': max_x,
                                'y1': temp_min,
                                'line': {
                                    'color': 'blue',
                                    'width': 1,
                                    'dash': 'dot',
                                },
                            },
                        ])
        figure = Figure(data=data, layout=layout)
        graph = plot(figure, auto_open=False, output_type='div')

        return render.temp_humi_graph(
            coop.status,
            graph,
            range_days,
            coop.config['Webcam']['URL'],
        )
示例#29
0
    def graph_02(self):
        legend = {"title":"Número de acessos, postagens e curtidas agrupados por estudante",
                    "xaxis":"",
                    "yaxis":"Número de acessos, postagens e curtidas",
                    "columns":{1:"Curtidas", 2:"Postagens", 3:"Acessos"},
                    'hovertext':{1:' curtida(s)',2:' postagem(s)',3:' acesso(s)'}
                }
        if (self._language == "en"):
            legend = {"title":"Number of access, posts and likes grouped by student",
                        "xaxis":"",
                        "yaxis":"Number of access, posts and likes",
                        "columns":{1:"Likes", 2:"Posts", 3:"Access"},
                        'hovertext':{1:' like(s)',2:' post(s)',3:' access'}
                    }
        
        df = self.DATASET.iloc[:,0:len(self.DATASET.columns[1:])]
        trace = []
        for i in range(1,len(df.columns)): 
            trace.append(Bar(
                    x=df[df.columns[0]].values,
                    y=df.iloc[:,i].values,
                    hovertext=['<b>'+df.iloc[j,0]+'</b><br>'+str(df.iloc[j,i])+legend['hovertext'][i] for j in range(len(df))],
                    hoverinfo='text',
                    name=legend['columns'][i]
            ))

        data = trace
        layout = Layout(
                title=legend["title"],
                xaxis=dict(
                    title = legend["xaxis"],
                    titlefont=dict(
                        # family='Arial, sans-serif',
                        # size=18,
                        color='rgb(180,180,180)',
                    ),
                ),
                yaxis=dict(
                    title = legend["yaxis"],
                    titlefont=dict(
                        # family='Arial, sans-serif',
                        # size=18,
                        color='rgb(180,180,180)',
                    ),
                    showticklabels=True,
                    tick0=0,
                    dtick=5,
        #             ticklen=4,
        #             tickwidth=4,
                    exponentformat='e',
                    showexponent='all',
                    gridcolor='#bdbdbd',
        #             range=[0, 4.1]
                )
            )

        fig = Figure(data=data, layout=layout)
        if self._type_result == "jupyter-notebook":
            iplot(fig, filename='Bar')
        elif self._type_result == "dash":            
            return dcc.Graph(
                id='V003@2',
                figure=fig
            )
        elif self._type_result == "flask":            
            modeBarButtonsToRemove = ['toImage', 'sendDataToCloud', 'hoverCompareCartesian', 'lasso2d', 'hoverClosestCartesian', 'toggleHover', 'hoverClosest3d', 'hoverClosestGeo', 'hoverClosestGl2d', 'hoverClosestPie']
            config = {"displaylogo": False, "responsive": True, "displayModeBar": True, "modeBarButtonsToRemove": modeBarButtonsToRemove}
            return {"id":"V003@2","layout":json.dumps({"data": data, "layout": layout, "config": config}, cls=PlotlyJSONEncoder)}
示例#30
0
It's not worth the trouble (it seems) to create bona-fide Plotly presentations
as a way of showing multiple plots (in serial, not subplot, format).
The end quality seems pretty bad and you are forced to do everything online.
Thus, just generating multiple html files seems preferable.
"""
import plotly.presentation_objs as pres  # type: ignore
import plotly.plotly as py  # type: ignore
from plotly.graph_objs import Figure, Bar, Scatter  # type: ignore

trace1 = Scatter(x=[0, 1], y=[0, 0.5])
trace2 = Scatter(x=[0, 1], y=[0, -0.5])
trace3 = Scatter(x=[0, 1], y=[0, 1])
trace4 = Bar(x=['cats', 'dogs'], y=[2, 2.1])
trace5 = Bar(x=['rats', 'cats'], y=[1, 5])
datas = [[trace1], [trace2], [trace3], [trace4], [trace5]]
figs = [Figure(data=data, layout={'autosize': True}) for data in datas]

plot_urls = [
    py.plot(fig, filename='temp%d' % i, auto_open=False)
    for i, fig in enumerate(figs)
]

print('plot_urls ', plot_urls)

markdown_string = """
# slide 1
There is only one slide.
Plotly(https://plot.ly/~ksb_stanford/100)
---
# slide 2
Again, another slide on this page.