Skip to content

Commit

Permalink
Fix data ordering in panels when facetting
Browse files Browse the repository at this point in the history
The bug appeared when facetting but there could be other
cases where it would show up. The real bug is in pandas,
how it sorts a dataframe by a categorical column.

fixes #26
  • Loading branch information
has2k1 committed Jun 29, 2017
1 parent 164003d commit b89befe
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 4 deletions.
6 changes: 5 additions & 1 deletion doc/changelog.rst
Expand Up @@ -12,6 +12,10 @@ v0.2.2
is only set to the value ``'direct'`` if predictions will
be made outside the data range.

- Fixed bug where facetting led to a reordering of the data. This
would manifest as a bug for ``geoms`` where order was important.
(:issue:`26`)

v0.2.1
------
*(2017-06-22)*
Expand All @@ -33,7 +37,7 @@ v0.2.1
the panels.

- Fixed bug in :class:`~plotnine.stat_density` where changing the
x limits lead to an exception (:issue: `22`)
x limits lead to an exception (:issue:`22`)


v0.2.0
Expand Down
4 changes: 3 additions & 1 deletion plotnine/facets/facet_grid.py
Expand Up @@ -142,6 +142,8 @@ def map(self, data, layout):
keys = join_keys(facet_vals, layout, vars)
data['PANEL'] = match(keys['x'], keys['y'], start=1)

data = data.sort_values('PANEL', kind='mergesort')

# matching dtype and
# the categories(panel numbers) for the data should be in the
# same order as the panels. i.e the panels are the reference,
Expand All @@ -150,7 +152,7 @@ def map(self, data, layout):
data['PANEL'],
categories=layout['PANEL'].cat.categories,
ordered=True)
data = data.sort_values('PANEL')

data.reset_index(drop=True, inplace=True)
return data

Expand Down
3 changes: 2 additions & 1 deletion plotnine/facets/facet_wrap.py
Expand Up @@ -137,13 +137,14 @@ def map(self, data, layout):
# assign each point to a panel
keys = join_keys(facet_vals, layout, self.vars)
data['PANEL'] = match(keys['x'], keys['y'], start=1)
data = data.sort_values('PANEL', kind='mergesort')

# matching dtype
data['PANEL'] = pd.Categorical(
data['PANEL'],
categories=layout['PANEL'].cat.categories,
ordered=True)
data = data.sort_values('PANEL')

data.reset_index(drop=True, inplace=True)
return data

Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 11 additions & 1 deletion plotnine/tests/test_geom_ribbon_area.py
Expand Up @@ -4,7 +4,7 @@
import pandas as pd

from plotnine import (ggplot, aes, geom_area, geom_ribbon,
scale_x_continuous, theme)
facet_wrap, scale_x_continuous, theme)

n = 4 # No. of ribbions in a vertical stack
m = 100 # Points
Expand Down Expand Up @@ -57,3 +57,13 @@ def test_area_aesthetics():
)

assert p + _theme == 'area_aesthetics'


def test_ribbon_facetting():
p = (ggplot(df, aes('x', ymin='ymin', ymax='ymax',
fill='factor(z)')) +
geom_ribbon() +
facet_wrap('~ z')
)

assert p + _theme == 'ribbon_facetting'

0 comments on commit b89befe

Please sign in to comment.