other_types_of_plots.rst
author amit
Wed, 22 Sep 2010 20:01:59 +0530
changeset 183 c66ee1743d25
parent 160 f394adb5b00e
permissions -rw-r--r--
Merging changes with Nishant's

.. 2.4 LO: other types of plots (3) [anoop] 
.. -----------------------------------------
.. * scatter 
.. * pie chart 
.. * bar chart 
.. * log 
.. * illustration of other plots, matplotlib help

===================
Other type of plots
===================

{{{ show the first slide }}}

Hello and welcome to the tutorial other type of plots.

{{{ show the outline slide }}}

In this tutorial we will cover scatter plot, pie chart, bar chart and
log plot. We will also see few other plots and also introduce you to
the matplotlib help.


Let us start with scatter plot. 

{{{ switch to the next slide }}}

In a scatter plot, the data is displayed as a collection of points,
each having the value of one variable determining the position on the
horizontal axis and the value of the other variable determining the
position on the vertical axis. This kind of plot is also called a
scatter chart, scatter diagram and scatter graph.

Before we proceed further get your IPython interpreter running with
the ``-pylab`` option. Start your IPython interpreter as
::

    ipython -pylab

{{{ open the ipython interpreter in the terminal using the command
ipython -pylab }}}

{{{ switch to the next slide having the problem statement of first
exercise }}}

Now, let us plot a scatter plot showing the percentage profit of company A
from the year 2000-2010. The data for the same is available in the
file ``company-a-data.txt``. 

{{{ open the file company-a-data.txt and show the content }}}

The data file has two lines with a set of values in each line, the
first line representing years and the second line representing the
profit percentages.

{{{ close the file and switch to the terminal }}}

To product the scatter plot first we need to load the data from the
file using ``loadtxt``. We learned in one of the previous sessions,
and it can be done as ::

    year,profit = loadtxt('/home/fossee/other-plot/company-a-data.txt',dtype=type(int()))

Now in-order to generate the scatter graph we will use the function 
``scatter()`` 
::

	scatter(year,profit)

Notice that we passed two arguments to ``scatter()`` function, first
one the values in x-coordinate, year, and the other the values in
y-coordinate, the profit percentage.

{{{ switch to the next slide which has the problem statement of
problem to be tried out }}}

Now here is a question for you to try out, plot the same data with red
diamonds. 

**Clue** - *try scatter? in your ipython interpreter* 

.. scatter(year,profit,color='r',marker='d')

Now let us move on to pie chart.

{{{ switch to the slide which says about pie chart }}}

A pie chart or a circle graph is a circular chart divided into
sectors, illustrating proportion.

{{{ switch to the slide showing the problem statement of second
exercise question }}}

Plot a pie chart representing the profit percentage of company A, with
the same data from file ``company-a-data.txt``. So let us reuse the
data we have loaded from the file previously.

We can plot the pie chart using the function ``pie()``.
::

   pie(profit,labels=year)

Notice that we passed two arguments to the function ``pie()``. The
first one the values and the next one the set of labels to be used in
the pie chart.

{{{ switch to the next slide which has the problem statement of
problem to be tried out }}}

Now here is a question for you to try out, plot a pie chart with the
same data with colors for each wedges as white, red, black, magenta,
yellow, blue, green, cyan, yellow, magenta and blue respectively.

**Clue** - *try pie? in your ipython interpreter* 

.. pie(t,labels=s,colors=('w','r','k','m','y','b','g','c','y','m','b'))

{{{ switch to the slide which says about bar chart }}}

Now let us move on to bar chart. A bar chart or bar graph is a chart
with rectangular bars with lengths proportional to the values that
they represent.

{{{ switch to the slide showing the problem statement of third
exercise question }}}

Plot a bar chart representing the profit percentage of company A, with
the same data from file ``company-a-data.txt``. 

So let us reuse the data we have loaded from the file previously.

We can plot the bar chart using the function ``bar()``.
::

   bar(year,profit)

Note that the function ``bar()`` needs at least two arguments one the
values in x-coordinate and the other values in y-coordinate which is
used to determine the height of the bars.

{{{ switch to the next slide which has the problem statement of
problem to be tried out }}}

Now here is a question for you to try, plot a bar chart which is not
filled and which is hatched with 45\ :sup:`o` slanting lines as shown
in the image in the slide.

**Clue** - *try bar? in your ipython interpreter* 

.. bar(year,profit,fill=False,hatch='/')

{{{ switch to the slide which says about bar chart }}}

Now let us move on to log-log plot. A log-log graph or log-log plot is
a two-dimensional graph of numerical data that uses logarithmic scales
on both the horizontal and vertical axes. Because of the nonlinear
scaling of the axes, a function of the form y = ax\ :sup:`b` will
appear as a straight line on a log-log graph

{{{ switch to the slide showing the problem statement of fourth
exercise question }}}


Plot a `log-log` chart of y=5*x\ :sup:`3` for x from 1-20.

Before we actually plot let us calculate the points needed for
that. And it could be done as,
::

    x = linspace(1,20,100)
    y = 5*x**3

Now we can plot the log-log chart using ``loglog()`` function,
::

    loglog(x,y)

To understand the difference between a normal ``plot`` and a ``log-log
plot`` let us create another plot using the function ``plot``.
::

    figure(2)
    plot(x,y)

{{{ show both the plots side by side }}}

So that was ``log-log() plot``.

{{{ switch to the next slide which says: "How to get help on
matplotlib online"}}}

Now we will see few more plots and also see how to access help of
matplotlib over the internet.

Help about matplotlib can be obtained from
matplotlib.sourceforge.net/contents.html

.. #[[Anoop: I am not so sure how to do the rest of it, so I guess we
   can just browse through the side and tell them few. What is your
   opinion??]]

Now let us see few plots from
matplotlib.sourceforge.net/users/screenshots.html

{{{ browse through the site quickly }}}

{{{ switch to recap slide }}}

Now we have come to the end of this tutorial. We have covered scatter
plot, pie chart, bar chart, log-log plot and also saw few other plots
and covered how to access the matplotlib online help.

{{{ switch to the thank you slide }}}

Thank you!

..  Author: Anoop Jacob Thomas <anoop@fossee.in>
    Reviewer 1:
    Reviewer 2:
    External reviewer: