Skip to content

facet_grid: IndexError when dimension has only 1 value #129

Closed
@jdanbrown

Description

@jdanbrown
import pandas as pd
from plotnine import *

This breaks, I think because dimension a has only 1 value:

df = pd.DataFrame([
    {'x': 1, 'y': 1, 'a': 'foo', 'b': 'foo', 'c': 'c'},
    {'x': 2, 'y': 2, 'a': 'foo', 'b': 'foo', 'c': 'c'},
    {'x': 3, 'y': 1, 'a': 'foo', 'b': 'bar', 'c': 'c'},
])
ggplot(df) + geom_point(aes(x='x', y='y')) + facet_grid('a ~ .')
/Users/danb/miniconda3/lib/python3.6/site-packages/plotnine/facets/facet_grid.py:101: RuntimeWarning: None of the categories were found in values. Did you mean to use
'Categorical.from_codes(codes, categories)'?
  panel = pd.Categorical(panel, categories=range(1, n+1))
Traceback (most recent call last):
  File "/Users/danb/miniconda3/lib/python3.6/site-packages/IPython/core/formatters.py", line 693, in __call__
    printer.pretty(obj)
  File "/Users/danb/miniconda3/lib/python3.6/site-packages/IPython/lib/pretty.py", line 380, in pretty
    return _default_pprint(obj, self, cycle)
  File "/Users/danb/.ipython/profile_default/ipython_config.py", line 195, in <lambda>
    p.text(pformat(obj))
  File "/Users/danb/.ipython/profile_default/ipython_config.py", line 187, in <lambda>
    pformat = lambda x: _pp.pformat (x, width=_get_cols() or _get_cols_fallback_notebook, indent=2)
  File "/Users/danb/miniconda3/lib/python3.6/site-packages/pprintpp/__init__.py", line 140, in pformat
    return PrettyPrinter(indent=indent, width=width, depth=depth).pformat(object)
  File "/Users/danb/miniconda3/lib/python3.6/site-packages/pprintpp/__init__.py", line 298, in pformat
    self._format(object, state)
  File "/Users/danb/miniconda3/lib/python3.6/site-packages/pprintpp/__init__.py", line 474, in _format
    orepr = repr(object)
  File "/Users/danb/miniconda3/lib/python3.6/site-packages/plotnine/ggplot.py", line 95, in __repr__
    self.draw()
  File "/Users/danb/miniconda3/lib/python3.6/site-packages/plotnine/ggplot.py", line 188, in draw
    return self._draw(return_ggplot)
  File "/Users/danb/miniconda3/lib/python3.6/site-packages/plotnine/ggplot.py", line 195, in _draw
    self._build()
  File "/Users/danb/miniconda3/lib/python3.6/site-packages/plotnine/ggplot.py", line 303, in _build
    layers.compute_statistic(layout)
  File "/Users/danb/miniconda3/lib/python3.6/site-packages/plotnine/layer.py", line 87, in compute_statistic
    l.compute_statistic(layout)
  File "/Users/danb/miniconda3/lib/python3.6/site-packages/plotnine/layer.py", line 363, in compute_statistic
    data = self.stat.compute_layer(data, params, layout)
  File "/Users/danb/miniconda3/lib/python3.6/site-packages/plotnine/stats/stat.py", line 271, in compute_layer
    return groupby_apply(data, 'PANEL', fn)
  File "/Users/danb/miniconda3/lib/python3.6/site-packages/plotnine/utils.py", line 613, in groupby_apply
    lst.append(func(d, *args, **kwargs))
  File "/Users/danb/miniconda3/lib/python3.6/site-packages/plotnine/stats/stat.py", line 268, in fn
    pscales = layout.get_scales(pdata['PANEL'].iat[0])
  File "/Users/danb/miniconda3/lib/python3.6/site-packages/plotnine/facets/layout.py", line 140, in get_scales
    idx = self.layout.loc[bool_idx, 'SCALE_X'].values[0]
IndexError: index 0 is out of bounds for axis 0 with size 0

This works, I think because dimension b has >1 value:

df = pd.DataFrame([
    {'x': 1, 'y': 1, 'a': 'foo', 'b': 'foo', 'c': 'c'},
    {'x': 2, 'y': 2, 'a': 'foo', 'b': 'foo', 'c': 'c'},
    {'x': 3, 'y': 1, 'a': 'foo', 'b': 'bar', 'c': 'c'},
])
ggplot(df) + geom_point(aes(x='x', y='y')) + facet_grid('b ~ .')

image

This works, I think because dimension c has 1 value that's the same as the dimension name...

df = pd.DataFrame([
    {'x': 1, 'y': 1, 'a': 'foo', 'b': 'foo', 'c': 'c'},
    {'x': 2, 'y': 2, 'a': 'foo', 'b': 'foo', 'c': 'c'},
    {'x': 3, 'y': 1, 'a': 'foo', 'b': 'bar', 'c': 'c'},
])
ggplot(df) + geom_point(aes(x='x', y='y')) + facet_grid('c ~ .')

df-pd-dataframe-n-96562be6

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions