2016-07-29 5 views
1

Ich möchte Plots basierend auf summarischen Statistiken erstellen, anstatt ggplot für mich aggregieren zu lassen. Nachdem ich mit geom_violin() versucht und versagt hatte, entschied ich mich schließlich für das Berechnen von Perzentilen und das Erstellen von Barplots. Trotzdem kann ich sie nicht produzieren.facet_grid funktioniert nicht in ggplot für Python?

In R das funktioniert:

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

Aber wenn ich die gleiche Sache in Python versuchen:

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

Es wirft viele Fehler. Misse ich es falsch?

--------------------------------------------------------------------------- 
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__' 

Antwort

Verwandte Themen