散景水平堆积条形图

时间:2018-05-18 18:46:06

标签: python data-visualization bokeh stacked-chart

我正在尝试将垂直堆积条形图转换为水平堆积条形图,但我无法进行旋转。我尝试使用hbar创建旋转,但图表仍然最终保持不变。所有提示都赞赏!

from bokeh.charts import Bar, output_file, show, hplot
from bokeh.models import HoverTool, ColumnDataSource, Range1d, LabelSet, Label

# create data
data = {
    'customer': ['Cust 1', 'Cust 2',  'Cust 1', 'Cust 3', 'Cust 1', 'Cust 2'],
    'itemSold': ['python', 'python', 'pypy', 'pypy', 'jython', 'jython'],
    'sales': [200, 600, 850, 620, 400, 550]
}

#create hover tooltip
hover = HoverTool(tooltips=[
    ("sales", "$sales"),
    ("customer", "@customer"),
    ("itemSold", "@itemSold")

])


# x-axis itemSold , stacking customer
bar = Bar(data, values='sales', label='itemSold', stack='customer',
          title="Python itemSold Sampling", legend='top_right', sizing_mode = "scale_both", tools=[hover, 'wheel_zoom'])
bar.hbar(y = data['sales'], height=0.5, right = data['customer'])

output_file("stacked.html")
show(bar)

1 个答案:

答案 0 :(得分:2)

bokeh.charts似乎已经在0.12.9中弃用了所以我用最新的(写作时为0.12.16)完成了,希望你处于可以毫无问题地升级的位置,应该是虽然向后兼容很远。

此外,我希望堆栈是正确的方式! This example may also help.

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

# create data
products = ['python', 'pypy', 'jython']
customers = ['Cust 1', 'Cust 2']
colours = ['red', 'blue']
data = {
    'products': products,
    'Cust 1': [200, 850, 400],
    'Cust 2': [600, 620, 550]
}


source = ColumnDataSource(data)

p = figure(y_range=products)

p.hbar_stack(customers, y='products', height=0.5, source=source, color=colours)

show(p)
output_file("stacked.html")