facet_grid 无法在 python 的 ggplot 中工作?
facet_grid not working in ggplot for python?
我想根据汇总统计数据创建图表,而不是让 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)
但是如果我在 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__'
看来它现在已在 0.11.1 中修复。
我想根据汇总统计数据创建图表,而不是让 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)
但是如果我在 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__'
看来它现在已在 0.11.1 中修复。