Debbie
Debbie

Reputation: 969

Python+Bokeh : How to color points category wise on a line plot in Bokeh?

I am trying to plot a lineplot with Bokeh whih will show points different colors according to their categories. these points are respiration rates of a patient. There are 13 categories of respiration rates like A, B, C etc. I was able todraw the plot, but unable to put colors on it according to categories.

Here's my code to draw the plot:

x = np.arange(1, len(resp_rates))
y = resp_rates
# output to static HTML file
output_file("Respiration rate.html")

# create a new plot with a title and axis labels
p = figure(title="Respiration rate class", x_axis_label= "Patient ID 123", y_axis_label= "Respiration rates", plot_width = 1000)

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


# show the results
show(p)

Output: enter image description here

I have calculated the classes of every points which are stored in list called labels. How to color the points on the lineplot according to their classes/labels? I have to use Bokeh only.

Edit: I changed the code:

x = np.arange(1, len(resp_rates))
y = resp_rates
# output to static HTML file
output_file("Respiration rate.jpg")

# create a new plot with a title and axis labels
p = figure(title="Respiration rate class", x_axis_label= "Patient ID 123", y_axis_label= "Respiration rates", plot_width = 3000, x_range=(0, 101))

# add a line renderer with legend and line thickness
pal = Dark2[3]
factors = list(set(labels))
source = ColumnDataSource(data=dict(Classes=labels, x=x, y=y))
p.line(x, y, legend="Respiration rate", line_width=2, factor_cmap('Classes', palette=pal, factors=factors), source=source)


# show the results
show(p)

Now it's giving this error:

 File "<ipython-input-32-8377d3798bb8>", line 13
    p.line(x, y, legend="Respiration rate", line_width=2, factor_cmap('Classes', palette=pal, factors=factors), source=source)
                                                         ^
SyntaxError: positional argument follows keyword argument

Upvotes: 0

Views: 2111

Answers (1)

bigreddot
bigreddot

Reputation: 34568

The line glyph method only draw a line of a single color. It does not draw anything else, e.g. it does not draw any marker glyphs at the points that make up the line. If you want that, you can pass the same data to whatever marker you want to use for the points:

p.line(x, y, legend="Respiration rate", line_width=2)
p.circle(x, y, color=...)

Where the value of color could be a list of colors that you have colormapped based on the category ahead of time, or could be a factor_cmap to automatically colormap the points as demonstrated here:

https://docs.bokeh.org/en/latest/docs/user_guide/data.html#markers

As a suggestion, you should consider putting the data in a ColumnDataSource explicitly, so that it can be shared between the line and marker glyphs without duplication (and this is necessary if you want to use factor_cmap at all).

Upvotes: 1

Related Questions