'Conditional coloring in bokeh vbar plots

Try to make a vbar plot with bokeh. Would like to set a condition for the fill color when the value exceeds a pre-set number. Say, red when greater than 5, else purple. Is this doable with bokeh?

Any demonstration would be greatly appreciated. My data is stored in a pandas dataframe.



Solution 1:[1]

You can create a column in your DataFrame named color and pass this data to the keyword color in the function vbar() using the DataFrame as source.

Minimal Example

import pandas as pd
import numpy as np

from bokeh.plotting import figure, show, output_notebook
output_notebook()

df = pd.DataFrame({'a':[1,2,3,4,5,6]})
df['color'] = np.where(df['a'].le(5), 'purple','red')

p = figure()
p.vbar('index', top='a', width=0.75, fill_alpha=0.5, color='color', source=df)
show(p)

Output

bar plot

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mosc9575