基本散景条形图

eur*_*i10 7 bokeh

In[21]: from bokeh.charts import Bar, output_file, show
In[22]: dict = {'sec': {u'A': 10, u'B': 20}}
In[23]: df = pd.DataFrame(dict)
In[24]: df
Out[24]: 
   sec
A   10
B   20
In[25]: output_file("bar.html")
In[26]: p = Bar(df)
In[27]: show(p)
Run Code Online (Sandbox Code Playgroud)

我想要一个带有A和B标签的条形图,2个尺寸为10和20的条形图: 在此输入图像描述

此示例不呈现任何内容.

我尝试使用values =,labels = etc进行各种变化,但似乎数据总是默认汇总(agg ='sum')

Bar对象是否太高,无法绘制我想要的内容(2个条形,大小10和20,名为A和B)或者我是否错误地使用了对象?DataFrame我使用了错误的'格式',我应该把索引放在一列吗?如果有人可以提供一个非常简单的图表的例子,那就太棒了!

编辑:得到它使用这个,太糟糕的DataFrame索引不能是标签,这对我来说似乎合乎逻辑:)

import pandas as pd
from bokeh.charts import Bar
from bokeh.io import output_notebook, show
output_notebook()
dict = {'values': {u'A': 10, u'B': 20}}
df = pd.DataFrame(dict)
df['label'] = df.index
df
p = Bar(df, values='values',label='label')
show(p)
Run Code Online (Sandbox Code Playgroud)

big*_*dot 15

其他答案已过时.该bokeh.chartsAPI已被弃用并删除,应该不再被使用.对于基本(而非基本)条形图,用户现在应该使用处理分类数据中bokeh.plotting描述的稳定API .举个例子:

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

output_file("bars.html")

fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']

p = figure(x_range=fruits, plot_height=250, title="Fruit Counts")
p.vbar(x=fruits, top=[5, 3, 4, 2, 4, 6], width=0.9)

p.xgrid.grid_line_color = None
p.y_range.start = 0

show(p)
Run Code Online (Sandbox Code Playgroud)

产量:

在此输入图像描述

上面的"用户指南"链接中有更多示例,显示如何实现彩色贴图,堆叠,分组和嵌套条,或直接使用Pandas DataFrames和GroupBys.


tk.*_*tk. 0

如果您查看Bokeh 文档, 您会发现 Bar 聚合了数据。更好地重新定义数据框更容易。除了 Bar 之外,可能还有其他小部件可以使用,但至少下面的示例很容易理解,更重要的是可以使用:

from bokeh.charts import Bar
from bokeh.io import save
import pandas as pd
dict = {'values':[10,20], 'names':['A','B']}
df = pd.DataFrame(dict)

p = Bar(df, 'names', values='values', title="test chart")
save(p,'test3.html')
Run Code Online (Sandbox Code Playgroud)

我让你做你需要的所有造型。

希望这有帮助,t。