Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Choosing order of bars in Bokeh bar chart

As part of trying to learn to use Bokeh I am trying to make a simple bar chart. I am passing the labels in a certain order (days of the week) and Bokeh seems to be sorting them alphabetically. How can I have the bars show up in the order of the original list?

from bokeh.plotting import show
from bokeh.io import output_notebook
from bokeh.charts import Bar
from collections import OrderedDict
import calendar 

output_notebook()

data = OrderedDict()
data['values'] = [2,3,4,5,6,7,8] #values only ascending to make correct graph clear
data['days'] = [calendar.day_name[i-1] for i in range(7)]
p = Bar(data, label='days', values='values', 
         title='OrderedDict Input',xlabel="Day", ylabel="Value")
show(p)

Output generated

like image 776
Rachel Avatar asked Oct 14 '15 21:10

Rachel


1 Answers

Note from Bokeh project maintainers: This answer refers to an obsolete and deprecated API that should not be used in any new code. For information about creating bar charts with modern and fully supported Bokeh APIs, see other responses.


Here's how to retain original order of the labels in your example using the Charts interface, tested with Bokeh 0.11.1.

from bokeh.plotting import show
from bokeh.io import output_notebook
from bokeh.charts import Bar
from collections import OrderedDict
import calendar 
from bokeh.charts.attributes import CatAttr

output_notebook()

data = OrderedDict()
data['values'] = [2,3,4,5,6,7,8] #values only ascending to make correct graph clear
data['days'] = [calendar.day_name[i-1] for i in range(7)]
p = Bar(data, label=CatAttr(columns=['days'], sort=False), 
        values='values',title='OrderedDict Input',xlabel="Day", ylabel="Value")
show(p)
like image 183
user666 Avatar answered Sep 17 '22 11:09

user666