Search code examples
pythondata-visualizationpython-interactive

Python interactive visualization able to write selected data to dataframe?


I am looking for an interactive visualization python tool that would allow me to select a number of data points and then write only those data points into a new data frame, numpy array, etc. For example, I'd like to visualize all of the equity trades a desk of traders did in one day along with tick-by-tick price data (y axis price, x axis time) then select a subset of them and send them to a new dataframe for further processing. Does anything like that exist? I thought Holoviews might but haven't figured it out yet. Thanks, Colin


Solution

  • Indeed you can do this with Holoviews. In this example it is used in combination with Bokeh. Save the script file, open a command window in the directory of the file and execute: "bokeh serve --myscript.py --show". The "arr" in the code is the array of the selected datapoints.

    import numpy as np
    import holoviews as hv
    import holoviews.plotting.bokeh
    
    renderer = hv.renderer('bokeh')
    
    points = hv.Points(np.random.randn(1000,2 )).opts(plot=dict(tools=['box_select', 'lasso_select']))
    selection = hv.streams.Selection1D(source=points)
    
    def selected_info(index):
        arr = points.array()[index]
        print(arr)
        if index:
            label = 'Mean x, y: %.3f, %.3f' % tuple(arr.mean(axis=0))
        else:
            label = 'No selection'
        return points.clone(arr, label=label).opts(style=dict(color='red'))
    
    layout = points + hv.DynamicMap(selected_info, streams=[selection])
    
    doc = renderer.server_doc(layout)
    doc.title = 'HoloViews App'
    

    Strictly you don't need holoviews, you can do this also with bokeh alone. There are examples on the bokeh website.