How can I make a bar chart in Bokeh were positive/negative values have different colours?
Question:
In the below simple example code I have a bar chart with pos/neg values, how can I ammend the code to show green/red for pos/neg value bars?
from bokeh.io import show
from bokeh.plotting import figure
fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
counts = [-5, 3, 4, -2, -4, 6]
p = figure(x_range=fruits, plot_height=250, title="Fruit Counts",
toolbar_location=None, tools="")
p.vbar(x=fruits, top=counts, width=0.9)
p.xgrid.grid_line_color = None
show(p)
Any help much appreciated!
Answers:
The trick is to pass color information to the renderer, in your case vbar
. This can be a single color or a list of colors. If it is a list, this list has to have the same length as the other lists.
The color information can be a RGB value, a supported name of a color or a hex string. See also the colored example from the docs.
Minimal Example
from bokeh.plotting import show, figure, output_notebook
output_notebook()
fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
counts = [-5, 3, 4, -2, -4, 6]
color = ['blue' if x <= 0 else 'red' for x in counts]
p = figure(x_range=fruits, plot_height=250, title="Fruit Counts",
toolbar_location=None, tools="")
p.vbar(x=fruits, top=counts, width=0.9, color=color)
p.xgrid.grid_line_color = None
show(p)
Output
In the below simple example code I have a bar chart with pos/neg values, how can I ammend the code to show green/red for pos/neg value bars?
from bokeh.io import show
from bokeh.plotting import figure
fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
counts = [-5, 3, 4, -2, -4, 6]
p = figure(x_range=fruits, plot_height=250, title="Fruit Counts",
toolbar_location=None, tools="")
p.vbar(x=fruits, top=counts, width=0.9)
p.xgrid.grid_line_color = None
show(p)
Any help much appreciated!
The trick is to pass color information to the renderer, in your case vbar
. This can be a single color or a list of colors. If it is a list, this list has to have the same length as the other lists.
The color information can be a RGB value, a supported name of a color or a hex string. See also the colored example from the docs.
Minimal Example
from bokeh.plotting import show, figure, output_notebook
output_notebook()
fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
counts = [-5, 3, 4, -2, -4, 6]
color = ['blue' if x <= 0 else 'red' for x in counts]
p = figure(x_range=fruits, plot_height=250, title="Fruit Counts",
toolbar_location=None, tools="")
p.vbar(x=fruits, top=counts, width=0.9, color=color)
p.xgrid.grid_line_color = None
show(p)