ghostopened on Jul 18, 2018 · GitHub

Mapping a column to colors gives an error. I tried using a pandas.Categorical column instead, which is the equivalent of R's factor, but that didn't work either.

I can solve it with aes(color='factor(variable)'). The solution is in the tests, but this issue seems hard to diagnose: e.g. here and here (and myself).

Some ideas:

  1. More explicit error message
  2. Accept pandas.Categorical column. This seems to me more pythonic than color='factor(var)'.
  3. Automatically cast to factor [I don't like this idea]

Plotnine '0.3.0+76.gdb4acba'
Python 3.6.3
Ubuntu 17.10

import pandas as pd
from plotnine import ggplot, aes, geom_density
ex = pd.DataFrame({'cat': {0: 0.0, 1: 0.5, 2: 1.0, 3: 0.0, 4: 0.5, 5: 1.0, 6: 0.0, 7: 0.5, 8: 1.0, 9: 0.0, 10: 0.5}, 'val': {0: 0.6954301381597963, 1: 1.4310605976362851, 2: 1.0279771488425007, 3: 1.0687462384906576, 4: 0.8584934619489705, 5: 1.4812745102981542, 6: 0.6619235472465034, 7: 0.9035161527013149, 8: 1.0093593051054468, 9: 1.2173641002699964, 10: 0.830359984193685}})
p = ggplot(aes(x='val', color='cat'), data=ex) + geom_density()
p.save('out.png')
/home/user/tmp/venv/src/plotnine/plotnine/ggplot.py:708: UserWarning: Saving 6.4 x 4.8 in image.
  from_inches(height, units), units))
/home/user/tmp/venv/src/plotnine/plotnine/ggplot.py:709: UserWarning: Filename: out.png
  warn('Filename: {}'.format(filename))
Traceback (most recent call last):
  File "demo.py", line 5, in <module>
    p.save('out.png')
  File "/home/user/tmp/venv/src/plotnine/plotnine/ggplot.py", line 730, in save
    raise err
  File "/home/user/tmp/venv/src/plotnine/plotnine/ggplot.py", line 727, in save
    _save()
  File "/home/user/tmp/venv/src/plotnine/plotnine/ggplot.py", line 713, in _save
    fig = figure[0] = self.draw()
  File "/home/user/tmp/venv/src/plotnine/plotnine/ggplot.py", line 190, in draw
    return self._draw(return_ggplot)
  File "/home/user/tmp/venv/src/plotnine/plotnine/ggplot.py", line 221, in _draw
    raise err
  File "/home/user/tmp/venv/src/plotnine/plotnine/ggplot.py", line 213, in _draw
    self._draw_legend()
  File "/home/user/tmp/venv/src/plotnine/plotnine/ggplot.py", line 416, in _draw_legend
    legend_box = self.guides.build(self)
  File "/home/user/tmp/venv/src/plotnine/plotnine/guides/guides.py", line 136, in build
    gdefs = self.train(plot)
  File "/home/user/tmp/venv/src/plotnine/plotnine/guides/guides.py", line 206, in train
    guide = guide.train(scale)
  File "/home/user/tmp/venv/src/plotnine/plotnine/guides/guide_colorbar.py", line 77, in train
    breaks = scale.get_breaks(strict=True)
  File "/home/user/tmp/venv/src/plotnine/plotnine/scales/scale.py", line 636, in get_breaks
    cond = (breaks >= limits[0]) & (limits[1] >= breaks)
TypeError: '>=' not supported between instances of 'list' and 'int'

Read the original on github.com ↗