在一个轴上具有自动布局的多级(分组)标签

2024-05-23 21:25:12 发布

您现在位置:Python中文网/ 问答频道 /正文

在bokeh有可能在一个轴上有多个级别的标签吗?也就是说,对于像

TheBars | ThFoos | TheValues
--------|--------|----------
Bar     | Barfoo | 5
Bar     | Bargoo | 6
Bar     | Barhoo | 7
Foo     | Foobar | 1
Foo     | Foocar | 2

。。。组BarFoo中的所有列不仅应分组在一起,而且还应在轴上附加一个公共的组标签。在Excel呈现的示例中:

Multi-level labels

我目前正在使用bokeh.models.CategoricalColorMapper可视化地分离组,并在数据源中使用排序来将组粘在一起。这个很好,不过我也喜欢标签。


Tags: 示例foobokehbar标签级别excelfoobar
2条回答

此功能的文档可在此处查看:

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

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

output_file("bars.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=250, title="Fruit Counts by Year",
           toolbar_location=None, tools="")

p.vbar(x='x', top='counts', width=0.9, source=source)

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

我想知道是否已经拉好了?我不能复制v0.12.7中的多级轴,包括提到的所有分类教程链接。在

结果:

    ValueError: expected an element of either List(String) 
    or List(Int), got [('3', '6'), ('1', '6'), ('1', '4'), ('3', '3'), ('1', '8'), ('2', '6'), ('3', '4'), ('2', '5'), ('2', '4')]

相关问题 更多 >