在下面的简单示例代码中,我有一个带有正/负值的条形图,我如何修改代码以显示正/负值条的绿色/红色?
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)
非常感谢任何帮助!
诀窍是将颜色信息传递给渲染器,在您的情况下
vbar
。这可以是单一颜色或颜色列表。如果它是一个列表,这个列表必须与其他列表具有相同的长度。
颜色信息可以是RGB值、支持的颜色名称或十六进制字符串。另请参阅文档中的彩色示例。
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)