In [1]:
from ggplot import *
import pandas as pd
import numpy as np

In [2]:
ggplot(aes(x='wt', y='mpg', color='factor(cyl)'), data=mtcars) +\
    geom_point()


Out[2]:
<ggplot: (279917357)>

In [14]:
import matplotlib.pyplot as plt

colors = ["red", "blue", "green"]
i = 0
for name, frame in mtcars.groupby("cyl"):
    plt.scatter(frame.wt, frame.mpg, c=colors[i], label=name)
    i += 1
plt.xlabel("wt")
plt.ylabel("mpg")
plt.legend(title="Cyl")


Out[14]:
<matplotlib.legend.Legend at 0x10b03af50>

In [2]:
ggplot(aes(x='carat', y='price', color='cut', shape='color'), data=diamonds) +\
    geom_jitter() +\
    scale_color_brewer(type='qual')


Out[2]:
<ggplot: (282072389)>

stat_smooth


In [3]:
ggplot(aes(x='date', y='beef'), data=meat) +\
    geom_point() +\
    stat_smooth(method='ma') # defaults to lowess


Out[3]:
<ggplot: (282719933)>

In [4]:
ggplot(aes(x='date', y='beef'), data=meat) +\
    geom_point() +\
    stat_smooth(span=0.2)


Out[4]:
<ggplot: (282082533)>

In [5]:
ggplot(aes(x='date', y='beef'), data=meat) +\
    geom_point() +\
    stat_smooth(method='lm', colour='coral', se=False)


Out[5]:
<ggplot: (283471113)>

stat_function


In [6]:
p = ggplot(pd.DataFrame({'x':np.arange(10)}),aes(x='x'))
p += stat_function(fun=np.sin,color="red")
p += stat_function(fun=np.cos,color="blue")
p


Out[6]:
<ggplot: (283471517)>

Working w/ Long Data


In [7]:
meat_lng = pd.melt(meat, id_vars=['date'])

In [15]:
ggplot(aes(x='date', y='value', group='variable'), data=meat_lng) +\
    geom_line()
# goot but let's add some color


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-15-6d16d9212fba> in <module>()
----> 1 ggplot(aes(x='date', y='value', group='variable'), data=meat_lng) +    geom_line()
      2 # goot but let's add some color

NameError: name 'meat_lng' is not defined

In [9]:
ggplot(aes(x='date', y='value', color='variable'), data=meat_lng) +\
    geom_line()
# good but let's smooth out the curves


Out[9]:
<ggplot: (282710845)>

In [10]:
ggplot(aes(x='date', y='value', color='variable'), data=meat_lng) +\
    stat_smooth(method='ma', span=12, se=False)
# much better :)


Out[10]:
<ggplot: (282719713)>

Faceting

facet_wrap


In [11]:
ggplot(aes(x='date', y='value', color='variable'), data=meat_lng) +\
    stat_smooth(method='ma', span=12, se=False) +\
    facet_wrap("variable")


Out[11]:
<ggplot: (283476333)>

facet_grid


In [12]:
ggplot(aes(x="price"), data=diamonds) +\
    geom_histogram() +\
    facet_grid("cut", "clarity")


---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-12-531541d60bbc> in <module>()
----> 1 ggplot(aes(x="price"), data=diamonds) +    geom_histogram() +    facet_grid("cut", "clarity")

/usr/local/lib/python2.7/site-packages/IPython/core/displayhook.pyc in __call__(self, result)
    245             self.start_displayhook()
    246             self.write_output_prompt()
--> 247             format_dict, md_dict = self.compute_format_data(result)
    248             self.write_format_data(format_dict, md_dict)
    249             self.update_user_ns(result)

/usr/local/lib/python2.7/site-packages/IPython/core/displayhook.pyc in compute_format_data(self, result)
    155 
    156         """
--> 157         return self.shell.display_formatter.format(result)
    158 
    159     def write_format_data(self, format_dict, md_dict=None):

/usr/local/lib/python2.7/site-packages/IPython/core/formatters.pyc in format(self, obj, include, exclude)
    150             md = None
    151             try:
--> 152                 data = formatter(obj)
    153             except:
    154                 # FIXME: log the exception

/usr/local/lib/python2.7/site-packages/IPython/core/formatters.pyc in __call__(self, obj)
    479                 type_pprinters=self.type_printers,
    480                 deferred_pprinters=self.deferred_printers)
--> 481             printer.pretty(obj)
    482             printer.flush()
    483             return stream.getvalue()

/usr/local/lib/python2.7/site-packages/IPython/lib/pretty.pyc in pretty(self, obj)
    360                             if callable(meth):
    361                                 return meth(obj, self, cycle)
--> 362             return _default_pprint(obj, self, cycle)
    363         finally:
    364             self.end_group()

/usr/local/lib/python2.7/site-packages/IPython/lib/pretty.pyc in _default_pprint(obj, p, cycle)
    480     if getattr(klass, '__repr__', None) not in _baseclass_reprs:
    481         # A user-provided repr.
--> 482         p.text(repr(obj))
    483         return
    484     p.begin_group(1, '<')

/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/ggplot-0.5.7-py2.7.egg/ggplot/ggplot.pyc in __repr__(self)
    108     def __repr__(self):
    109         """Print/show the plot"""
--> 110         figure = self.draw()
    111         # We're going to default to making the plot appear when __repr__ is
    112         # called.

/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/ggplot-0.5.7-py2.7.egg/ggplot/ggplot.pyc in draw(self)
    210                         for geom in self.geoms:
    211                             ax = plt.gca()
--> 212                             callbacks = geom.plot_layer(frame, ax)
    213                         axis_extremes[_iter] = [min(plt.xlim()), max(plt.xlim()),
    214                                                 min(plt.ylim()), max(plt.ylim())]

/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/ggplot-0.5.7-py2.7.egg/ggplot/geoms/geom.pyc in plot_layer(self, data, ax)
    113         _cols = set(data.columns) & set(self.manual_aes)
    114         data = data.drop(_cols, axis=1)
--> 115         data = self._calculate_stats(data)
    116         self._verify_aesthetics(data)
    117         _needed = self.valid_aes | self._extra_requires

/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/ggplot-0.5.7-py2.7.egg/ggplot/geoms/geom.pyc in _calculate_stats(self, data)
    273                 new_data = new_data.append(_data, ignore_index=True)
    274         else:
--> 275             new_data = self._stat._calculate(data)
    276 
    277         # some geoms expect a sorted x domain

/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/ggplot-0.5.7-py2.7.egg/ggplot/stats/stat_bin.pyc in _calculate(self, data)
     90             empty_bins = set(range(len(x))) - set(x_assignments)
     91             for _b in empty_bins:
---> 92                 _wfreq_table[_b] = 0
     93             _wfreq_table = _wfreq_table.sort_index()
     94 

/usr/local/lib/python2.7/site-packages/pandas/core/series.pyc in __setitem__(self, key, value)
    829                 return
    830 
--> 831             raise KeyError('%s not in this series!' % str(key))
    832         except TypeError, e:
    833             # python 3 type errors should be raised

KeyError: '14 not in this series!'
/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/ggplot-0.5.7-py2.7.egg/ggplot/ggplot.py:198: RuntimeWarning: Facetting is currently not supported with geom_bar. See
                    https://github.com/yhat/ggplot/issues/196 for more information
  warnings.warn(msg, RuntimeWarning)
stat_bin: binwidth defaulted to range/30.
    Use 'binwidth = x' to adjust this.

In [13]:
ggplot(aes(x="price"), data=diamonds) +\
    geom_histogram() +\
    facet_grid("cut", "clarity", scales="free_y")


---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-13-f9a8f1b9e050> in <module>()
----> 1 ggplot(aes(x="price"), data=diamonds) +    geom_histogram() +    facet_grid("cut", "clarity", scales="free_y")

/usr/local/lib/python2.7/site-packages/IPython/core/displayhook.pyc in __call__(self, result)
    245             self.start_displayhook()
    246             self.write_output_prompt()
--> 247             format_dict, md_dict = self.compute_format_data(result)
    248             self.write_format_data(format_dict, md_dict)
    249             self.update_user_ns(result)

/usr/local/lib/python2.7/site-packages/IPython/core/displayhook.pyc in compute_format_data(self, result)
    155 
    156         """
--> 157         return self.shell.display_formatter.format(result)
    158 
    159     def write_format_data(self, format_dict, md_dict=None):

/usr/local/lib/python2.7/site-packages/IPython/core/formatters.pyc in format(self, obj, include, exclude)
    150             md = None
    151             try:
--> 152                 data = formatter(obj)
    153             except:
    154                 # FIXME: log the exception

/usr/local/lib/python2.7/site-packages/IPython/core/formatters.pyc in __call__(self, obj)
    479                 type_pprinters=self.type_printers,
    480                 deferred_pprinters=self.deferred_printers)
--> 481             printer.pretty(obj)
    482             printer.flush()
    483             return stream.getvalue()

/usr/local/lib/python2.7/site-packages/IPython/lib/pretty.pyc in pretty(self, obj)
    360                             if callable(meth):
    361                                 return meth(obj, self, cycle)
--> 362             return _default_pprint(obj, self, cycle)
    363         finally:
    364             self.end_group()

/usr/local/lib/python2.7/site-packages/IPython/lib/pretty.pyc in _default_pprint(obj, p, cycle)
    480     if getattr(klass, '__repr__', None) not in _baseclass_reprs:
    481         # A user-provided repr.
--> 482         p.text(repr(obj))
    483         return
    484     p.begin_group(1, '<')

/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/ggplot-0.5.7-py2.7.egg/ggplot/ggplot.pyc in __repr__(self)
    108     def __repr__(self):
    109         """Print/show the plot"""
--> 110         figure = self.draw()
    111         # We're going to default to making the plot appear when __repr__ is
    112         # called.

/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/ggplot-0.5.7-py2.7.egg/ggplot/ggplot.pyc in draw(self)
    210                         for geom in self.geoms:
    211                             ax = plt.gca()
--> 212                             callbacks = geom.plot_layer(frame, ax)
    213                         axis_extremes[_iter] = [min(plt.xlim()), max(plt.xlim()),
    214                                                 min(plt.ylim()), max(plt.ylim())]

/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/ggplot-0.5.7-py2.7.egg/ggplot/geoms/geom.pyc in plot_layer(self, data, ax)
    113         _cols = set(data.columns) & set(self.manual_aes)
    114         data = data.drop(_cols, axis=1)
--> 115         data = self._calculate_stats(data)
    116         self._verify_aesthetics(data)
    117         _needed = self.valid_aes | self._extra_requires

/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/ggplot-0.5.7-py2.7.egg/ggplot/geoms/geom.pyc in _calculate_stats(self, data)
    273                 new_data = new_data.append(_data, ignore_index=True)
    274         else:
--> 275             new_data = self._stat._calculate(data)
    276 
    277         # some geoms expect a sorted x domain

/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/ggplot-0.5.7-py2.7.egg/ggplot/stats/stat_bin.pyc in _calculate(self, data)
     90             empty_bins = set(range(len(x))) - set(x_assignments)
     91             for _b in empty_bins:
---> 92                 _wfreq_table[_b] = 0
     93             _wfreq_table = _wfreq_table.sort_index()
     94 

/usr/local/lib/python2.7/site-packages/pandas/core/series.pyc in __setitem__(self, key, value)
    829                 return
    830 
--> 831             raise KeyError('%s not in this series!' % str(key))
    832         except TypeError, e:
    833             # python 3 type errors should be raised

KeyError: '14 not in this series!'

Scaling

Scaling continuous data


In [15]:
ggplot(aes(x='wt', y='mpg'), data=mtcars) +\
    geom_point() +\
    scale_x_continuous("Weight") +\
    scale_y_continuous("Miles per Gallon")


Out[15]:
<ggplot: (284866953)>

In [16]:
ggplot(aes(x='wt', y='mpg'), data=mtcars) +\
    geom_point() +\
    scale_x_continuous("Weight", limits=(2, 4)) +\
    scale_y_continuous("Miles per Gallon", limits=(10, 35))


Out[16]:
<ggplot: (284845045)>

In [17]:
ggplot(aes(x='wt', y='mpg'), data=mtcars) +\
    geom_point() +\
    scale_x_continuous("Weight") +\
    scale_y_continuous("Miles per Gallon") +\
    xlim(2, 4) + ylim(10, 35)


Out[17]:
<ggplot: (284425805)>

Scaling colors


In [18]:
ggplot(aes(x='wt', y='mpg', color='factor(cyl)'), data=mtcars) +\
    geom_point() +\
    scale_color_brewer()


Out[18]:
<ggplot: (282956825)>

In [19]:
ggplot(aes(x='wt', y='mpg', color='factor(cyl)'), data=mtcars) +\
    geom_point() +\
    scale_color_brewer(type='div', palette=5)


Out[19]:
<ggplot: (284749401)>

In [20]:
ggplot(aes(x='wt', y='mpg', color='factor(cyl)'), data=mtcars) +\
    geom_point() +\
    scale_color_brewer(type='qual')


Out[20]:
<ggplot: (284426685)>

In [21]:
idx = np.random.uniform(0, 1, len(diamonds)) > 0.75
ggplot(aes(x='carat', y='depth', color='price'), data=diamonds[idx]) +\
    geom_point() +\
    scale_colour_gradient(low='blue', high='red')


Out[21]:
<ggplot: (284802733)>

In [ ]: