简体   繁体   English

使用OrderedDict但在图形月份中出现故障

[英]Using OrderedDict but in graph months out of order

I have a pivot_table being read into a bar plot using bokeh. 我有一个使用bokeh读取的数据透视表。 The problem is that the months are plotted out of order, even though I am using OrderedDict. 问题是,即使我使用的是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. 我希望他们从1月开始,一直到12月。

在此处输入图片说明

You can use OrderedDict like this: 您可以这样使用OrderedDict:

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. OrderedDict是可以记住首次插入键的顺序的字典。 A dict (kwargs) doesn't have a specified order. 字典(kwargs)没有指定的顺序。

The problem is that the parameters are collected by OrderedDict as a dictionary (kwargs), which is not ordered. 问题在于参数是由OrderedDict收集为字典(kwargs)的,而不是有序的。

Do this instead: 改为这样做:

months = OrderedDict()
months['Jan'] = pivot_table[1].astype(float).values
months['Feb'] = pivot_table[2].astype(float).values
...

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)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM