Search code examples
plotlyplotly-dashplotly-python

Including spreadsheet in interactive plotly scatterplot


Recently I've been using plotly to generate beautiful interactive scatterplots where one can navigate and search for specific instances. Then I've saved the result in a html file that can be opened anytime.

I'm wondering if it's possible to include a spreadsheet (potentially in the same generated html file) with attached tabular descriptions of the points (so having 2D plot, these would simply be coordinates of XY axes).


Solution

  • You could use go.Table

    go.Table provides a Table object for detailed data viewing. The data are arranged in a grid of rows and columns. Most styling can be specified for header, columns, rows or individual cells.

    If you have a scatter and a table trace that are both part of the same figure, both traces will show up when you write the figure to an html file.

    from plotly.subplots import make_subplots
    import plotly.graph_objects as go
    
    x_values = [1, 2, 3]
    y_values = [1, 3, 5]
    
    fig = make_subplots(
        rows=2,
        cols=1,
        specs=[[{"type": "xy"}], [{"type": "domain"}]]
    )
    
    fig.add_trace(go.Scatter(x=x_values, y=y_values), row=1, col=1)
    fig.add_trace(go.Table(header=dict(values=['X', 'Y']), cells=dict(values=[x_values, y_values])), row=2, col=1)
    
    fig.write_html("demo.html")
    

    result