如果正值/负值有不同的颜色,我如何在散景中制作条形图?

问题描述 投票:0回答:1

在下面的简单示例代码中,我有一个带有正/负值的条形图,我如何修改代码以显示正/负值条的绿色/红色?

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)

非常感谢任何帮助!

python bokeh
1个回答
2
投票

诀窍是将颜色信息传递给渲染器,在您的情况下

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)

输出

© www.soinside.com 2019 - 2024. All rights reserved.