Line plot using view

I can add a couple things.

  1. CDSView is not compatible with the Line glyph (and Patch glyph/any single connected topology-type glyph) → good discussion why is here Explicitly warn that CDSView is unsupported on line glyphs · Issue #9388 · bokeh/bokeh · GitHub . Basically it doesn’t make sense to apply a filter/sub view on a single connected topology because the desired behaviour is ambiguous, e.g. do you want the line to be “broken” along filtered indices, or do you want it to simply “skip” them and connect to the next unfiltered index? There are probably other desired behaviours beyond just these too.

  2. Looking at your MRE, your ColumnDataSource isn’t Line glyph material anyway because (I think) you want two separate lines (one for ‘a’ and one for ‘b’)?

So I think you’re right with going down the multiline route. And you’ve correctly surmised you need to set up your source with xy coords as list of lists so that each line corresponds to one record in the CDS.

Your issue from there is actually that you’re assigning the name attribute on the GroupFilter , where you should be assigning column_name. See the docs, they are wildly different things → filters — Bokeh 3.3.4 Documentation

Working example:

import numpy as np

from bokeh.io import show
from bokeh.models import ColumnDataSource, CDSView, GroupFilter
from bokeh.plotting import figure
import pandas as pd

x = np.linspace(0, 10, 10)
y = 5 * x + 10
kind = ["a"] * 5 + ["b"] * 5

df = pd.DataFrame(data={'x':x,'y':y,'kind':kind})

gb = df.groupby('kind').agg({c:lambda x: list(x) for c in ['x','y']}).reset_index()
                             
data = ColumnDataSource(gb)
view_a = CDSView(filter=GroupFilter(column_name="kind", group="a"))
view_b = CDSView(filter=GroupFilter(column_name="kind", group="b"))

p = figure()
p.multi_line(xs="x", ys="y", source=data, view=view_a)
show(p)

2 Likes