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

From Script | Spoken-Tutorial
Jump to: navigation, search
Visual Cue Narration
Show Slide 1

Containing title, name of the production team along with the logo of MHRD

Hello Friends and welcome to the tutorial on Other types of plots.
Show Slide 2

Learning objectives

Till now we have seen only one kind of plotting. Hence in this tutorial we will be looking at some more kinds of plots.

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


Show Slide 3

Pre-requisite

Before beginning this tutorial,we would suggest you to complete the tutorial on "Loading data from files" and "Plotting data".
Open the ipython interpreter in the terminal using the command ipython -pylab
ipython -pylab
Before we start with the topic, let us start our IPython interpreter
Show Slide 4

Scatter plot

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. This kind of plot is also called a scatter chart, a scatter diagram or a scatter graph. Let us now generate a scatter plot with the help of an exercise
Show Slide 5

Assignment 1

Plot a scatter plot showing the percentage profit of a 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
year,profit =
loadtxt('/home/fossee/other-plot/company-a-data.txt',dtype=type(int()))
To produce the scatter plot, we first need to load the data from the file using loadtxt command.
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.
scatter(year,profit) Now in-order to generate the scatter graph we will use the function scatter()
Show Slide 6

scatter function

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.
Show Slide 7

Assignment 2

Plot a scatter plot of the same data in company-a-data.txt with red diamond markers. Pause the video here, try out the following exercise and resume the video.
Continue from paused state Switch to the terminal
clf()
scatter(year,profit,color='r',marker='d')
Thus, we got our scatter plot. It is always a good practice to clear the previous figure before creating another one. Now let us see another kind of plot, the pie chart, for the same data.
Show Slide 8

Which says about pie chart

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

Assignment 3

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.

Switch to the terminal
clf()
pie(profit,labels=year)
We can plot the pie chart using the function pie().
Show Slide 10

pie() function

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.

Pause the video here, try out the following exercise and resume the video.

Show Slide 11

Assignment 4

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.
Switch to the terminal
clf()
pie(t,labels=s,colors=('w','r','k','m','y','b','g','c','y','m','b'))
Show Slide 12

Which says about bar chart

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

Assignment 5

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 terminal
clf()
bar(year,profit)
We can plot the bar chart using the function bar().
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.
Show Slide 14

Assignment 6

Plot a bar chart which is not filled and which is hatched with 45o slanting lines as shown in the image.The data for the chart may be obtained from the file company-a-data.txt.
clf()
bar(year,profit,fill=False,hatch='/')
Show Slide 15

Which says about log-log graph

Now let us move on to the log-log plot. 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. 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
Show Slide 16

Assignment 7

Plot a log-log chart of y=5*x3 for x from 1-20.
Switch to the terminal
x = linspace(1,20,100)
y = 5*x**3
Before we actually plot let us calculate the points needed for that.
Show Slide 17

loglog() function

Here is the syntax of the log-log function. Now we can plot the log-log chart using loglog() function,
Switch to the terminal
clf()
loglog(x,y)
figure(2)
plot(x,y)
To understand the difference between a normal plot and a log-log plot let us create another plot using the function plot.
Show both the plots side by side The difference is clear.So that was log-log() plot.
Show Slide 18

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

Show Slide 19

Summary slide

This brings us to the end of this tutorial. In this tutorial we learnt to,
  1. Plot a scatter plot using scatter() function
  2. Plot a pie chart using pie() function
  3. Plot a bar chart using bar() function
  4. Plot a log-log graph using loglog() function
  5. Access the matplotlib online help.


Show Slide 20

Self assessment questions slide

Here are some self assessment questions for you to solve.
  1. scatter(x, y, color='blue', marker='d') and plot(x, y, color='b', marker='d') does exactly the same.
    • True
    • False
  1. What statement can be issued to generate a bar chart with vertical line hatching.
    • bar(x, y, color='w', hatch='/')
    • bar(x, y, fill=False, hatch='//')
    • bar(x, y, fill=False, hatch='|')
    • bar(x, y, color='w', hatch=)


Show Slide 21

Solution of self assessment questions on slide

And the answers,
  1. False. Both functions do not produce the same kind of plot.
  2. bar(x, y, fill=False, hatch='|') is the correct option to generate a bar chart with vertical line hatching.


Show Slide 22

acknowledgment slide

Hope you have enjoyed this tutorial and found it useful. Thank you!

Contributors and Content Editors

Chandrika