Search code examples
pythonbokeh

Bokeh - Fill the gap in line plot (standalone)


I'm looking to fill the gap in this plot:

data = pd.DataFrame({'YYYY_MM': {0: '2020-11',
  1: '2020-12',
  2: '2021-01',
  3: '2021-02',
  4: '2021-03'},
 'value': {0: 0.7,
  1: np.nan,
  2: 0.5,
  3: 0.8,
  4: 0.2}})

factors = data["YYYY_MM"].tolist()
p = figure(x_range=FactorRange(*factors), 
           plot_height=350)

p.line(x="YYYY_MM", 
       y="value", 
       source = data,
       color="orange",
       line_width=2,
      legend_label="legend")


p.circle(x="YYYY_MM", 
       y="value", 
       source = data,
       color="orange",
      legend_label="legend")

show(p)

enter image description here

How can I put a line between the dot and the line? How can I do that the line plot ignore the missing value? I have found some examples when bokeh does automatically what I'm looking for (link) but it seems for the line plot it doesn't do it automatically. So, how can do I solve this? Any suggestions?


Solution

  • The linked question is not relevant. That concerns compressing an axis to omit certain values (e.g. non-trading days) so that what is left is "closer together". That has no relation to wanting to keep the full axis, but ignore nan values in the data.

    You have to remove the nan value from your data before passing it to Bokeh, there is no other way. Bokeh will always interrupt rendering for nan values (that behavior is intentional). You might consider pandas dropna method to get a view of the dataframe with all nan values removed that you can pass to Bokeh.