简体   繁体   English

如何使用Vincent绘制大熊猫groupby结果

[英]how to plot pandas groupby result using vincent

I am using the below statement to do groupby using multiplt keys 我正在使用以下语句使用multiplt键进行分组

CountryBrowser = impressions.groupby(['Country', 'Browser']).size()

The results are as follows 结果如下

 Country      Browser                           
 Afghanistan  Firefox 3.Any                          1
              Firefox Other.Other                    5
              Google Chrome                          5
              Safari (iPad) Any.Any                  1
              Safari 5.Any                           1
 Albania      Firefox Other.Other                    3
              Google Chrome                         15
              Microsoft Internet Explorer 10.Any     1
 Algeria      Firefox Other.Other                   31
              Google Chrome                         31
              Microsoft Internet Explorer 10.Any     1
              Opera 11.Any                           1
              Safari (iPad) Any.Any                  1

I want to plot it using vincent OR Matplotlib, I have the below code and it doesn't work for me 我想使用vincent或Matplotlib绘制它,我有以下代码,但对我不起作用

gbar = vincent.GroupedBar(CountryBrowser)
gbar.axis_titles(x='Country', y='Impressions')
gbar.legend(title='Browser')
gbar.display()

https://vincent.readthedocs.org/en/latest/quickstart.html#grouped-bar https://vincent.readthedocs.org/zh-CN/latest/quickstart.html#grouped-bar

For a grouped bar, vincent expects a 1-level index, and uses the columns for the groups. 对于已分组的条形图,vincent期望一个1级索引,并将这些列用于组。 You've got a MutliIndex with a single column. 您只有一个MutliIndex列。 Fortunately you can get to what vincent expects with a simple unstack() : 幸运的是,您可以通过简单的unstack()达到vincent的期望:

In [8]: idx = pd.MultiIndex.from_tuples([('a', 1), ('a', 2), ('b', 1), ('b', 2)])

In [9]: s = pd.Series(np.random.uniform(size=4), index=idx)

In [10]: s  # This is what you have
Out[10]: 
a  1    0.921138
   2    0.677708
b  1    0.916239
   2    0.130728
dtype: float64

In [11]: s.unstack()  # This is what you want
Out[11]: 
          1         2
a  0.921138  0.677708
b  0.916239  0.130728

[2 rows x 2 columns]

In [13]: group = vincent.GroupedBar(s.unstack())

In [14]: group.display()
<IPython.core.display.HTML at 0x103faa850>
<IPython.core.display.Javascript at 0x104957850>

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

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