Most efficient way to plot thousands of horizontal lines

I need to make an interactive HoloViews plot with thousands of horizontal lines defined by a dataframe with left, right and height coordinates. The datasets I’m working with have about 100 000 lines, so I’m also planning to datashade the plot. I can’t figure out a way to plot the lines in a way that doesn’t take a long time to load. Here is what I’ve come up with:

import pandas as pd
import numpy as np
import holoviews as hv
import holoviews.operation.datashader as hd

df = pd.DataFrame({
    'left': np.random.uniform(0, 100, 1000),
    'right': np.random.uniform(101, 200, 1000),
    'height': np.random.uniform(0, 50, 1000), 
})

lines_data = [[(row['left'], row['height']), (row['right'], row['height'])] for _, row in df.iterrows()]
lines = hv.Path(lines_data, kdims=["x", "height"])
lines_datashaded = hd.datashade(lines)
lines_datashaded.opts(width=800, height=400)

Is there a way to pass the left, right and height columns as arguments to the hv.Path function instead of coercing them into a list of tuples? Or is there perhaps an alternative function I could use?

I managed to find the solution: the function hv.Segments is much faster in this case. Here is a solution for anyone who finds this:

import pandas as pd
import numpy as np
import holoviews as hv
import holoviews.operation.datashader as hd

df = pd.DataFrame({
    'left': np.random.uniform(0, 100, 1000),
    'right': np.random.uniform(101, 200, 1000),
    'height': np.random.uniform(0, 50, 1000), 
})
df['height_right'] = df['height']
lines = hv.Segments(df, kdims=["left", "height", "right", "height_right"])
lines_datashaded = hd.datashade(lines)
lines_datashaded.opts(width=800, height=400)
2 Likes

Hi @duncanMR

Welcome to the community and thanks for reporting back the solution with a minimum, reproducible example. That helps build the community knowledge base.

1 Like