Python/C2/Other-types-of-plots/English-timed

From Script | Spoken-Tutorial
Revision as of 16:23, 2 January 2013 by Minal (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Visual Cue Narration
0:01 Hello Friends and welcome to the tutorial on Other types of plots.
0:06 Till now we have seen only one kind of plotting.
0:10 Hence in this tutorial we will be looking at some more kinds of plots.
0:16 At the end of this tutorial, you will be able to
  1. Create scatter plot
  2. Create pie charts
  3. Create bar charts
  4. Create log-log plots
  5. Use the matplotlib help
0:29 So let us begin with scatter plot
0:33 Before beginning this tutorial,we would suggest you to complete the tutorial on "Loading data from files" and "Plotting data".
0:42 In a scatter plot, the data is displayed as a collection of points, where each point determines it's position on the horizontal axis and the vertical axis respectively.
0:54 This kind of plot is also called a scatter chart, a scatter diagram or a scatter graph.
1:01 Before we start with the topic, let us start our IPython interpreter
1:06 So type ipython hypen pylab
1:13 Plot a scatter plot showing the percentage profit of a company A from the year 2000-2010.
1:22 The data for the same is available in the file company-a-data.txt.
1:33 Type cat space slash home slash fossee bacslash other-plot slash company-a-data.txt (enter)
1:50 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.
2:02 To produce the scatter plot, we first need to load the data from the file using loadtxt command.
2:10 Type year,profit = loadtxt within bracket in single quote slash home slash fossee slash other-plot slash company-a-data.txt comma dtype=type in bracket int() hit enter


2:52 By default loadtxt converts the value to float.
2:57 The dtype=type within bracket int() argument in loadtxt converts the value to integer, as we require the data as integer further in the tutorial.
3:11 Now in-order to generate the scatter graph we will use the function scatter()
3:18 Type scatter within bracket year comma profit and hit enter
3:32 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.
3:57 Plot a scatter plot of the same data in company-a-data.txt with red diamond markers.
4:09 Pause the video here, try out the following exercise and resume the video.
4:17 Now let us see another kind of plot, the pie chart, for the same data.
4:40 A pie chart or a circle graph is a circular chart divided into sectors, illustrating proportion.
4:49 Plot a pie chart representing the profit percentage of company A, with the same data from file company-a-data.txt.
5:00 So let us reuse the data we have loaded from the file previously.
5:11 We can plot the pie chart using the function pie().
5:15 Type pie within bracket profit comma labels=year


5:29 Notice that we passed two arguments to the function pie().
5:33 First one the values and the next one the set of labels to be used in the pie chart.
5:38 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.
5:58 Pause the video here, try out the following exercise and resume the video.
6:05 Now let us move on to the bar charts.
6:08 A bar chart or bar graph is a chart with rectangular bars with lengths proportional to the values that they represent.
6:19 Plot a bar chart representing the profit percentage of company A, with the same data from file company-a-data.txt.
6:30 So let us reuse the data we have loaded from the file previously.
6:34 We can plot the bar chart using the function bar()
6:44 type bar within bracket year comma profit
6:52 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.
7:05 Plot a bar chart which is not filled and which is hatched with 45o slanting lines as shown in the image.
7:17 The data for the chart may be obtained from the file company-a-data.txt.
7:26 Type bar within bracket year comma profit comma fill=False comma hatch=slash in single quote hit enter
8:05 Now let us move on to the log-log plot.
8:10 A log-log graph or a log-log plot is a two-dimensional graph of numerical data that uses logarithmic scales on both the horizontal and vertical axes.
8:24 Because of the nonlinear scaling of the axes, a function of the form y = axb will appear as a straight line on a log-log graph
8:38 Plot a log-log chart of y=5 into x3 for x from 1-20.
8:49 Before we actually plot let us calculate the points needed for that.
8:54 x = linspace within brackets 1 comma 20 comma 100
y = 5 into x into into 3
9:23 Here is the syntax of the log-log function.
9:28 Now we can plot the log-log chart using loglog() function,
9:34 Type loglog within brackets x comma y hit enter
9:48 To understand the difference between a normal plot and a log-log plot let us create another plot using the function plot.
9:57 figure within brackets 2

plot within brackets x comma y

10:24 The difference is clear.So that was log-log() plot.
10:33 Now we will see few more plots and also see how to access help of matplotlib over the Internet.
10:43 Help about matplotlib can be obtained from matplotlib.sourceforge.net/contents.html
10:55 More plots can be seen at matplotlib.sourceforge.net slash users slash screenshots.html and also at matplotlib.sourceforge.net slash gallery.html
11:13 This brings us to the end of this tutorial. In this tutorial we learnt to,
11:20 1. Plot a scatter plot using scatter() function
11:22 2. Plot a pie chart using pie() function
11:25 3. Plot a bar chart using bar() function
11:28 4. Plot a log-log graph using loglog() function
11:33 5. Access the matplotlib online help.Thank you.
11:42 Here are some self assessment questions for you to solve.
11:46 1. scatter within bracket x comma y comma color=blue in single quotes comma marker= in single quotesd< slash tt> and <tt>plot within bracket x comma y comma color=b in single quotes comma marker=in single quotes d) does exactly the same.
12:04 Is True or False?
12:07 . What statement can be issued to generate a bar chart with vertical line hatching.
12:15 bar within bracket x comma y comma color=w in single quote comma hatch=in single quote slash
12:27 bar within bracket x comma y comma fill=False comma hatch=slash slash in single quote
12:38 bar within bracket x comma y comma fill=False comma hatch=in single quote|)
12:52 bar within bracket x comma y comma color= in single quote w comma hatch=single quote
13:02 And the answers,
13:06 1. False.
13:9 Both functions do not produce the same kind of plot.
13:13 2. bar within bracket x comma y comma fill=False comma hatch=in single quote|) is the correct option to generate a bar chart with vertical line hatching.
13:31 Hope you have enjoyed this tutorial and found it useful.
13:34 Thank you!

Contributors and Content Editors

Gaurav, Jyotisolanki, Minal, PoojaMoolya, Ranjana, Sneha