Python-3.4.3/C2/Plotting-Charts/English

From Script | Spoken-Tutorial
Revision as of 16:21, 6 June 2017 by Trupti (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Python/C2/plotting-charts/English
Title of script: plotting charts
Author: Anoop Jacob Thomas, Thirumalesh H S
Keywords: Python, IPython, pylab, scatter plot, charts, matplotlib help


Visual Cue Narration
Show slide


Hello friends. Welcome to the tutorial on plotting Narrator name is missing in the first slideOk..We will write the narrator name once it is fixed. The previous tutorial had March date hence I did not write april's dateyou can write the current date when the script is completed.Change the date to Aprilcharts
Show slide

Learning Objectives


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


  • Create pie charts
  • Create bar charts and
  • Find more information on matplotlib



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

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 %pylab and press Enter.

Show Slide
Pie chart


A pie chart is a circular chart divided into sectors, change the text in slideto illustrate numerical proportion
Show Slide

pie() function

Syntax:
pie(values, labels= labels)
*
values – the data to be plotted
  • labels – the label for each wedge in the pie chart


The syntax for pie() function is as follows:


pie(values, labels= labels)*
values – the data to be plotted
  • labels – the label for each wedge in the pie chart


Show Slide
Exercise 1
*
Plot a pie chart representing the profit percentage of company A.


*
Use the data from file company-a-data.txt


available in the code file link of this tutorial


[File browser]

Open the file company-a-data.txt and show the content

The data file has two columns with a set of values in each column.


The first line represents years. The second column represents the profit percentages.
[IPython Terminal]


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


To produce a scatter plot,


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

Type


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


unpack = True --> returns the transposed array of data

[Ipython Terminal]


clf()

pie(profit,labels=year)


Type clf() to clear the plot window, if any plots executed previously.


Type pie(profit,labels=year)


[Plot Window]


Show pie chart plot

Notice that we passed two arguments to the function pie().


First one being the values, which is profit in our case.


The next one being the set of labels to be used in the pie chart, which is year in our case.

Show Slide
Exercise 2


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

interpreter - this word is at the end in the slide. Delete this. Give different color for "pie?"Black is missing in the slidechange the slidePlot a pie chart with the same data with the following colors for each wedges.


  • white, red, black, magenta,
  • yellow, blue, green, cyan,
  • yellow, magenta and blue.


Hint: In your ipython interpreter, try typing pie?

[IPython Terminal]


clf()


pie(profit,labels=year,colors=('w','r','k','m','y','b','g','c','y','m','b'))


Solution to Exercise 2


Clear the plot window by typing clf()


let us share and see the reply from NirmalaWill it be relevant, the requirements are already mentioned in the question.

"Plot a pie chart with the data with colors for each wedges as white, red, black, "


I felt it will be repetition of info.


Most of the earlier tutorials do not have the description of the outputThere is no relation between pie chart created using: pie(profit,labels=year)


That will be cleared using clf()


The Question has already specified "Plot a pie chart with the same data with colors for each wedges as white, red, black," - that is the expected outputCan we add that narration of whatever output we get after executing the commandsame applies for the bar graphsI think it is required especially here because there is a difference in the pie chart we get when we execute pie(profit,labels=year) command and this long command. We can just say the desired output is here or see the color change. I do not know how to tell it technicallyAnyway, I addedthe changes that you requested at the all the pointsType pie(profit,labels=year,colors=('w','r','k','m','y','b','g','c','y','m','b'))


This will display a pie chart with each wedge having the color as specified in the parameters.


Show Slide
Bar chart


A bar chart is a chart*
with rectangular bars
  • with lengths proportional to the values that they represent.


Show Slide


bar() function

The syntax for bar graph is:
bar(x, y)
*
x – a sequence of data
  • y - a sequence of data, the same length of x


Show Slide
Exercise 3


*
Plot a bar chart representing the profit percentage of company A.


  • Use the data from file company-a-data.txt.


[IPython Terminal]


clf()

bar(year, profit)


Solution to Exercise 3


Clear the plot window by typing clf()


same as aboveType bar(year, profit)


This will display a bar chart representing the profit percentage of company A
Show Slide
Exercise 4


Plot a bar chart as shown in the image:


Hint: Bar chart is not filled and which is hatched with 45o slanting lines,

The data for the chart may be obtained from the file company-a-data.txt.


Hint: In your ipython interpreter, try typing bar?


Hint: In ipython interpreter, try typing bar? change the slide. Give different color for the command "bar?"donenarration is not clearClue is to try typing bar? In ipython interpreter.

[Ipython Terminal]
clf()


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


Solution to Exercise 4


Clear the plot window by typing clf()


same as above. Also mention the change in the outputType bar(year,profit,fill=False,hatch='/')


This will display a bar chart which is not filled and which is hatched with 45o slanting lines

Show Slide
"Getting help on matplotlib"

{SHOW WEBSITE while narrating}

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
Summary slide


This brings us to the end of this tutorial.


In this tutorial we learnt to,


  • Plot a pie chart using pie() function
  • Plot a bar chart using bar() function
  • Access the matplotlib online help


Show Slide
Assignment


Here are some self assessment questions for you to solve.


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
Solution to assignment


And the answers,


  1. bar(x, y, fill=False, hatch='|') is the correct option to generate a bar chart with vertical line hatching.


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
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, Trupti