Correcting a typo in the examples gallery

Dear Bokeh Team,

Referring to the following page from the Bokeh User Guide (https://docs.bokeh.org/en/latest/docs/user_guide/categorical.html#nested-categories), I believe that the zipped list used to create “counts” should be flattened using the following extra step (the 2nd line of code in the below snippet, which I have sandwiched in between your original Bokeh code):

counts = sum(zip(data['2015'], data['2016'], data['2017']), ()) # like an hstack
flat_list = [item for sublist in counts for item in sublist]
source = ColumnDataSource(data=dict(x=x, counts=flat_list))

Doing so will ensure that ColumnDataSource has a matching number of entries.

Please let me know i this looks correct to you, happy to be corrected if you think the original code is correct!

Kind regards,
Greg

Hi @G_Endeavour welcome to the Bokeh Discourse. In this case the result is already flattened:

In [11]: counts = sum(zip(data['2015'], data['2016'], data['2017']), ()) # like an hstack

In [12]: counts
Out[12]: (2, 5, 3, 1, 3, 2, 4, 3, 4, 3, 2, 4, 2, 4, 5, 4, 6, 3)

And the reason for this is that the zip results are “summed” with an empty list start argument. This basically has the effect of transposing the sense of fruits/years in the data. It’s arguably one of those tricks that is a little too clever for its own good. At the very least, it’s not especially obvious.

As an aside, all the live plots in the Bokeh docs are actually generated by the accompanying code, so you can always be guaranteed that if the plot is displayed, then the code is working.

1 Like

Hi @Bryan,

Your extremely quick response is highly appreciated – what a welcome, to have the core developer of Bokeh respond so quick (and with humility)!

I was working in Jupyter Notes and it returned this instead:
(2, 5, 3) (1, 3, 2,) (4, 3, 4) (3, 2, 4) (2, 4, 5) (4, 6, 3)

In Spyder IDE however, the correct output was returned:
(2, 5, 3, 1, 3, 2, 4, 3, 4, 3, 2, 4, 2, 4, 5, 4, 6, 3)

I am unsure why this occurred, because after restarting PC (& kernel) Jupyter Notes actually delivered the correct output; I am new to using Jupyter Notes, so am unsure if this is a known stability issue, or it was just a quirk. In any case, it is not a Bokeh problem for sure, and your code is 100% correct.

Bokeh/Pyviz is an amazing visualisation suite, thanks for all of your/your team’s hard work.

@G_Endeavour Thank you for the kind words. I am not actually familiar with Jupyter Notes. If this is something you can reproduce reliably, starting from the same data as above, then it might be worth raising an issue with them to let them know about it.

Thanks @Bryan, I have reproduced the problem again, so yes, I will log the error observation with Jupyter and take it from there.

1 Like