散景条图:按类别划分的彩条

时间:2016-07-14 01:27:17

标签: python pandas bokeh

我正在调整位于here的第二个示例。

这是我的代码:

from bokeh.charts import BoxPlot, Bar, output_file, show
from bokeh.sampledata.autompg import autompg as df
output_file("bar.html")
p = Bar(df, values='mpg', label='cyl', color='origin', legend="top_left",
            title="MPG Summary (grouped and shaded by CYL)")
show(p)

有三个变化:(1)我使用Bar图,(2)我将color属性更改为不同的分类变量,(3)我添加了legend属性。

我相信问题出在(2)和(3)之间。更具体地说,图例成为labelcolor属性的元组,因为它们不同 - 当它们相同时,图表和图例正常工作。

这是R中ggplot2的一个基本功能,我认为它可以在这里工作。我做错了什么或这是一个错误?

散景版0.12.0

使用图片更新:

enter image description here

1 个答案:

答案 0 :(得分:0)

包括bokeh.charts在内的Bar API已于2017年弃用并删除。从那时起,我们开展了大量工作来改进稳定且受支持的bokeh.plotting API,现在可以轻松创建多种分类和条形图。许多示例可以在“用户指南”的Handling Categorical Data章节中找到。

你想要用你的情节完成什么并不完全清楚。使用相同的数据,这里是按原点和气缸数分解的车辆计数图:

from bokeh.core.properties import value
from bokeh.plotting import figure, show
from bokeh.sampledata.autompg import autompg as df

# Bokeh categories are strings
df.cyl = [str(x) for x in df.cyl]
df.origin = [str(x) for x in df.origin]

# pivot to wide format
df = df.pivot_table(index='cyl', columns='origin', values='mpg', fill_value=0, aggfunc='count')

p = figure(title="Count by cylinder and origin", x_axis_label="Cylinders",
           x_range=sorted(df.index))
p.y_range.start = 0

p.vbar_stack(df.columns, x='cyl', width=0.9, color=["#c9d9d3", "#718dbf", "#e84d60"],
             source=df, legend=[value(x) for x in df.columns])

show(p)

enter image description here

对于更高级别的,以数据为中心的API,您可以使用更少的代码执行此操作,您可以查看构建在Bokeh之上的Holoviews

相关问题