Bokeh Pandas plot using SQLite data

422 Views Asked by At

I am using an SQLite database with Pandas and want to display the dynamic data using Bokeh (varea_stack)

My dynamic data (df) structure looks like this:

id        date       site  numberOfSessions  ...  avgSessionDuration  uniqueDimensionCombinations  events  pageViews
0   1  2020-07-29  177777770                 3  ...               11.00                            2       4          3
1   2  2020-07-29  178888883                 1  ...               11.00                            1       4          3
2   3  2020-07-29  177777770                 1  ...               11.00                            1       4          3
3   4  2020-07-29  173333333                 2  ...              260.50                            2      23         10
4   5  2020-07-29  178888883                 2  ...              260.50                            2      23         10
5   6  2020-07-29  173333333                 2  ...              260.50                            2      23         10
6   7  2020-07-29  178888883                12  ...              103.75                           12     143         36
7   8  2020-07-30  178376403                12  ...              103.75                           12     143         36
8   9  2020-07-30  178376403                12  ...              103.75                           12     143         36
9  10  2020-07-28  178376403                12  ...              103.75                           12     143         36

I would like to create a varea_stack plot where the:

x-axis -> "date"

y-axis -> "numberOfSessions" stacked according to "site"

(I am thinking maybe using some sort of Pivot Table?)

this is what I have:

from bokeh.plotting import figure, output_file, show
from bokeh.embed import components
from bokeh.models import HoverTool

plot = figure()
plot.varea_stack(df.site.unique().tolist(), x=df.index.values.tolist(), source=df)
script, div = components(plot)

the Error I get:

Keyword argument sequences for broadcasting must be the same length as stackers

I have been searching online (https://docs.bokeh.org/en/latest/docs/reference/plotting.html#bokeh.plotting.figure.Figure.varea_stack) and through Stackoverflow. I can't seem to find an answer.

1

There are 1 best solutions below

0
On

I can't really speak to the Pandas operations needed, but this is the general format the data needs to be in for varea_stack:

sites = [<list of sites>]

data = {
    'date'    : <all the datetime values>,
    <site1>   : <site1 values for every date>,
    <site2>   : <site2 values for every date>,
    <site3>   : <site3 values for every date>,
    ...
}

plot.varea_stack(sites, x='date', source=data)

Note that to be usable by varea_stack the following must be true:

  • every item in the sites list has to be a column in the data
  • every sites column has to be the same length (a value for every date)

Note that the above also assumes the dates are converted to real datetime values. If you are using your dates are categoricals (i.e. not using real datetimes and a continous datetime axis) then you will need to pass the list of date (strings) to the x_range for figure as well (as with any categorical axis).