我正在调整位于 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) 之间。更具体地说,图例变成了 label
和 color
属性的元组,因为它们不同 - 当它们相同时,图表和图例可以正常工作。
这是 R 中 ggplot2
的基本功能,我认为它可以在这里工作。我做错了什么还是这是一个错误?
bokeh 版本 0.12.0
更新图像:
最佳答案
bokeh.charts
API(包括 Bar
)已于 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)
对于更高级别、以数据为中心的 API,您可以使用更少的代码完成此操作,您可以查看 Holoviews它建立在 Bokeh 之上。
关于python - Bokeh 条形图 : color bars by category,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38364129/