other-type-of-plots/script.rst
author bhanu
Mon, 08 Nov 2010 01:34:02 +0530
changeset 454 0c69c2b32183
parent 369 8988867e0470
child 471 462f9d3f0418
permissions -rw-r--r--
Language check done for the script `strings`

.. Objectives
.. ----------

.. At the end of this tutorial, you will be able to 

.. 1. Create scatter plot
.. #. Create pie charts
.. #. Create bar charts
.. #. Create log-log plots.

.. Prerequisites
.. -------------

..   1. should have ``ipython`` and ``pylab`` installed. 
..   #. getting started with ``ipython``.
..   #. loading data from files
..   #. plotting the data

     
.. Author              : Anoop Jacob Thomas <anoop@fossee.in>
   Internal Reviewer   : Puneeth
   External Reviewer   :
   Checklist OK?       : <put date stamp here, if OK> [2010-10-05]

.. #[Puneeth: Quickref missing]

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

{{{ show the first slide }}}

Hello and welcome to the tutorial other type of plots.

.. #[Puneeth: this sentence doesn't read well]

{{{ show the outline slide }}}

.. #[Puneeth: motivate looking at other plots. Why are we looking at
.. them? Tell that we have only looked at one type of plot all the
.. while, etc.]

Till now we have seen only one type of plot, and in this tutorial us
see more type of plots like scatter plot, pie chart, bar chart and
log-log plot. We will see few other more plots and also see an
introduction to matplotlib help.

.. #[Puneeth: cover, see and introduce you. be consistent. does, the
.. "We" include the viewer or not?]

Let us start with scatter plot. 

{{{ switch to the next slide, scatter plot }}}

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, start your IPython interpreter
::

    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 produce the scatter plot first we need to load the data from the
file using ``loadtxt``. We learned it 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()))

By default loadtxt converts the value to float. The
``dtype=type(int())`` argument in loadtxt converts the value to
integer as we require the data as integer further in the tutorial.

.. #[Puneeth: make a remark about dtype, that has not been covered in
.. the loadtxt tutorial.]

{{{ switch to next slide, ``scatter`` function }}}

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 markers. 

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

Pause here and solve the question before moving on.

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

Now let us see another plot, pie chart and try to plot the same data
as a pie chart.

.. #[Puneeth: instead of just saying that, say that let's plot a pie
.. chart for the same data. continuity, will be good.]

{{{ 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.

.. #[Puneeth, this part can be move above.]

{{{ switch to next slide, ``pie()`` function }}}

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

   pie(profit,labels=year)

Notice that we passed two arguments to the function ``pie()``. 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* 

Pause here and solve the question before moving on.

.. 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 fifth
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.

{{{ switch to the next slide, ``bar()`` function }}}

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. The data for the chart may be obtained from
the file ``company-a-data.txt``.

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

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

{{{ switch to the slide which says about log-log graph }}}

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. 
::

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

{{{ switch to next slide, ``loglog()`` function }}}

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


More plots can be seen at
matplotlib.sourceforge.net/users/screenshots.html and also at
matplotlib.sourceforge.net/gallery.html

{{{ switch to summary 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!