2017-06-13 87 views
7

使用下面的代碼,我可以得到一個2x2圖形和4個圖。使用畫筆,我可以選擇一些數據點。我的問題是如何將選定的數據點作爲JSON數組或CVS。此代碼使用mlpd3,但散景可以使用畫筆進行類似選擇。但是沒有選擇數據點的示例。我試圖將選定的數據作爲對象繼續使用python進行處理。在單元中看到數據會很高興。Python:如何從mlpd3,Bokeh,Plotly中鏈接的筆刷中獲取數據?

%matplotlib inline 
import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib as mat 
import mpld3 

mpld3.enable_notebook() 


from mpld3 import plugins 

fig, ax = plt.subplots(2, 2, figsize=(10, 8)) 
fig.subplots_adjust(hspace=0.1, wspace=0.1) 
ax = ax[::-1] 

X = np.random.normal(size=(2, 100)) 
for i in range(2): 
    for j in range(2): 
     ax[i, j].xaxis.set_major_formatter(plt.NullFormatter()) 
     ax[i, j].yaxis.set_major_formatter(plt.NullFormatter()) 
     points = ax[i, j].scatter(X[j], X[i]) 

plugins.connect(fig, plugins.LinkedBrush(points)) 

背景虛化在CustomJS對於選擇類似的行爲

http://bokeh.pydata.org/en/latest/docs/user_guide/interaction/callbacks.html#userguide-interaction-jscallbacks-customjs-interactions

哪一個更容易提取所選擇的項目 - 將工作..如果有Plotly的解決方案,這也將工作。

+2

在mpld3中沒有辦法做到這一點:mpld3的輸出是一個靜態的javascript可視化,它不需要(或者知道)Python後端。出於這個原因,前端和後端之間的這種通信需要幾乎完全重寫庫。我建議使用Bokeh。 – jakevdp

+0

@jakevdp謝謝!改變問題。 – Merlin

回答

1

這是IPython中之外,但你可以結合d3.jsjquery運行flaskdjango來獲取數據回蟒蛇。

+0

FWIW - 這是Dash採用的一般方法,使用'flask'作爲Python服務器,'Plotly.js'作爲前端圖形庫,'fetch'作爲HTTP請求庫。 –

3

您可以使用Plotly的新Dash framework從Plotly圖表中選擇數據。

有一個在文檔的例子在這裏下的「圖形Crossfiltering」 https://plot.ly/dash/getting-started-part-2

我已經粘貼了以下完整的例子只是歷史的保存。

在下面的每個回調中,您都可以訪問選定的點,剛剛懸停的點或剛剛單擊的點。這個應用程序只是顯示應用程序中的點的值,但您可以對點進行任何操作(例如,計算其他值)。

example gif of selecting data in a Dash app

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) 
+0

你可以用jupyter編寫這個代碼並逐個執行它,但最終它不會以其他方式綁定到jupyter內核。以下是在Jupyter中開發的Dash應用程序示例:https://plot.ly/~jackp/17610 –

+0

您可以請隔離回答上面關於jupyter筆記本問題的代碼並添加回答。它看起來像很多不需要的代碼。 – Merlin

+0

好的 - 我只是刪除了樣式變量。我還添加了一些評論,您可以在何處訪問懸停,點擊或選擇活動的數據。 –