python - Bokeh 水平堆叠条形图

标签 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)

最佳答案

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")

关于python - Bokeh 水平堆叠条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50417528/

相关文章:

python - matplotlib fill_ Between 留下间隙

javascript - 如何在 D3JS 中设置 Circle Pack 的最小半径?

python - 通过悬停在 Bokeh 中突出显示多个 hex_tiles

javascript - Bokeh 自定义 ToolTips {safe} 标签不显示任何内容

python - 如何向 pointdrawtool 添加 python 回调

python - 比 If Else 更聪明

python - 如何在 Bokeh 悬停格式化程序上设置自定义日期时间模式?

python - 使用多处理时无法重现 scikit-learn 和 numpy 相关代码

python - 如何处理引号不均匀的 .tsv?

python - 如何使用python读取excel文件中的特定行