Search code examples
bokehcorrelationholoviews

Holoviews tap stream of correlation heatmap and regression plot


I want to make a correlation heatmap for a DataFrame and a regression plot for each pair of the variables. I have tried to read all the docs and am still having a very hard time to connect two plots so that when I tap the heatmap, the corresponding regression plot can show up.

Here's some example code:

import holoviews as hv
from holoviews import opts
import seaborn as sns
import numpy as np
import pandas as pd
hv.extension('bokeh')

df = sns.load_dataset('tips')
df = df[['total_bill', 'tip', 'size']]

corr = df.corr()
heatmap = hv.HeatMap((corr.columns, corr.index, corr))\
            .opts(tools=['tap', 'hover'], height=400, width=400, toolbar='above')

m, b = np.polyfit(df.tip, df.total_bill, deg=1)
x = np.linspace(df.tip.min(), df.tip.max())
y = m*x + b

curve = hv.Curve((x, y))\
          .opts(height=400, width=400, color='red', ylim=(0, 100))
points = hv.Scatter((df.tip, df.total_bill))

hv.Layout((points * curve) + heatmap).cols(2)

image


Solution

  • I adjusted the relevant parts of the docs http://holoviews.org/reference/streams/bokeh/Tap.html with your code. Maybe this clears up your confusion.

    import pandas as pd
    import numpy as np
    import holoviews as hv
    from holoviews import opts
    hv.extension('bokeh', width=90)
    
    import seaborn as sns
    
    # Declare dataset
    df = sns.load_dataset('tips')
    df = df[['total_bill', 'tip', 'size']]
    
    # Declare HeatMap
    corr = df.corr()
    heatmap = hv.HeatMap((corr.columns, corr.index, corr))
    
    # Declare Tap stream with heatmap as source and initial values
    posxy = hv.streams.Tap(source=heatmap, x='total_bill', y='tip')
    
    # Define function to compute histogram based on tap location
    def tap_histogram(x, y):
        m, b = np.polyfit(df[x], df[y], deg=1)
        x_data = np.linspace(df.tip.min(), df.tip.max())
        y_data = m*x_data + b
        return hv.Curve((x_data, y_data), x, y) * hv.Scatter((df[x], df[y]), x, y)
    
    
    tap_dmap = hv.DynamicMap(tap_histogram, streams=[posxy])
    
    (heatmap + tap_dmap).opts(
        opts.Scatter(height=400, width=400, color='red', ylim=(0, 100), framewise=True),
        opts.HeatMap(tools=['tap', 'hover'], height=400, width=400, toolbar='above'),
        opts.Curve(framewise=True)
    )