Reputation: 2117
I have a pivot_table being read into a bar plot using bokeh. The problem is that the months are plotted out of order, even though I am using OrderedDict. Here is a sample of the pivot_table
pivot_table.head(3)
Out[45]:
Month 1 2 3 4 5 6 7 8 9 10 11 CompanyName
Company1 17 30 29 39 15 26 24 12 36 21 18
Company2 4 11 13 22 35 29 15 18 29 31 17
Company3 11 8 25 24 7 15 20 0 21 12 12
Month 12
CompanyName
Company1 15
Company2 14
Company3 17
It is being read in using this code:
# get the months
Jan = pivot_table[1].astype(float).values
Feb = pivot_table[2].astype(float).values
Mar = pivot_table[3].astype(float).values
Apr = pivot_table[4].astype(float).values
May = pivot_table[5].astype(float).values
Jun = pivot_table[6].astype(float).values
Jul = pivot_table[7].astype(float).values
Aug = pivot_table[8].astype(float).values
Sep = pivot_table[9].astype(float).values
Oct = pivot_table[10].astype(float).values
Nov = pivot_table[11].astype(float).values
Dec = pivot_table[12].astype(float).values
# build a dict containing the grouped data
months = OrderedDict(Jan=Jan, Feb=Feb, Mar=Mar, Apr=Apr, May=May,Jun=Jun,Jul=Jul,Aug=Aug,Sep=Sep,Oct=Oct,Nov=Nov,Dec=Dec)
palette = brewer["PuBuGn"][8]
output_file("stacked_bar.html")
bar = Bar(months, Companies, title="Stacked bars", palette = palette, legend = "top_right", width = 1200, height=900, stacked=True)
show(bar)
The problem is that this is the output. Although beautiful, the months are out of order. I would like them to start at Jan on the bottom up through Dec.
Upvotes: 0
Views: 165
Reputation: 179
For the months try this,
months = OrderedDict((
("Jan",Jan)
,("Feb",Feb)
,("Mar",Mar)
,("Apr",Apr)
,("May",May)
,("Jun",Jun)
,("Jul",Jul)
,("Aug",Aug)
,("Sep",Sep)
,("Oct",Oct)
,("Nov",Nov)
,("Dec",Dec)))
Upvotes: 0
Reputation: 18513
You can use OrderedDict like this:
months = OrderedDict([('Jan', Jan), ('Feb', Feb), ('Mar', Mar)])
Or:
months = OrderedDict()
months['Jan'] = Jan
months['Feb'] = Feb
months['Mar'] = Mar
An OrderedDict is a dict that remembers the order that keys were first inserted. A dict (kwargs) doesn't have a specified order.
Upvotes: 2
Reputation: 11580
The problem is that the parameters are collected by OrderedDict as a dictionary (kwargs), which is not ordered.
Do this instead:
months = OrderedDict()
months['Jan'] = pivot_table[1].astype(float).values
months['Feb'] = pivot_table[2].astype(float).values
...
Upvotes: 0