No line displayed when using nan values

I pasted the code from First steps 1: Creating a line chart — Bokeh 2.4.2 Documentation and it worked as expected. I then changed the values to more closely resemble my data, (I have some NaNs that I still wanted to chart, SEE Missing points: “You can pass NaN values to line() and multi_line() glyphs. This produces disjointed lines with gaps for NaN values.”) but I can’t get the line to show up.

# beta1c.py

from bokeh.plotting import figure, show

nan = float('nan') <--change from example in docs

# prepare some data
x = [1, 2, 3, nan, 5] <--change from example in docs
y = [6, nan, 2, 4, 5] <--change from example in docs

# create a new plot with a title and axis labels
p = figure(title="Simple line example", x_axis_label="x", y_axis_label="y")

# add a line renderer with legend and line thickness
p.line(x, y, legend_label="Temp.", line_width=2)

# show the results
show(p)

The only question I know to ask is: Why doesn’t this work? Thx.

update, like 20 minutes later:
I decided to try to jupyter notebook tutorlal. After the first exercise, which uses the same plot points as the tutorial I was using before, it suggested I play with the values. So I put in my nan…and it worked! That’s just not fair, and very discouraging.

# create a new plot with default tools, using figure
p = figure(plot_width=400, plot_height=400)
nan = float('nan')
# add a circle renderer with x and y coordinates, size, color, and alpha
p.circle([1, 2, 3, 4, 5], [6, nan, 2, 4, 5], size=15, line_color="navy", fill_color="orange", fill_alpha=0.5)

show(p) # show the results

update # 2
Because I am a masochist, I copied the code from binder and pasted into my editor (PyCharm) and it even worked there. It must be something tiny but totally necessary, but I’m too frustrated to keep looking and trying to figure it out. Your help appreciated.

Hi @malikarumi please edit your post to use code formatting so that the code is intelligible (either with the </> icon on the editing toolbar, or triple backtick ``` fences around the code blocks)

ok… but I have to put in ten characters to say that…

First off: the above code is not valid python code. To make it easier for others to help you, please make sure posted code is a complete Minimal Reproducible Example that is valid code that can be run, as-is.

The reason the data above results in a blank plot is because there are never two adjacent non-nan points anywhere. In order to draw segments, there have to be adjacent valid (i.e. non-nan for both x and y) points. [1]


  1. Bokeh does not “bridge” across nan values with an invented segment that does not exist in the data. The nan point, and any segments would before or after it, are deleted. In this specific case, since there is a nan every other point, that means every segment is deleted. ↩︎

@Bryan Thank you. This is the answer I needed.

1 Like

Cannot get bokeh to work with my own data - part 2

The goal is a heatmap showing compliance with a medical regimen over time. The named medications being monitored are on the y axis, and the dates are on the x axis. The better the compliance, the darker the heatmap, which uses only shades of green.

I took the code from Handling categorical data — Bokeh 2.4.2 Documentation

and literally just turned it 90 degrees to the right.

This is the result from my version of this chart:

Everything is there, exactly as it should be - except my data! Even the legend is right, so why no data?!

I fully recognize that this problem is nearly identical to what I posted the first time. But that had to do with the presence of NaNs in my data, which is not the case here. So why do I still have this problem?

I tried this two different ways. First, by having the human readable text from the user compliance input form as the values:

And by just using the corresponding hex codes as the values:

Both gave me the same resulting chart.

Finally, here is my code:

fruitdocs1.py

from bokeh.io import output_file, show

from bokeh.plotting import figure

from bokeh.models import ColumnDataSource

from datetime import datetime, timedelta

output_file("cds_rect.html")

meds = ['alphastim', 'amlodipine', 'ammonium_lactate', 'caffeine', 'desvenlafaxine',

'dicyclomine', 'gasx', 'kaopectate', 'linzess', 'magnesium', 'miralax',

'modafinil', 'naproxen', 'vaporub']

values = ['Yes', 'No', 'Not Recorded', 'Not Required']

colors = [

"#bae4b3", "#74c476", "#31a354",

"#006d2c"]

time = ['2022-04-19', '2022-04-20', '2022-04-21']

data = {

'meds': meds,

'2022-04-19': ['Yes', 'No', 'No', 'Yes', 'Not Required', 'Not Required', 'Not Required',

'Not Recorded', 'No', 'No', 'Not Recorded', 'Not Recorded', 'Not Recorded',

'Yes'],

'2022-04-20': ['Yes', 'Not Recorded', 'No', 'Not Required', 'Not Required', 'Yes',

'Not Recorded', 'Not Required', 'No', 'Not Recorded', 'No', 'Not Recorded',

'Not Required', 'No'],

'2022-04-21': ['Not Required', 'No', 'No', 'Yes', 'No', 'Not Recorded', 'Not Recorded',

'Not Required', 'Not Required', 'Yes', 'Not Recorded', 'No', 'Not Recorded',

'Yes']

}

source = ColumnDataSource(data)

p = figure(y_range=meds, x_range=time, height=500, title="Medical Compliance fruitdocs1",

toolbar_location=None, tools="")

p.rect(x=time, y=meds, width=timedelta(days=1), color=colors, height=1,

legend_label=values)

p.y_range.range_padding = 0.1

p.xgrid.grid_line_color = None

p.axis.minor_tick_line_color = None

p.outline_line_color = None

p.legend.location = "top_right"

p.legend.orientation = "vertical"

show(p)

Thank you for your help.

@malikarumi When I copy and paste your code and try to run it with Bokeh 2.4.2 I immediately get CDS validation errors

BokehUserWarning: ColumnDataSource's columns must be of the same length. Current lengths: ('line_color', 4), ('x', 3), ('y', 14)
BokehUserWarning: ColumnDataSource's columns must be of the same length. Current lengths: ('fill_color', 4), ('line_color', 4), ('x', 3), ('y', 14)
BokehUserWarning: ColumnDataSource's columns must be of the same length. Current lengths: ('fill_color', 4), ('hatch_color', 4), ('line_color', 4), ('x', 3), ('y', 14)

I don’t recall when those checks were added, perhaps you are using an older version of Bokeh that simply silently fails? CDS column lengths not matching is definitely a usage error that will result in undefined behavior. So in any case I would suggest starting by fixing that problem.

Edit: also an exception about legend label type:

ValueError: legend_label value must be a string

FYI it is always advised to state relevant version information in all support questions.

I’m also really confused about this statement:

I took the code from Handling categorical data — Bokeh 2.4.2 Documentation and literally just turned it 90 degrees to the right.

Because your code above is nothing like the example code. Taking the vbar_stack and turning it 90 degrees would be a matter of using hbar_stack instead of vbar_stack. Your code above doesn’t do any stacking at all.

@malikarumi Also is this a new question that is completely unrelated to the original post? Please always create new separate topics for new separate questions, in order to help keep this resource organized for the entire community.

Edit: I am locking this topic and will delete the new replies here after you open a new topic.