other-type-of-plots/script.rst
changeset 261 c7f0069d698a
child 288 a3b98b4c371e
equal deleted inserted replaced
260:25b4e962b55e 261:c7f0069d698a
       
     1 .. 2.4 LO: other types of plots (3) [anoop] 
       
     2 .. -----------------------------------------
       
     3 .. * scatter 
       
     4 .. * pie chart 
       
     5 .. * bar chart 
       
     6 .. * log 
       
     7 .. * illustration of other plots, matplotlib help
       
     8 
       
     9 ===================
       
    10 Other type of plots
       
    11 ===================
       
    12 
       
    13 {{{ show the first slide }}}
       
    14 
       
    15 Hello and welcome to the tutorial other type of plots.
       
    16 
       
    17 {{{ show the outline slide }}}
       
    18 
       
    19 In this tutorial we will cover scatter plot, pie chart, bar chart and
       
    20 log plot. We will also see few other plots and also introduce you to
       
    21 the matplotlib help.
       
    22 
       
    23 
       
    24 Let us start with scatter plot. 
       
    25 
       
    26 {{{ switch to the next slide }}}
       
    27 
       
    28 In a scatter plot, the data is displayed as a collection of points,
       
    29 each having the value of one variable determining the position on the
       
    30 horizontal axis and the value of the other variable determining the
       
    31 position on the vertical axis. This kind of plot is also called a
       
    32 scatter chart, scatter diagram and scatter graph.
       
    33 
       
    34 Before we proceed further get your IPython interpreter running with
       
    35 the ``-pylab`` option. Start your IPython interpreter as
       
    36 ::
       
    37 
       
    38     ipython -pylab
       
    39 
       
    40 {{{ open the ipython interpreter in the terminal using the command
       
    41 ipython -pylab }}}
       
    42 
       
    43 {{{ switch to the next slide having the problem statement of first
       
    44 exercise }}}
       
    45 
       
    46 Now, let us plot a scatter plot showing the percentage profit of company A
       
    47 from the year 2000-2010. The data for the same is available in the
       
    48 file ``company-a-data.txt``. 
       
    49 
       
    50 {{{ open the file company-a-data.txt and show the content }}}
       
    51 
       
    52 The data file has two lines with a set of values in each line, the
       
    53 first line representing years and the second line representing the
       
    54 profit percentages.
       
    55 
       
    56 {{{ close the file and switch to the terminal }}}
       
    57 
       
    58 To product the scatter plot first we need to load the data from the
       
    59 file using ``loadtxt``. We learned in one of the previous sessions,
       
    60 and it can be done as ::
       
    61 
       
    62     year,profit = loadtxt('/home/fossee/other-plot/company-a-data.txt',dtype=type(int()))
       
    63 
       
    64 Now in-order to generate the scatter graph we will use the function 
       
    65 ``scatter()`` 
       
    66 ::
       
    67 
       
    68 	scatter(year,profit)
       
    69 
       
    70 Notice that we passed two arguments to ``scatter()`` function, first
       
    71 one the values in x-coordinate, year, and the other the values in
       
    72 y-coordinate, the profit percentage.
       
    73 
       
    74 {{{ switch to the next slide which has the problem statement of
       
    75 problem to be tried out }}}
       
    76 
       
    77 Now here is a question for you to try out, plot the same data with red
       
    78 diamonds. 
       
    79 
       
    80 **Clue** - *try scatter? in your ipython interpreter* 
       
    81 
       
    82 .. scatter(year,profit,color='r',marker='d')
       
    83 
       
    84 Now let us move on to pie chart.
       
    85 
       
    86 {{{ switch to the slide which says about pie chart }}}
       
    87 
       
    88 A pie chart or a circle graph is a circular chart divided into
       
    89 sectors, illustrating proportion.
       
    90 
       
    91 {{{ switch to the slide showing the problem statement of second
       
    92 exercise question }}}
       
    93 
       
    94 Plot a pie chart representing the profit percentage of company A, with
       
    95 the same data from file ``company-a-data.txt``. So let us reuse the
       
    96 data we have loaded from the file previously.
       
    97 
       
    98 We can plot the pie chart using the function ``pie()``.
       
    99 ::
       
   100 
       
   101    pie(profit,labels=year)
       
   102 
       
   103 Notice that we passed two arguments to the function ``pie()``. The
       
   104 first one the values and the next one the set of labels to be used in
       
   105 the pie chart.
       
   106 
       
   107 {{{ switch to the next slide which has the problem statement of
       
   108 problem to be tried out }}}
       
   109 
       
   110 Now here is a question for you to try out, plot a pie chart with the
       
   111 same data with colors for each wedges as white, red, black, magenta,
       
   112 yellow, blue, green, cyan, yellow, magenta and blue respectively.
       
   113 
       
   114 **Clue** - *try pie? in your ipython interpreter* 
       
   115 
       
   116 .. pie(t,labels=s,colors=('w','r','k','m','y','b','g','c','y','m','b'))
       
   117 
       
   118 {{{ switch to the slide which says about bar chart }}}
       
   119 
       
   120 Now let us move on to bar chart. A bar chart or bar graph is a chart
       
   121 with rectangular bars with lengths proportional to the values that
       
   122 they represent.
       
   123 
       
   124 {{{ switch to the slide showing the problem statement of third
       
   125 exercise question }}}
       
   126 
       
   127 Plot a bar chart representing the profit percentage of company A, with
       
   128 the same data from file ``company-a-data.txt``. 
       
   129 
       
   130 So let us reuse the data we have loaded from the file previously.
       
   131 
       
   132 We can plot the bar chart using the function ``bar()``.
       
   133 ::
       
   134 
       
   135    bar(year,profit)
       
   136 
       
   137 Note that the function ``bar()`` needs at least two arguments one the
       
   138 values in x-coordinate and the other values in y-coordinate which is
       
   139 used to determine the height of the bars.
       
   140 
       
   141 {{{ switch to the next slide which has the problem statement of
       
   142 problem to be tried out }}}
       
   143 
       
   144 Now here is a question for you to try, plot a bar chart which is not
       
   145 filled and which is hatched with 45\ :sup:`o` slanting lines as shown
       
   146 in the image in the slide.
       
   147 
       
   148 **Clue** - *try bar? in your ipython interpreter* 
       
   149 
       
   150 .. bar(year,profit,fill=False,hatch='/')
       
   151 
       
   152 {{{ switch to the slide which says about bar chart }}}
       
   153 
       
   154 Now let us move on to log-log plot. A log-log graph or log-log plot is
       
   155 a two-dimensional graph of numerical data that uses logarithmic scales
       
   156 on both the horizontal and vertical axes. Because of the nonlinear
       
   157 scaling of the axes, a function of the form y = ax\ :sup:`b` will
       
   158 appear as a straight line on a log-log graph
       
   159 
       
   160 {{{ switch to the slide showing the problem statement of fourth
       
   161 exercise question }}}
       
   162 
       
   163 
       
   164 Plot a `log-log` chart of y=5*x\ :sup:`3` for x from 1-20.
       
   165 
       
   166 Before we actually plot let us calculate the points needed for
       
   167 that. And it could be done as,
       
   168 ::
       
   169 
       
   170     x = linspace(1,20,100)
       
   171     y = 5*x**3
       
   172 
       
   173 Now we can plot the log-log chart using ``loglog()`` function,
       
   174 ::
       
   175 
       
   176     loglog(x,y)
       
   177 
       
   178 To understand the difference between a normal ``plot`` and a ``log-log
       
   179 plot`` let us create another plot using the function ``plot``.
       
   180 ::
       
   181 
       
   182     figure(2)
       
   183     plot(x,y)
       
   184 
       
   185 {{{ show both the plots side by side }}}
       
   186 
       
   187 So that was ``log-log() plot``.
       
   188 
       
   189 {{{ switch to the next slide which says: "How to get help on
       
   190 matplotlib online"}}}
       
   191 
       
   192 Now we will see few more plots and also see how to access help of
       
   193 matplotlib over the internet.
       
   194 
       
   195 Help about matplotlib can be obtained from
       
   196 matplotlib.sourceforge.net/contents.html
       
   197 
       
   198 .. #[[Anoop: I am not so sure how to do the rest of it, so I guess we
       
   199    can just browse through the side and tell them few. What is your
       
   200    opinion??]]
       
   201 
       
   202 Now let us see few plots from
       
   203 matplotlib.sourceforge.net/users/screenshots.html
       
   204 
       
   205 {{{ browse through the site quickly }}}
       
   206 
       
   207 {{{ switch to recap slide }}}
       
   208 
       
   209 Now we have come to the end of this tutorial. We have covered scatter
       
   210 plot, pie chart, bar chart, log-log plot and also saw few other plots
       
   211 and covered how to access the matplotlib online help.
       
   212 
       
   213 {{{ switch to the thank you slide }}}
       
   214 
       
   215 Thank you!
       
   216 
       
   217 ..  Author: Anoop Jacob Thomas <anoop@fossee.in>
       
   218     Reviewer 1:
       
   219     Reviewer 2:
       
   220     External reviewer: