Python-3.4.3/C2/Other-Types-Of-Plots/English

From Script | Spoken-Tutorial
Jump to: navigation, search

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


Title of script: Other types of plots.

Author: Anoop Jacob Thomas, Thirumalesh H S

Keywords: Python, IPython, pylab, scatter plot, loglog plot, video tutorial


Visual Cue Narration
Show slide Hello friends. Welcome to the tutorial on Other types of plots
Show slide

Learning Objectives

  • Create scatter plot
  • Create log-log plots
At the end of this tutorial, you will be able to -
  • Create scatter plot
  • Create log-log plots
Show slide

System Specifications

To record this tutorial, I am using
  • Ubuntu Linux 14.04 operating system
  • Python 3.4.3
  • IPython 5.1.0
Show slide

Pre-requisites

  • run basic Python commands on the ipython console
  • Load data from files and
  • Plot data.

If not, see the relevant Python tutorials on http://spoken-tutorial.org

To practice this tutorial, you should know how to
  • run basic Python commands on the ipython console
  • Load data from files and
  • Plot data.

If not, see the relevant Python tutorials on this website.

[Terminal]

ipython3

Let us first open the Terminal by pressing Ctrl+Alt+T keys simultaneously.

Now, type ipython3 and press Enter.

[IPython console]

%pylab and press Enter.

Let us initialise the pylab package.

Type percent pylab and press Enter.

Show Slide

Scatter plot

In a scatter plot, the data is displayed as a collection of points.

Each point determines its position on the x and y axes

Show Slide

Exercise 1

  • Plot a scatter plot showing the percentage profit of a company A from the year 2000 to 2010.
  • The data for the same is available in the file company hyphen a hyphen data dot txt.
  • company hyphen a hyphen data dot txt file is available in the code file link of this tutorial.
  • Please download and use it.
In terminal type:

cat company-a-data.txt

Let us see the content of the file company hyphen a hyphen data dot txt.

So type,

cat company hyphen a hyphen data dot txt. and press Enter

Show cat company-a-data.txt output The data file has two columns with a set of values in each column.

The first column represents the years. And the second column represents the profit percentage.

[IPython Terminal]

year, profit = loadtxt('company-a-data.txt', unpack = True)

To produce the scatter plot,

we first need to load the data from the file using loadtxt command.

Type

year comma profit equal to loadtxt within parentheses within single quotes company hyphen a hyphen data dot txt after single quotes comma unpack equal to True and Press Enter

Highlight unpack = True unpack equal to True returns the transposed array of data
Show Slide

scatter() function

scatter() function is used to generate the scatter graph

Syntax: scatter within parentheses x comma y

  • x is a sequence of data
  • y is a sequence of data having the same length of x
[IPython Terminal]

scatter(year, profit)

Let us use scatter function to plot scatter graph for data stored in year and profit.

Type scatter within parentheses year comma profit and press Enter.

Notice that we passed two arguments to scatter() function.

First one being the values in x-coordinate that is year.

Second, the values in y-coordinate, the profit percentages.

Show Slide

Exercise

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

Read the documentation of scatter.


Plot a scatter plot of same data in company hyphen a hyphen data dot txt with red diamond markers.

[IPython Terminal]


clf()

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

Solution to Exercise

Clear the plot window by typing clf parentheses and press Enter

Now type

scatter within parentheses year comma profit comma color equal to within single quotes r comma marker equal to within single quotes d and press Enter

Show plot window Thus, we got our scatter plot.

Now let us see another kind of plot.

Show Slide

log-log graph

  • A log-log plot is a two-dimensional graph of numerical data.
  • It uses logarithmic scales on both axes.
  • Graph appears as straight line due to non-linear scaling
Show Slide

loglog() function

Syntax

loglog within parentheses x comma y

  • x is a sequence of data
  • y is a sequence of data, having the same length of x
Show Slide

Exercise 3

Plot a log-log chart of y equal to 5 times x cube for x from 1 to 20.
[IPython Terminal]


x = linspace(1,20,100)

y = 5*x**3

Before we actually plot, let us calculate the points needed for that.

Type

x equalto linspace within parentheses 1 comma 20 comma 100 and press Enter

Then, y equal to 5 into x raised to 3 and press Enter

[IPython Terminal]

clf()

loglog(x,y)

Clear the plot window by typing clf parentheses and press Enter

Type loglog within parentheses x comma y and press Enter

Show plot window We see the required plot
Show Slide

Summary

This brings us to the end of this tutorial. In this tutorial we learnt to,


  1. Plot a scatter plot using scatter() function
  1. Plot a log-log graph using loglog() function
Show Slide

Assignment


Here are some self assessment questions for you to solve.
  1. scatter within parentheses x comma y comma color equal to within single quotes blue comma marker equal to within single quotes d
  2. and plot within parentheses x comma y comma color equal to within single quotes b comma marker equal to within single quotes d

does exactly the same?

  • True or False
Show Slide

Solution to assignment

And the answer,
  1. False. Both functions do not produce the same kind of plot.
Show Slide

Forum

Please post your timed queries in this forum.
Show Slide

Fossee Forum

Please post your general queries on Python in this forum.
Show Slide

Textbook Companion

FOSSEE team coordinates the TBC project.
Show Slide

Acknowledgment

http://spoken-tutorial.org

Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.

For more details, visit this website.

Previous slide This is _________ from IIT Bombay (or FOSSEE, if you wish) signing off.

Thank you.

Contributors and Content Editors

Nancyvarkey, Nirmala Venkat, Pratham920