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
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With