Python >> Tutoriel Python >  >> Python Tag >> Bokeh

Python :Comment obtenir des données à partir de pinceaux liés dans mlpd3, Bokeh, Plotly ?

Vous pouvez obtenir les données sélectionnées à partir d'un graphique Plotly en utilisant le nouveau cadre Dash de Plotly.

Il y a un exemple dans les docs ici sous "Graph Crossfiltering" https://plot.ly/dash/getting-started-part-2

J'ai collé l'exemple complet ci-dessous juste pour la préservation de l'historique.

Dans chacun des rappels ci-dessous, vous avez accès aux points sélectionnés, aux points sur lesquels vous venez de survoler ou aux points sur lesquels vous venez de cliquer. Cette application affiche simplement les valeurs des points dans l'application, mais vous pouvez faire n'importe quoi avec les points (par exemple, calculer autre chose).

import dash
from dash.dependencies import Input, Output
import dash_core_components as dcc
import dash_html_components as html
import json

app = dash.Dash(__name__)

app.layout = html.Div([
    dcc.Graph(
        id='basic-interactions',
        figure={
            'data': [
                {
                    'x': [1, 2, 3, 4],
                    'y': [4, 1, 3, 5],
                    'text': ['a', 'b', 'c', 'd'],
                    'customdata': ['c.a', 'c.b', 'c.c', 'c.d'],
                    'name': 'Trace 1',
                    'mode': 'markers',
                    'marker': {'size': 12}
                },
                {
                    'x': [1, 2, 3, 4],
                    'y': [9, 4, 1, 4],
                    'text': ['w', 'x', 'y', 'z'],
                    'customdata': ['c.w', 'c.x', 'c.y', 'c.z'],
                    'name': 'Trace 2',
                    'mode': 'markers',
                    'marker': {'size': 12}
                }
            ]
        }
    ),

    html.Div([
        dcc.Markdown("""
            **Hover Data**

            Mouse over values in the graph.
        """.replace('   ', '')),
        html.Pre(id='hover-data')
    ], style=styles['column']),

    html.Div([
        dcc.Markdown("""
            **Click Data**

            Click on points in the graph.
        """.replace('    ', '')),
        html.Pre(id='click-data'),
    ], style=styles['column']),

    html.Div([
        dcc.Markdown("""
            **Selection Data**

            Choose the lasso or rectangle tool in the graph's menu
            bar and then select points in the graph.
        """.replace('    ', '')),
        html.Pre(id='selected-data'),
    ])
])


@app.callback(
    Output('hover-data', 'children'),
    [Input('basic-interactions', 'hoverData')])
def display_hover_data(hoverData):
    #
    # This is where you can access the hover data
    # This function will get called automatically when you hover over points
    # hoverData will be equal to an object with that data
    # You can compute something off of this data, and return it to the front-end UI
    # 


    return json.dumps(hoverData, indent=2)


@app.callback(
    Output('click-data', 'children'),
    [Input('basic-interactions', 'clickData')])
def display_click_data(clickData):
    # Similarly for data when you click on a point
    return json.dumps(clickData, indent=2)


@app.callback(
    Output('selected-data', 'children'),
    [Input('basic-interactions', 'selectedData')])
def display_selected_data(selectedData):
    # Similarly for data when you select a region
    return json.dumps(selectedData, indent=2)


if __name__ == '__main__':
    app.run_server(debug=True)