Skip to main content
 首页 » 编程设计

python之我如何在 Bokeh 中组合多个条形图

2024年10月24日16lonelyxmas

我想在同一张图上绘制两个条形图。

一个将只有正值,一个将只有负值。

我想要绿色的正条(x 轴上方)和红色的负条(x 轴下方)

问题:

<强> 1。是否可以使用 bokeh.charts 接口(interface)中现有的高级 Bar 方法来执行此操作?

<强> 2。如果没有,我如何使用较低级别的 bokeh.plotting 界面创建条形图? (而不是更高级别的 bokeh.charts 接口(interface))

请您参考如下方法:

编辑:下面的答案已经过时几年了。现在,各种条形图(堆叠、分组、颜色映射)都变得更加简单和容易。有关许多示例,请参阅用户指南的这一部分:

https://docs.bokeh.org/en/latest/docs/user_guide/categorical.html


1. 我尝试使用高级 Bar 方法制作多个条形图,但是我无法实现我想要的效果,因此我使用了绘图界面。

2. 这是您要找的吗?

    from bokeh.plotting import figure, output_file, show 
 
    plot = figure(width=600, height=600, x_range=(0,50), y_range=(-10,10)) 
 
    plot.quad(top=[10],bottom=[0],left=[1],right=[2], color='green', line_color='black', legend='positive') 
    plot.quad(top=[12],bottom=[0],left=[2],right=[3], color='green', line_color='black', legend='positive') 
    plot.quad(top=[1],bottom=[0],left=[3],right=[4], color='green', line_color='black', legend='positive') 
    plot.quad(top=[2],bottom=[0],left=[4],right=[5], color='green', line_color='black', legend='positive') 
    plot.quad(top=[3],bottom=[0],left=[5],right=[6], color='green', line_color='black', legend='positive') 
    plot.quad(top=[4],bottom=[0],left=[6],right=[7], color='green', line_color='black', legend='positive') 
 
    plot.quad(top=[-5],bottom=[0],left=[1],right=[2], color='red', line_color='black', legend='negative') 
    plot.quad(top=[-6],bottom=[0],left=[2],right=[3], color='red', line_color='black', legend='negative') 
    plot.quad(top=[-2],bottom=[0],left=[3],right=[4], color='red', line_color='black', legend='negative') 
    plot.quad(top=[-8],bottom=[0],left=[4],right=[5], color='red', line_color='black', legend='negative') 
    plot.quad(top=[-9],bottom=[0],left=[5],right=[6], color='red', line_color='black', legend='negative') 
    plot.quad(top=[-10],bottom=[0],left=[6],right=[7], color='red', line_color='black', legend='negative') 
 
    output_file('test.html') 
    show(plot)