Search code examples
pythonbokeh

How to Make Event Plot using Python Bokeh Library?


I am trying to make event plot using Bokeh Library. I know it is possible using matplotlib.pyplot.eventplot, But I need more interactivity features that is available in Bokeh

I came across the screenshot of such a chart in this Website. So it must be possible in Bokeh. Bokeh Event Plot

How do I make a similar plot using Bokeh Library?


Solution

  • The idea is very basic. You can use the rect() function of the figure object, where you have to define x, y and width. In your case the figure should have an x-axis in datetime-format. The width is your duration of on task.

    Minimal example

    import pandas as pd
    from bokeh.plotting import figure, show
    from bokeh.models import ColumnDataSource
    
    df = pd.DataFrame({'y':[1, 2, 3, 4], 
                       'start':[0, 1, 1, 10], 
                       'duration':[1, 2, 1, 5], 
                       'color':['green', 'red', 'blue', 'red']
                      })
    >>>df
       y  start  duration  color
    0  1      0         1  green
    1  2      1         2    red
    2  3      1         1   blue
    3  4     10         5    red
    
    p = figure(
        width=350,
        height=350,
        title="Task Stream",
        toolbar_location="above",
        x_axis_type="datetime",
    )
    
    p.rect(
        source=ColumnDataSource(df),
        x="start",
        y="y",
        width="duration",
        height=0.4,
        fill_color="color",
        line_color="color",
    )
    show(p)
    

    Output

    Basic example for streaming data

    Comment

    As I mentionend in the comment, the figure you have shared is generated using the dask package which internally uses bokeh. They use the same idea I described above, but of course the dask project offers much more than this on figure and maby solves more than one problem you have.