Plotting

In [2]:
%matplotlib inline
from pylab import *
from random import random
In [13]:
# Histograms
x = randn(10000)
hist_data = hist(x, 10)
In [25]:
# plotting a line.
x = range(10)
y = [3*random() + i + random() for i in x]
plot(x,y)
savefig('/Users/andrew/Desktop/temp.png')
In [26]:
# scatter plot
scatter(x,y)
Out[26]:
<matplotlib.collections.PathCollection at 0x10a01fd50>
In [36]:
# plotting two lines
y2 = [3 + i + 2*random() for i in x]
plot(x, y, hold=True, color='r') # hold=True gives you the same axes
plot(x, y2, hold=True, color='b')
Out[36]:
[<matplotlib.lines.Line2D at 0x10aed3f10>]
In [37]:
# Fill in an area
fill([2, 3, 4], [3, 3, 2])
Out[37]:
[<matplotlib.patches.Polygon at 0x10b1b9bd0>]

Fill for standard deviation of a sequence

In [38]:
x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
mean = [4, 4, 3, 3, 2, 2, 1, 1, 2, 2]
stdev = [.5, .4, .3, .5, .6, .1, .2, .3, .1, .05]

x2 = list(x)
x2.reverse()

topline = [mean[i] + stdev[i] for i in xrange(len(x))]
bottomline = [mean[i] - stdev[i] for i in xrange(len(x))]
bottomline.reverse()

fill(x + x2, topline + bottomline)
Out[38]:
[<matplotlib.patches.Polygon at 0x10b3b00d0>]
In [44]:
fill(x + x2, topline + bottomline, hold=True, color='r', alpha=0.2) ## Alpha is a transparency param
plot(x, mean, hold=True, color='k')
Out[44]:
[<matplotlib.lines.Line2D at 0x10a402d10>]
In [46]:
# axis labeling
fill(x + x2, topline + bottomline, hold=True, color='r', alpha=0.2)
plot(x, mean, hold=True, color='r')
xlabel("age (days)")
ylabel("income ($1k)")
Out[46]:
<matplotlib.text.Text at 0x10b8d26d0>
In [51]:
# set the range of the axis
fill(x + x2, topline + bottomline, hold=True, color='r', alpha=0.2)
plot(x, mean, hold=True, color='r')
xlabel("age")
ylabel("income (% of lifetime)")
# either one of these work
xlim((0,15))
ylim(0,100)
Out[51]:
(0, 100)
In [52]:
# log scale axes
fill(x + x2, topline + bottomline, hold=True, color='r', alpha=0.2)
plot(x, mean, hold=True, color='r')
xlabel("age")
ylabel("income")
yscale('log')
In [53]:
# captioning
fill(x + x2, topline + bottomline, hold=True, color='r', alpha=0.2)
plot(x, mean, hold=True, color='r')
xlabel("age")
ylabel("income")
title("Poor example of the relationship between Age and Income")
Out[53]:
<matplotlib.text.Text at 0x10ca13350>
In [72]:
# legend
y2 = [3 + i + 2*random() for i in x]
plot(x, y, hold=True, color='r', label='male')
plot(x, y2, hold=True, color='b', label='female')
legend()
xlabel("age")
ylabel("income")
title("Legend Example")
Out[72]:
<matplotlib.text.Text at 0x10fba9c50>
In [55]:
help(legend)
Help on function legend in module matplotlib.pyplot:

legend(*args, **kwargs)
    Places a legend on the axes.
    
    To make a legend for lines which already exist on the axes
    (via plot for instance), simply call this function with an iterable
    of strings, one for each legend item. For example::
    
        ax.plot([1, 2, 3])
        ax.legend(['A simple line'])
    
    However, in order to keep the "label" and the legend element
    instance together, it is preferable to specify the label either at
    artist creation, or by calling the
    :meth:`~matplotlib.artist.Artist.set_label` method on the artist::
    
        line, = ax.plot([1, 2, 3], label='Inline label')
        # Overwrite the label by calling the method.
        line.set_label('Label via method')
        ax.legend()
    
    Specific lines can be excluded from the automatic legend element
    selection by defining a label starting with an underscore.
    This is default for all artists, so calling :meth:`legend` without
    any arguments and without setting the labels manually will result in
    no legend being drawn.
    
    For full control of which artists have a legend entry, it is possible
    to pass an iterable of legend artists followed by an iterable of
    legend labels respectively::
    
       legend((line1, line2, line3), ('label1', 'label2', 'label3'))
    
    Parameters
    ----------
    loc : int or string or pair of floats, default: 0
        The location of the legend. Possible codes are:
    
            ===============   =============
            Location String   Location Code
            ===============   =============
            'best'            0
            'upper right'     1
            'upper left'      2
            'lower left'      3
            'lower right'     4
            'right'           5
            'center left'     6
            'center right'    7
            'lower center'    8
            'upper center'    9
            'center'          10
            ===============   =============
    
    
        Alternatively can be a 2-tuple giving ``x, y`` of the lower-left
        corner of the legend in axes coordinates (in which case
        ``bbox_to_anchor`` will be ignored).
    
    bbox_to_anchor : :class:`matplotlib.transforms.BboxBase` instance                          or tuple of floats
        Specify any arbitrary location for the legend in `bbox_transform`
        coordinates (default Axes coordinates).
    
        For example, to put the legend's upper right hand corner in the
        center of the axes the following keywords can be used::
    
           loc='upper right', bbox_to_anchor=(0.5, 0.5)
    
    ncol : integer
        The number of columns that the legend has. Default is 1.
    
    prop : None or :class:`matplotlib.font_manager.FontProperties` or dict
        The font properties of the legend. If None (default), the current
        :data:`matplotlib.rcParams` will be used.
    
    fontsize : int or float or {'xx-small', 'x-small', 'small', 'medium',                   'large', 'x-large', 'xx-large'}
        Controls the font size of the legend. If the value is numeric the
        size will be the absolute font size in points. String values are
        relative to the current default font size. This argument is only
        used if `prop` is not specified.
    
    numpoints : None or int
        The number of marker points in the legend when creating a legend
        entry for a line/:class:`matplotlib.lines.Line2D`.
        Default is ``None`` which will take the value from the
        ``legend.numpoints`` :data:`rcParam<matplotlib.rcParams>`.
    
    scatterpoints : None or int
        The number of marker points in the legend when creating a legend
        entry for a scatter plot/
        :class:`matplotlib.collections.PathCollection`.
        Default is ``None`` which will take the value from the
        ``legend.scatterpoints`` :data:`rcParam<matplotlib.rcParams>`.
    
    scatteryoffsets : iterable of floats
        The vertical offset (relative to the font size) for the markers
        created for a scatter plot legend entry. 0.0 is at the base the
        legend text, and 1.0 is at the top. To draw all markers at the
        same height, set to ``[0.5]``. Default ``[0.375, 0.5, 0.3125]``.
    
    markerscale : None or int or float
        The relative size of legend markers compared with the originally
        drawn ones. Default is ``None`` which will take the value from
        the ``legend.markerscale`` :data:`rcParam <matplotlib.rcParams>`.
    
    frameon : None or bool
        Control whether a frame should be drawn around the legend.
        Default is ``None`` which will take the value from the
        ``legend.frameon`` :data:`rcParam<matplotlib.rcParams>`.
    
    fancybox : None or bool
        Control whether round edges should be enabled around
        the :class:`~matplotlib.patches.FancyBboxPatch` which
        makes up the legend's background.
        Default is ``None`` which will take the value from the
        ``legend.fancybox`` :data:`rcParam<matplotlib.rcParams>`.
    
    shadow : None or bool
        Control whether to draw a shadow behind the legend.
        Default is ``None`` which will take the value from the
        ``legend.shadow`` :data:`rcParam<matplotlib.rcParams>`.
    
    framealpha : None or float
        Control the alpha transparency of the legend's frame.
        Default is ``None`` which will take the value from the
        ``legend.framealpha`` :data:`rcParam<matplotlib.rcParams>`.
    
    mode : {"expand", None}
        If `mode` is set to ``"expand"`` the legend will be horizontally
        expanded to fill the axes area (or `bbox_to_anchor` if defines
        the legend's size).
    
    bbox_transform : None or :class:`matplotlib.transforms.Transform`
        The transform for the bounding box (`bbox_to_anchor`). For a value
        of ``None`` (default) the Axes'
        :data:`~matplotlib.axes.Axes.transAxes` transform will be used.
    
    title : str or None
        The legend's title. Default is no title (``None``).
    
    borderpad : float or None
        The fractional whitespace inside the legend border.
        Measured in font-size units.
        Default is ``None`` which will take the value from the
        ``legend.borderpad`` :data:`rcParam<matplotlib.rcParams>`.
    
    labelspacing : float or None
        The vertical space between the legend entries.
        Measured in font-size units.
        Default is ``None`` which will take the value from the
        ``legend.labelspacing`` :data:`rcParam<matplotlib.rcParams>`.
    
    handlelength : float or None
        The length of the legend handles.
        Measured in font-size units.
        Default is ``None`` which will take the value from the
        ``legend.handlelength`` :data:`rcParam<matplotlib.rcParams>`.
    
    handletextpad : float or None
        The pad between the legend handle and text.
        Measured in font-size units.
        Default is ``None`` which will take the value from the
        ``legend.handletextpad`` :data:`rcParam<matplotlib.rcParams>`.
    
    borderaxespad : float or None
        The pad between the axes and legend border.
        Measured in font-size units.
        Default is ``None`` which will take the value from the
        ``legend.borderaxespad`` :data:`rcParam<matplotlib.rcParams>`.
    
    columnspacing : float or None
        The spacing between columns.
        Measured in font-size units.
        Default is ``None`` which will take the value from the
        ``legend.columnspacing`` :data:`rcParam<matplotlib.rcParams>`.
    
    handler_map : dict or None
        The custom dictionary mapping instances or types to a legend
        handler. This `handler_map` updates the default handler map
        found at :func:`matplotlib.legend.Legend.get_legend_handler_map`.
    
    Notes
    -----
    
    Not all kinds of artist are supported by the legend command.
    See :ref:`plotting-guide-legend` for details.
    
    Examples
    --------
    
    .. plot:: mpl_examples/api/legend_demo.py


In [56]:
y2 = [3 + i + 2*random() for i in x]
plot(x, y, hold=True, color='r', label='male')
plot(x, y2, hold=True, color='b', label='female')
legend(loc=10)
xlabel("age")
ylabel("income")
title("Legend Example")
Out[56]:
<matplotlib.text.Text at 0x10d2ac610>
In [71]:
y2 = [3 + i + 2*random() for i in x]
plot(x, y, 'r+-.', hold=True, label='male')
plot(x, y2, hold=True, color='b', marker='o', linestyle='dashed', label='female')
legend(loc=4)
xlabel("age")
ylabel("income")
title("Legend Example")
Out[71]:
<matplotlib.text.Text at 0x10f9f3990>
In [60]:
help(plot)
Help on function plot in module matplotlib.pyplot:

plot(*args, **kwargs)
    Plot lines and/or markers to the
    :class:`~matplotlib.axes.Axes`.  *args* is a variable length
    argument, allowing for multiple *x*, *y* pairs with an
    optional format string.  For example, each of the following is
    legal::
    
        plot(x, y)        # plot x and y using default line style and color
        plot(x, y, 'bo')  # plot x and y using blue circle markers
        plot(y)           # plot y using x as index array 0..N-1
        plot(y, 'r+')     # ditto, but with red plusses
    
    If *x* and/or *y* is 2-dimensional, then the corresponding columns
    will be plotted.
    
    An arbitrary number of *x*, *y*, *fmt* groups can be
    specified, as in::
    
        a.plot(x1, y1, 'g^', x2, y2, 'g-')
    
    Return value is a list of lines that were added.
    
    By default, each line is assigned a different color specified by a
    'color cycle'.  To change this behavior, you can edit the
    axes.color_cycle rcParam.
    
    The following format string characters are accepted to control
    the line style or marker:
    
    ================    ===============================
    character           description
    ================    ===============================
    ``'-'``             solid line style
    ``'--'``            dashed line style
    ``'-.'``            dash-dot line style
    ``':'``             dotted line style
    ``'.'``             point marker
    ``','``             pixel marker
    ``'o'``             circle marker
    ``'v'``             triangle_down marker
    ``'^'``             triangle_up marker
    ``'<'``             triangle_left marker
    ``'>'``             triangle_right marker
    ``'1'``             tri_down marker
    ``'2'``             tri_up marker
    ``'3'``             tri_left marker
    ``'4'``             tri_right marker
    ``'s'``             square marker
    ``'p'``             pentagon marker
    ``'*'``             star marker
    ``'h'``             hexagon1 marker
    ``'H'``             hexagon2 marker
    ``'+'``             plus marker
    ``'x'``             x marker
    ``'D'``             diamond marker
    ``'d'``             thin_diamond marker
    ``'|'``             vline marker
    ``'_'``             hline marker
    ================    ===============================
    
    
    The following color abbreviations are supported:
    
    ==========  ========
    character   color
    ==========  ========
    'b'         blue
    'g'         green
    'r'         red
    'c'         cyan
    'm'         magenta
    'y'         yellow
    'k'         black
    'w'         white
    ==========  ========
    
    In addition, you can specify colors in many weird and
    wonderful ways, including full names (``'green'``), hex
    strings (``'#008000'``), RGB or RGBA tuples (``(0,1,0,1)``) or
    grayscale intensities as a string (``'0.8'``).  Of these, the
    string specifications can be used in place of a ``fmt`` group,
    but the tuple forms can be used only as ``kwargs``.
    
    Line styles and colors are combined in a single format string, as in
    ``'bo'`` for blue circles.
    
    The *kwargs* can be used to set line properties (any property that has
    a ``set_*`` method).  You can use this to set a line label (for auto
    legends), linewidth, anitialising, marker face color, etc.  Here is an
    example::
    
        plot([1,2,3], [1,2,3], 'go-', label='line 1', linewidth=2)
        plot([1,2,3], [1,4,9], 'rs',  label='line 2')
        axis([0, 4, 0, 10])
        legend()
    
    If you make multiple lines with one plot command, the kwargs
    apply to all those lines, e.g.::
    
        plot(x1, y1, x2, y2, antialised=False)
    
    Neither line will be antialiased.
    
    You do not need to use format strings, which are just
    abbreviations.  All of the line properties can be controlled
    by keyword arguments.  For example, you can set the color,
    marker, linestyle, and markercolor with::
    
        plot(x, y, color='green', linestyle='dashed', marker='o',
             markerfacecolor='blue', markersize=12).
    
    See :class:`~matplotlib.lines.Line2D` for details.
    
    The kwargs are :class:`~matplotlib.lines.Line2D` properties:
    
      agg_filter: unknown
      alpha: float (0.0 transparent through 1.0 opaque)         
      animated: [True | False]         
      antialiased or aa: [True | False]         
      axes: an :class:`~matplotlib.axes.Axes` instance         
      clip_box: a :class:`matplotlib.transforms.Bbox` instance         
      clip_on: [True | False]         
      clip_path: [ (:class:`~matplotlib.path.Path`,         :class:`~matplotlib.transforms.Transform`) |         :class:`~matplotlib.patches.Patch` | None ]         
      color or c: any matplotlib color         
      contains: a callable function         
      dash_capstyle: ['butt' | 'round' | 'projecting']         
      dash_joinstyle: ['miter' | 'round' | 'bevel']         
      dashes: sequence of on/off ink in points         
      drawstyle: ['default' | 'steps' | 'steps-pre' | 'steps-mid' |                   'steps-post']         
      figure: a :class:`matplotlib.figure.Figure` instance         
      fillstyle: ['full' | 'left' | 'right' | 'bottom' | 'top' | 'none']         
      gid: an id string         
      label: string or anything printable with '%s' conversion.         
      linestyle or ls: [``'-'`` | ``'--'`` | ``'-.'`` | ``':'`` | ``'None'`` |                   ``' '`` | ``''``]         and any drawstyle in combination with a linestyle, e.g., ``'steps--'``.         
      linewidth or lw: float value in points         
      lod: [True | False]         
      marker: unknown
      markeredgecolor or mec: any matplotlib color         
      markeredgewidth or mew: float value in points         
      markerfacecolor or mfc: any matplotlib color         
      markerfacecoloralt or mfcalt: any matplotlib color         
      markersize or ms: float         
      markevery: unknown
      path_effects: unknown
      picker: float distance in points or callable pick function         ``fn(artist, event)``         
      pickradius: float distance in points         
      rasterized: [True | False | None]         
      sketch_params: unknown
      snap: unknown
      solid_capstyle: ['butt' | 'round' |  'projecting']         
      solid_joinstyle: ['miter' | 'round' | 'bevel']         
      transform: a :class:`matplotlib.transforms.Transform` instance         
      url: a url string         
      visible: [True | False]         
      xdata: 1D array         
      ydata: 1D array         
      zorder: any number         
    
    kwargs *scalex* and *scaley*, if defined, are passed on to
    :meth:`~matplotlib.axes.Axes.autoscale_view` to determine
    whether the *x* and *y* axes are autoscaled; the default is
    *True*.
    
    
    
    Additional kwargs: hold = [True|False] overrides default hold state


Column Plots

In [75]:
import numpy
means = [25, 35, 30, 35, 27]
stds = [2, 3, 4, 1, 2]

placement = numpy.arange(5)
width = 0.35

bar(placement, means, width, color='r', yerr=stds)
ylabel("scores")
title("Bar Chart")
xticks(placement + width/2.0, ('one', 'two', '3', 'IV', 'five'))
Out[75]:
([<matplotlib.axis.XTick at 0x1100e6290>,
  <matplotlib.axis.XTick at 0x10feac710>,
  <matplotlib.axis.XTick at 0x1100353d0>,
  <matplotlib.axis.XTick at 0x11017ce90>,
  <matplotlib.axis.XTick at 0x110188610>],
 <a list of 5 Text xticklabel objects>)
In [76]:
means = [25, 35, 30, 35, 27]
means2 = list(means)
means2.reverse()
stds = [2, 3, 4, 1, 2]
stds2 = [3, 2, 1, 1, 5]

placement = numpy.arange(5)
width = 0.35

bar(placement, means, width, color='r', yerr=stds, hold=True)
bar([p+width for p in placement], means2, width, color='y', yerr=stds2, hold=True)
ylabel("scores")
title("Bar Chart")
xticks(placement + width, ('one', 'two', 'three', 'four', 'five'))
Out[76]:
([<matplotlib.axis.XTick at 0x1101b00d0>,
  <matplotlib.axis.XTick at 0x1100c6350>,
  <matplotlib.axis.XTick at 0x1102d55d0>,
  <matplotlib.axis.XTick at 0x110332490>,
  <matplotlib.axis.XTick at 0x110332bd0>],
 <a list of 5 Text xticklabel objects>)
In [78]:
# box and whisker plots
# some fake data 
spread = rand(50) * 100
center = ones(25) * 50
flier_high = rand(10) * 100 + 100
flier_low = rand(10) * -100
data = concatenate((spread, center, flier_high, flier_low), 0)
bp_data = boxplot(data)
In [79]:
from nltk.book import *
*** Introductory Examples for the NLTK Book ***
Loading text1, ..., text9 and sent1, ..., sent9
Type the name of the text or sentence to view it.
Type: 'texts()' or 'sents()' to list the materials.
text1: Moby Dick by Herman Melville 1851
text2: Sense and Sensibility by Jane Austen 1811
text3: The Book of Genesis
text4: Inaugural Address Corpus
text5: Chat Corpus
text6: Monty Python and the Holy Grail
text7: Wall Street Journal
text8: Personals Corpus
text9: The Man Who Was Thursday by G . K . Chesterton 1908

In [89]:
text4.dispersion_plot(["citizens", "democracy", "freedom", "moon"])
In [90]:
fdist = FreqDist(text4)
fdist.plot(50)
In []: