FactorRange和分类的vbar图表问题

时间:2018-05-03 10:54:59

标签: python bokeh

我正在使用vbar在bokeh / python中绘制第4级条形图。在FactorRange的文档中,仅支持3级(FactorRange level

所以我最多只能有3级分类条形图。我想合并下面2条进行比较,怎么可能?

我希望将红色和蓝色图表放在一个vbarchart中进行比较。

2 level-3 barchart

下面是我的代码和数据:

代码:

pRX = figure(x_range =[],plot_height=250, plot_width=1000, title="VS.FEGE.RXMAXSPEED", toolbar_location=None,tools="")
pTX = figure(x_range =[],plot_height=250, plot_width=1000, title="VS.FEGE.TXMAXSPEED", toolbar_location=None,tools="")

    x1 = list(tp['SRN'])
    x2 = list(tp['SN'])
    x3 = list(tp['PN'])
    x = [(str(a1), str(a2), str(a3)) for a1, a2, a3 in zip(x1, x2, x3)]
    countRx = list(tp['ID_67194369'])
    countTx = list(tp['ID_67194372'])
    sourceRx = ColumnDataSource(data=dict(x=x, counts=countRx))
    sourceTx = ColumnDataSource(data=dict(x=x, counts=countTx))
    #pRX.x_range=FactorRange(*x)
    pRX.x_range.factors = x
    pTX.x_range.factors = x
    pRX.vbar(x='x', top='counts', width=0.4, source=sourceRx)
    pTX.vbar(x='x', top='counts', width=0.4, color="red", source=sourceTx)
    return



layout = column( pTX, pRX)
curdoc().add_root(layout)

示例数据:

    SRN SN  PN  ID_67194369 ID_67194370
0   0   16  0   3635315.728 716296.803
1   0   16  1   0.000   0.000
2   0   17  0   4.757   0.717
3   0   17  1   0.000   0.000
4   0   18  0   473025.813  1649058.792
5   0   18  1   0.000   0.000
6   0   19  0   2.101   0.614
7   0   19  1   0.000   0.000
8   0   22  1   82132.182   345496.891
9   0   23  1   2.101   0.512
10  1   16  0   31830.312   28466.419
11  1   16  1   0.000   0.000
12  1   17  0   3.874   0.717
13  1   17  1   0.000   0.000
14  1   18  0   473077.917  1613683.357
15  1   18  1   0.000   0.000
16  1   19  0   1.998   0.614
17  1   19  1   0.000   0.000
18  1   22  1   83809.363   293451.018
19  1   23  1   2.203   0.512

1 个答案:

答案 0 :(得分:1)

您可以将dodge与嵌套类别结合使用。由于您尚未提供完整的代码示例,因此我无法更新您的代码示例。但是这里有一个完整的例子,可以避开2级嵌套栏(对于3个级别来说非常相似):

from bokeh.io import show, output_file
from bokeh.models import ColumnDataSource, FactorRange
from bokeh.plotting import figure
from bokeh.transform import dodge


output_file("bar_nested.html")

fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
years = ['2015', '2016', '2017']

data = {'fruits' : fruits,
        '2015'   : [2, 1, 4, 3, 2, 4],
        '2016'   : [5, 3, 3, 2, 4, 6],
        '2017'   : [3, 2, 4, 4, 5, 3]}

# this creates [ ("Apples", "2015"), ("Apples", "2016"), ("Apples", "2017"), ("Pears", "2015), ... ]
x = [ (fruit, year) for fruit in fruits for year in years ]
counts = sum(zip(data['2015'], data['2016'], data['2017']), ()) # like an hstack

source = ColumnDataSource(data=dict(x=x, counts=counts))

p = figure(x_range=FactorRange(*x), plot_height=350, title="Fruit Counts by Year",
           toolbar_location=None, tools="")

p.vbar(x=dodge('x', -0.25, range=p.x_range), top='counts', width=0.4, source=source)
p.vbar(x=dodge('x',  0.25, range=p.x_range), top='counts', width=0.4, source=source, color="red")

p.y_range.start = 0
p.x_range.range_padding = 0.1
p.xaxis.major_label_orientation = 1
p.xgrid.grid_line_color = None

show(p)

enter image description here

为简单起见,这段代码只是躲过了同样的" x"专栏两次。