facet_grid无法在ggplot for python中工作?

时间:2016-07-29 02:31:47

标签: python r ggplot2 python-ggplot

我想根据汇总统计信息创建绘图,而不是让ggplot为我聚合。在尝试使用geom_violin()失败后,我终于决定计算百分位数并创建条形图。即便如此,我似乎无法生产它们。

在R中,这有效:

library(dplyr)
library(ggplot2)

p = seq(0,1,length.out=11)
diaa <- diamonds[,c("cut","color","table")]
diab <- diaa %>% group_by(cut,color) %>% do(data.frame(p=p,stats=quantile(.$table,probs=p)))

ggplot(diab,aes(x=p,weight=stats)) + geom_bar() + facet_grid(cut ~ color)

enter image description here

但是如果我在python中尝试相同的东西:

from ggplot import *

diaa = diamonds[['cut','color','table']]
diab = diaa.groupby(['cut','color']).quantile([x/100.0 for x in range(0,100,5)])
diab.reset_index(inplace=True)
diab.columns = ['cut','color','p','stats']
ggplot(diab,aes(x='p',weight='stats')) + geom_bar() + facet_grid('color','cut')

它会引发很多错误。我在滥用它吗?

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/home/mccarthy/.pyenv/versions/2.7.12/envs/plotting/lib/python2.7/site-packages/IPython/core/formatters.pyc in __call__(self, obj)
    668                 type_pprinters=self.type_printers,
    669                 deferred_pprinters=self.deferred_printers)
--> 670             printer.pretty(obj)
    671             printer.flush()
    672             return stream.getvalue()

/home/mccarthy/.pyenv/versions/2.7.12/envs/plotting/lib/python2.7/site-packages/IPython/lib/pretty.pyc in pretty(self, obj)
    381                             if callable(meth):
    382                                 return meth(obj, self, cycle)
--> 383             return _default_pprint(obj, self, cycle)
    384         finally:
    385             self.end_group()

/home/mccarthy/.pyenv/versions/2.7.12/envs/plotting/lib/python2.7/site-packages/IPython/lib/pretty.pyc in _default_pprint(obj, p, cycle)
    501     if _safe_getattr(klass, '__repr__', None) not in _baseclass_reprs:
    502         # A user-provided repr. Find newlines and replace them with p.break_()
--> 503         _repr_pprint(obj, p, cycle)
    504         return
    505     p.begin_group(1, '<')

/home/mccarthy/.pyenv/versions/2.7.12/envs/plotting/lib/python2.7/site-packages/IPython/lib/pretty.pyc in _repr_pprint(obj, p, cycle)
    692     """A pprint that just redirects to the normal repr function."""
    693     # Find newlines and replace them with p.break_()
--> 694     output = repr(obj)
    695     for idx,output_line in enumerate(output.splitlines()):
    696         if idx:

/home/mccarthy/.pyenv/versions/2.7.12/envs/plotting/lib/python2.7/site-packages/ggplot/ggplot.pyc in __repr__(self)
    117 
    118     def __repr__(self):
--> 119         self.make()
    120         # this is nice for dev but not the best for "real"
    121         if os.environ.get("GGPLOT_DEV"):

/home/mccarthy/.pyenv/versions/2.7.12/envs/plotting/lib/python2.7/site-packages/ggplot/ggplot.pyc in make(self)
    600                                 facet_filter = facetgroup[self.facets.facet_cols].iloc[0].to_dict()
    601                                 for k, v in facet_filter.items():
--> 602                                     mask = (mask) & (df[k]==v)
    603                                 df = df[mask]
    604 

TypeError: 'NoneType' object has no attribute '__getitem__'

1 个答案:

答案 0 :(得分:0)

现在看起来已经修复了0.11.1。

https://github.com/yhat/ggplot/issues/515