yhat / ggpy

ggplot port for python
http://yhat.github.io/ggpy/
BSD 2-Clause "Simplified" License
3.7k stars 573 forks source link

AttributeError: 'EvalEnvironment' object has no attribute 'add_outer_namespace' #457

Closed dalanmiller closed 8 years ago

dalanmiller commented 8 years ago

Just installed yhat Rodeo and tried to create a graph with ggplot (I also get this error when tried in iPython however):

ggplot(aes(x='factor(cyl)'), data=mtcars) + \
    geom_bar(fill='white', color='steelblue')

But I get this error below

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-9-d036619e195a> in <module>()
      3 
      4 
----> 5 ggplot(aes(x='factor(cyl)'), data=mtcars) +     geom_bar(fill='white', color='steelblue')
      6 
      7 

/Users/simius/anaconda/lib/python2.7/site-packages/ggplot/ggplot.pyc in __init__(self, aesthetics, data)
     64 
     65         self.aesthetics = aesthetics
---> 66         self.data = _apply_transforms(data, self.aesthetics)
     67 
     68         # defaults

/Users/simius/anaconda/lib/python2.7/site-packages/ggplot/ggplot.pyc in _apply_transforms(data, aes)
    546             env = EvalEnvironment.capture(eval_env=(aes.__eval_env__ or 1))
    547             # add factor as a special case
--> 548             env.add_outer_namespace({"factor":factor})
    549             try:
    550                 new_val = env.eval(name, inner_namespace=data)

AttributeError: 'EvalEnvironment' object has no attribute 'add_outer_namespace'

Any thoughts?

constinit commented 8 years ago

Also had this. Was a problem with cairo. Try:

sudo apt-get install libffi-dev

sudo pip install cairocffi

jankatins commented 8 years ago

Jikes :-(

https://github.com/pydata/patsy/commit/95361bd077aa8b78537376d049ec0e35ee28b3e6

jankatins commented 8 years ago

So the fix is to replace this code in ggplot.ggplot.py (https://github.com/yhat/ggplot/blob/master/ggplot/ggplot.py#L550)

            env = EvalEnvironment.capture(eval_env=(aes.__eval_env__ or 1))
            # add factor as a special case
-            env.add_outer_namespace({"factor":factor})
+            env = env.with_outer_namespace({"factor":factor})

But this also needs an a strict version dependency on patsy 4.0+

dalanmiller commented 8 years ago

@acconsta Didn't work for me ;( and already had it installed

dalanmiller commented 8 years ago

@JanSchulz After making that one line change I then get this error:

TypeError: pivot_table() got an unexpected keyword argument 'rows'
jankatins commented 8 years ago

Yea, thats another thing you need to change: https://github.com/yhat/ggplot/issues/428

dalanmiller commented 8 years ago

@janschulz do you know if there's a PR to fix this just yet?

I'm surprised more people aren't having this issue?

jankatins commented 8 years ago

Th eval env problem from this issue? No, not that I know. Maybe it is fixed in the big rewrite (#360), but nothing in master... The index/row problem is in in PR https://github.com/yhat/ggplot/pull/430, but unmerged.

dalanmiller commented 8 years ago

I don't understand how people aren't having this issue on the simplest of examples in ggplot? I also tried your suggestion @acconsta to no avail.

In [1]: from ggplot import *

In [2]: ggplot(aes(x="factor(cyl)"), data=mtcars) + geom_bar()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-2-97dacbd575ef> in <module>()
----> 1 ggplot(aes(x="factor(cyl)"), data=mtcars) + geom_bar()

/usr/local/lib/python3.5/site-packages/ggplot/ggplot.py in __init__(self, aesthetics, data)
     64
     65         self.aesthetics = aesthetics
---> 66         self.data = _apply_transforms(data, self.aesthetics)
     67
     68         # defaults

/usr/local/lib/python3.5/site-packages/ggplot/ggplot.py in _apply_transforms(data, aes)
    548             env = EvalEnvironment.capture(eval_env=(aes.__eval_env__ or 1))
    549             # add factor as a special case
--> 550             env.add_outer_namespace({"factor":factor})
    551             try:
    552                 new_val = env.eval(name, inner_namespace=data)

AttributeError: 'EvalEnvironment' object has no attribute 'add_outer_namespace'

This is in Python 3.5.0 using ipython 4.0.0.

Here's my pip freeze:

appnope==0.1.0
brewer2mpl==1.4.1
decorator==4.0.4
Flask==0.10.1
ggplot==0.6.8
gnureadline==6.3.3
google-api-python-client==1.4.2
gunicorn==19.3.0
httplib2==0.9.2
ipython==4.0.0
ipython-genutils==0.1.0
itsdangerous==0.24
Jinja2==2.8
MarkupSafe==0.23
matplotlib==1.4.3
nose==1.3.7
numpy==1.10.1
oauth2client==1.5.1
pandas==0.17.0
path.py==8.1.2
patsy==0.4.0
pexpect==4.0.1
pickleshare==0.5
ptyprocess==0.5
py-stackexchange==2.2.4
pyasn1==0.1.9
pyasn1-modules==0.0.8
pyparsing==2.0.3
python-dateutil==2.4.2
pytz==2015.6
requests==2.7.0
rethinkdb==2.1.0.post2
rsa==3.2
scipy==0.16.0
simplegeneric==0.8.1
simplejson==3.8.0
six==1.10.0
slackclient==0.16
statsmodels==0.6.1
traitlets==4.0.0
uritemplate==0.6
websocket-client==0.32.0
Werkzeug==0.10.4
wheel==0.24.0
naught101 commented 8 years ago

This is already fixed in https://github.com/has2k1/ggplot/pull/1, I think..

olarerin commented 8 years ago

So any solution to this bug yet? I edited ggplot.py as suggested but no luck.

ColinAnthony commented 8 years ago

Not sure if this is relevant to your error, but I got this error when referencing a string as a list index in my plotting function:

def plotdgg(x, y, df):
    ggplot(aes(x=x[0], y=y), data=df)

produced this error: File "/usr/local/lib/python2.7/dist-packages/ggplot/ggplot.py", line 66, in init self.data = _apply_transforms(data, self.aesthetics) File "/usr/local/lib/python2.7/dist-packages/ggplot/ggplot.py", line 550, in _apply_transforms env.add_outer_namespace({"factor":factor}) AttributeError: 'EvalEnvironment' object has no attribute 'add_outer_namespace'

Using this function call, corrected the error:

def plotdgg(x, y, df):
    ggplot(aes(x=x, y=y), data=df)

Ubuntu15.04, python3.4 (anaconda), ggplot 0.6.8

janssen commented 8 years ago

But a string is supposed to work. Extremely annoying. Any solution yet?

OliveZh commented 8 years ago

still wating

cnglen commented 8 years ago

I got the same error

In [45]: ggplot(mtcars, aes(x='wt', y='mpg', colour='factor(cyl)', size='mpg', linetype='factor(cyl)'))

AttributeError Traceback (most recent call last)

in () ----> 1 ggplot(mtcars, aes(x='wt', y='mpg', colour='factor(cyl)', size='mpg', linetype='factor(cyl)')) /usr/local/lib/python3.4/dist-packages/ggplot/ggplot.py in **init**(self, aesthetics, data) 64 65 self.aesthetics = aesthetics ---> 66 self.data = _apply_transforms(data, self.aesthetics) 67 68 # defaults /usr/local/lib/python3.4/dist-packages/ggplot/ggplot.py in _apply_transforms(data, aes) 548 env = EvalEnvironment.capture(eval_env=(aes.__eval_env__ or 1)) 549 # add factor as a special case --> 550 env.add_outer_namespace({"factor":factor}) 551 try: 552 new_val = env.eval(name, inner_namespace=data) AttributeError: 'EvalEnvironment' object has no attribute 'add_outer_namespace'
kstohr commented 8 years ago

Getting the same error on a time series. Any fix? Plotting on dataframe with the date indexed as a time series throws an error.

ggplot(aes(x='as_of', y='likes', colour='Name'), data=ts) + geom_line() + stat_smooth(span=0.10)

However, plotting the same data on the data without being indexed by date, works.

ggplot(aes(x='as_of', y='likes', colour='Name'), data=df) + geom_line() + stat_smooth(span=0.10)

jupyter notebook, python 2.7, ubuntu

jankatins commented 8 years ago

The fix is the comment above: https://github.com/yhat/ggplot/issues/457#issuecomment-148017190 or in https://github.com/yhat/ggplot/pull/462

matkoniecz commented 8 years ago
data =[[1, 2]]
from pandas import DataFrame
powd = DataFrame(data, columns=['TIME', 'Watts'])

from ggplot import *

gg = ggplot(aes(x='date', y='beef'), data=powd) + \
    geom_point(color='lightblue') + \
    stat_smooth(span=.15, color='black', se=True) + \
    ggtitle("Beef: It's What's for Dinner") + \
    xlab("X") + \
    ylab("Y")

#ggsave(gg, filename='test.png')
gg.draw()
ggsave(filename='test.png')

crashes with

Traceback (most recent call last):
  File "test.py", line 11, in <module>
    xlab("X") + \
  File "/usr/local/lib/python3.4/dist-packages/ggplot/ggplot.py", line 66, in __init__
    self.data = _apply_transforms(data, self.aesthetics)
  File "/usr/local/lib/python3.4/dist-packages/ggplot/ggplot.py", line 550, in _apply_transforms
    env.add_outer_namespace({"factor":factor})
AttributeError: 'EvalEnvironment' object has no attribute 'add_outer_namespace'

https://github.com/yhat/ggplot/issues/457#issuecomment-147898808 failed to improve the situation.

dalanmiller commented 8 years ago

Just loaded up ggplot to try and make some graphs, and got this error again, which I Google'd to come back to this issue I created six months ago :cry:.

@JanSchulz did you stop seeing this issue?

jankatins commented 8 years ago

@dalanmiller No, I still have it when I install ggplot...

IMO ggplot for python is dead :-/

dalanmiller commented 8 years ago

@JanSchulz has something replaced it?

jankatins commented 8 years ago

matplotlib itself has now support for a data argument (plt.scatter(x="xvar", y="yvar", data=pd_dataframe)) and there is seaborn...

There is also now a solid r support for the jupyter notebook (irkernel), so plotting ggplot2 in the notebook is possible.

Maybe in the future @has2k1 will finish his rewrite (#360)...

matkoniecz commented 8 years ago

Matplotlib >= 1.4 also has matplotlib.style.use('ggplot') what nicely solves problem of hilariously ugly graphs.

naught101 commented 8 years ago

Man, that sucks. GoG notation is so useful. And MPL will probably never have support for facetting and maybe a bunch of other stuff...

timosong commented 8 years ago

Same exact error when I'm just doing the first line of code from the bar chart example: http://ggplot.yhathq.com/docs/geom_bar.html

I also don't understand how more people are not getting the same error .

Starting to think it may be worth the hassle to use python to format my dataframe and then pipe it to R and work from ggplot there.

janssen commented 8 years ago

I think ggplot is just dead. I switched to pandas/seaborn.

glamp commented 8 years ago

This is fixed in the next release which will happen later this week https://github.com/yhat/ggplot/tree/overhaul

jankatins commented 8 years ago

Why not take #360 (or better https://github.com/has2k1/ggplot/commits/rewrite) and go from there? From looking at the code, a lot of things work there and it comes very close to how ggplot works underneath (e.g. themes and how to compose new ones)

Re stat_indentity:

they're gone. They didn't even come close to working (also what does stat_identity even do) so they're gone.

From http://docs.ggplot2.org/0.9.3.1/geom_bar.html

# When the data contains y values in a column, use stat="identity"
library(plyr)
# Calculate the mean mpg for each level of cyl
mm <- ddply(mtcars, "cyl", summarise, mmpg = mean(mpg))
ggplot(mm, aes(x = factor(cyl), y = mmpg)) + geom_bar(stat = "identity")
glamp commented 8 years ago

fixed in 0.9.3