Search code examples
python-3.xpython-ggplot

Getting ggplot (python) error NameError: name 'stat_function' is not defined


I'm trying to make a plot using ggplot and I'm not sure why I'm getting this error. Here's the code:

from ggplot import *

ggplot(counts, aes(x='pred_prob',y='true_prob',size='count')) + \
    geom_point(color='blue') + \
    stat_function(fun=lambda x: x, color='red') + \
    xlim(-0.05,  1.05) + ylim(-0.05,1.05) 

Here's the error I get:

NameError                                 Traceback (most recent call last)
<ipython-input-71-fdefd49237a1> in <module>()
  2 
  3 baseline = np.mean(is_churn)
----> 4 ggplot(counts, aes(x='pred_prob',y='true_prob',size='count')) + geom_point(color='blue') + stat_function(fun=lambda x: x, color='red') + xlim(-0.05,  1.05) + ylim(-0.05,1.05)
NameError: name 'stat_function' is not defined

I'm not sure why I'm getting this error. Any ideas? I'm using python 3.5.2 and ggplot version 0.11.5


Solution

  • I got the same exact error from the same exact example code

    seems the ggplot library had gone through a refactoring.

    To install that version use:

    pip install -U git+https://github.com/yhat/[email protected]
    

    and then

    from ggplot import *
    from ggplot.stats.stat_function import stat_function # added line
    import pandas as pd
    %matplotlib inline
    
    ggplot(pd.DataFrame({'x':[0,500]}), aes(x='x')) + \
        stats.stat_function(fun = lambda x: 50. / (x + 50.)) + \
        ggtitle("Epsilon decay over time") + \
        xlab("amount of feedback") + ylab("epsilon (probability of testing)")
    

    It will produce the same figure as in the post

    enter image description here