Reputation: 2626
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)
I'd like a bar chart with A and B the labels, the 2 bars of size 10 & 20:
This example renders nothing.
I tried various variations with values=, labels= etc but it seems data is always aggregated by default (agg='sum')
Is Bar object too high level to plot what I'd like (2 bars, size 10 & 20 named A and B ) or am I using the object incorrectly ? Has the DataFrame I'm using the wrong 'format', should i put the index in a column ? if someone could provide an exemple of that very simple chart that would be awesome !
Edit: got it working with this, too bad index of the DataFrame cant be the labels, that seemed logical to me :)
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)
Upvotes: 11
Views: 19420
Reputation: 34568
The other answers are outdated. The bokeh.charts
API has been deprecated and removed, it should no longer be used. For basic (and not-so-basic) bar charts, users should now use the stable bokeh.plotting
API as described in Handling Categorical Data. As an example:
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)
Which yields:
There are many more examples in the User's Guide link above showing how to achieve sorted, colormapped, stacked, grouped and nested bars, or work directly from Pandas DataFrames and GroupBys.
Upvotes: 22
Reputation: 85
tk's answer gave me a run error. I added an import
for plotting and changed the last statement from save
to show
. This worked for me.
from bokeh.plotting import figure, show, output_file
from bokeh.charts import Bar
import pandas as pd
dict = {'values':[10,20], 'names':['A','B']}
df = pd.DataFrame(dict)
p = Bar(df, 'names', values='values', title="test chart")
show(p)
Upvotes: -1
Reputation: 332
if you look at Bokeh docs you see that Bar aggregates data. So easier is to better redifine your dataframe. There may be other widgets than Bar to work with, but at least example below is quite easy to understand, and more importantly works:
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')
I let you do all the styling you need.
Hope this helps, t.
Upvotes: 0