Difference between revisions of "Python-3.4.3/C2/Plotting-Charts/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with "'''Python/C2/plotting-charts/English'''<div style="color:#000000;margin-left:0cm;margin-right:0cm;"></div><div style="margin-left:0cm;margin-right:0cm;"><span style="color:#00...")
 
Line 1: Line 1:
'''Python/C2/plotting-charts/English'''<div style="color:#000000;margin-left:0cm;margin-right:0cm;"></div><div style="margin-left:0cm;margin-right:0cm;"><span style="color:#000000;">'''Title of script'''</span><span style="color:#000000;">: </span><span style="color:#000000;">'''plotting charts'''</span></div>
+
'''Python/C2/plotting-charts/English''''''Title of script''': '''plotting charts'''
  
<div style="color:#000000;margin-left:0cm;margin-right:0cm;">'''Author: Anoop Jacob Thomas, Thirumalesh H S'''</div>
+
'''Author: Anoop Jacob Thomas, Thirumalesh H S'''
  
<div style="margin-left:0cm;margin-right:0cm;"><span style="color:#000000;">'''Keywords: Python, IPython, pylab, scatter plot'''</span><span style="color:#000000;">, </span><span style="color:#000000;">'''charts, matplotlib help'''</span></div>
+
'''Keywords: Python, IPython, pylab, scatter plot''', '''charts, matplotlib help'''
  
  
  
{| style="border-spacing:0;width:17.912cm;"
+
{| style="border-spacing:0;"
|- style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| '''Visual Cue'''
| '''Visual Cue'''
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| '''Narration'''
| '''Narration'''
+
|- style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"
+
| Show slide
+
  
 +
|-
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Show slide
  
  
| Hello friends. Welcome to the tutorial on '''plotting''' <span style="color:#000000;">Narrator name is missing in the first slide</span><span style="color:#000000;">Ok..</span><span style="color:#000000;">We will write the narrator name once it is fixed. The previous tutorial had March date hence I did not write april's date</span><span style="color:#000000;">you can write the current date when the script is completed.</span><span style="color:#000000;">Change the date to April</span>'''charts'''
 
|- style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"
 
| Show slide
 
  
Learning Objectives
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Hello friends. Welcome to the tutorial on '''plotting'''  Narrator name is missing in the first slide
  
 +
Narrator name is missing in the first slide
  
 +
Narrator name is missing in the first slide
  
| <div style="color:#252525;">At the end of this tutorial, you will be able to - </div>
+
Narrator name is missing in the first slide
  
 +
Narrator name is missing in the first slide
  
* <div style="margin-left:0cm;margin-right:0cm;"><span style="color:#252525;">Create </span><span style="color:#252525;">'''pie charts'''</span></div>
+
'''charts'''
* <div style="margin-left:0cm;margin-right:0cm;"><span style="color:#252525;">Create </span><span style="color:#252525;">'''bar charts '''</span><span style="color:#252525;">and</span></div>
+
* <div style="margin-left:0cm;margin-right:0cm;"><span style="color:#252525;">Find more information on </span><span style="color:#252525;">'''matplotlib'''</span></div>
+
  
 +
|-
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Show slide
  
 +
Learning Objectives
  
  
|- style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"
+
 
| Show slide
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| At the end of this tutorial, you will be able to -
 +
 
 +
 
 +
* Create '''pie charts'''
 +
* Create '''bar charts '''and
 +
* Find more information on '''matplotlib'''
 +
 
 +
 
 +
 
 +
|-
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Show slide
  
 
System Specifications  
 
System Specifications  
| To record this tutorial, I am using * <div style="margin-left:1.27cm;margin-right:0cm;">'''Ubuntu Linux 14.04''' operating system</div>
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| To record this tutorial, I am using
* <div style="margin-left:1.27cm;margin-right:0cm;">'''Python 3.4.3'''</div>
+
* <div style="margin-left:1.27cm;margin-right:0cm;">'''IPython 5.1.0'''</div>
+
  
 +
* '''Ubuntu Linux 14.04''' operating system
 +
* '''Python 3.4.3'''
 +
* '''IPython 5.1.0'''
  
|- style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"
+
 
| Show slide
+
 
 +
|-
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Show slide
  
 
Pre-requisites
 
Pre-requisites
| <div style="color:#252525;">To practice this tutorial, you should know how to</div>* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#252525;">run basic </span><span style="color:#252525;">'''Python'''</span><span style="color:#252525;"> </span><span style="color:#252525;">'''commands'''</span><span style="color:#252525;"> on the </span><span style="color:#252525;">'''ipython'''</span><span style="color:#252525;"> </span><span style="color:#252525;">'''console'''</span></div>
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| To practice this tutorial, you should know how to
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#252525;">'''Load data '''</span><span style="color:#252525;">from</span><span style="color:#252525;">''' '''</span><span style="color:#252525;">files</span><span style="color:#252525;">''' '''</span><span style="color:#252525;">and</span></div>
+
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#252525;">'''Plot data'''</span><span style="color:#252525;">.</span></div>
+
  
 +
* 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.
  
<span style="color:#252525;">If not, see the </span><span style="color:#252525;">relevant</span><span style="color:#252525;"> Python tutorials on this website.</span>
 
 
|-
 
|-
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;" | [Terminal]
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| [Terminal]
  
 
'''ipython3'''
 
'''ipython3'''
Line 61: Line 75:
  
  
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;" | Let us first open the '''Terminal '''by pressing '''Ctrl+Alt+T '''keys simultaneously.  
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| Let us first open the '''Terminal '''by pressing '''Ctrl+Alt+T '''keys simultaneously.  
  
  
 
Now, type '''ipython3''' and press '''Enter'''.  
 
Now, type '''ipython3''' and press '''Enter'''.  
|- style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"
+
 
| [IPython console]
+
|-
 +
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| [IPython console]
  
 
'''%pylab '''and press '''Enter.'''
 
'''%pylab '''and press '''Enter.'''
| Let us initialise the '''pylab''' package.
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| Let us initialise the '''pylab''' package.
  
  
 
Type '''%pylab '''and press''' Enter.'''
 
Type '''%pylab '''and press''' Enter.'''
|- style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"
 
| <div style="color:#252525;">Show Slide </div>
 
  
<div style="color:#252525;">'''Pie chart'''</div>
+
|-
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Show Slide
  
 +
'''Pie chart'''
  
  
| <span style="color:#252525;">A </span><span style="color:#252525;">'''pie'''</span><span style="color:#252525;"> </span><span style="color:#252525;">'''chart'''</span><span style="color:#252525;"> is a circular chart divided into sectors, </span><span style="color:#000000;">change the text in slide</span><span style="color:#ff0000;">to illustrate numerical proportion</span>
 
|- style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"
 
| <div style="color:#252525;">Show Slide</div>
 
  
<span style="color:#252525;">'''pie()'''</span><span style="color:#252525;"> </span><span style="color:#252525;">function</span>
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| A '''pie''' '''chart''' is a circular chart divided into sectors,  change the text in slide
  
<div style="color:#ff0000;">Syntax:</div>
+
to illustrate numerical proportion
  
<div style="color:#ff0000;">'''pie(values, labels= labels)'''</div>* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#ff0000;">'''values – '''</span><span style="color:#ff0000;">the data to be </span><span style="color:#ff0000;">plotted</span></div>
+
|-
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#ff0000;">'''labels'''</span><span style="color:#ff0000;"> – the label for each wedge in the pie chart</span></div>
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Show Slide
  
 +
'''pie()''' function
  
 +
Syntax:
  
<div style="color:#252525;"></div>
+
'''pie(values, labels= labels)'''
| <span style="color:#252525;">The s</span><span style="color:#252525;">yntax</span><span style="color:#252525;"> for pie() function is as follows:</span>
+
  
 +
* '''values – '''the data to be plotted
 +
* '''labels''' – the label for each wedge in the pie chart
  
<span style="color:#252525;">'''pie(values, labels= </span><span style="color:#252525;">labels</span><span style="color:#252525;">)'''</span>* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#252525;">'''values – '''</span><span style="color:#252525;">the data to be plotted</span></div>
 
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#252525;">'''labels'''</span><span style="color:#252525;"> – the label for each wedge in the pie chart</span></div>
 
  
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| The syntax for pie() function is as follows:
  
|- style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"
 
| <div style="color:#252525;">Show Slide </div>
 
  
<div style="color:#252525;">Exercise 1</div>
+
'''pie(values, labels= labels)'''
| * <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#252525;">Plot a </span><span style="color:#252525;">'''pie'''</span><span style="color:#252525;"> </span><span style="color:#252525;">'''chart'''</span><span style="color:#252525;"> representing the profit percentage of company A. </span></div>
+
  
 +
* '''values – '''the data to be plotted
 +
* '''labels''' – the label for each wedge in the pie chart
  
  
<div style="color:#252525;"></div>* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#252525;">Use the data from file </span><span style="color:#0000ff;">'''company-a-data.txt'''</span></div>
 
  
 +
|-
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Show Slide
 +
 +
Exercise 1
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| * 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
 
available in the code file link of this tutorial
Line 117: Line 135:
  
  
|- style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"
 
| <div style="color:#252525;">[File browser]</div>
 
  
<span style="color:#252525;">Open the </span><span style="color:#252525;">'''file'''</span><span style="color:#252525;"> </span><span style="color:#252525;">'''company-a-data.txt'''</span><span style="color:#252525;"> and show the content</span>
+
|-
| <div style="color:#252525;">The data file has two columns with a set of values in each column.</div>
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| [File browser]
  
 +
Open the '''file''' '''company-a-data.txt''' and show the content
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| The data file has two columns with a set of values in each column.
  
<div style="color:#252525;">The first line represents years. The second column represents the profit percentages.</div>
+
 
|- style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"
+
The first line represents years. The second column represents the profit percentages.
| <div style="color:#252525;">[IPython Terminal]</div>
+
 
 +
|-
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| [IPython Terminal]
  
  
Line 133: Line 153:
  
  
| <span style="color:#ff0000;">To produce </span><span style="color:#ff0000;">a</span><span style="color:#ff0000;"> scatter plot, </span>
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| To produce a scatter plot,  
  
  
<span style="color:#ff0000;">we first need to load the data from the file using </span><span style="color:#ff0000;">'''loadtxt'''</span><span style="color:#ff0000;"> </span><span style="color:#ff0000;">command.</span>
+
we first need to load the data from the file using '''loadtxt''' command.
  
<div style="color:#ff0000;">Type</div>
+
Type
  
  
<span style="color:#ff0000;">'''year, profit = loadtxt(</span><span style="color:#ff0000;">'</span><span style="color:#ff0000;">add/path/ </span><span style="color:#ff0000;">company-a-data.txt'</span><span style="color:#ff0000;">, unpack = True)'''</span>
+
'''year, profit = loadtxt('add/path/ company-a-data.txt', unpack = True)'''
  
  
<span style="color:#ff0000;">'''unpack = True --> </span><span style="color:#ff0000;">returns'''</span><span style="color:#ff0000;"> the </span><span style="color:#ff0000;">'''transposed array '''</span><span style="color:#ff0000;">of </span><span style="color:#ff0000;">'''data'''</span>
+
'''unpack = True --> returns''' the '''transposed array '''of '''data'''
|- style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"
+
 
| <div style="color:#252525;">[Ipython Terminal]</div>
+
|-
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| [Ipython Terminal]
  
  
Line 155: Line 176:
  
  
| Type '''clf() '''to clear the plot window, if any plots executed previously.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Type '''clf() '''to clear the plot window, if any plots executed previously.
  
  
Line 162: Line 183:
  
  
|- style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"
+
 
| [Plot Window]
+
|-
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| [Plot Window]
  
  
 
Show '''pie chart '''plot
 
Show '''pie chart '''plot
| <span style="color:#252525;">Notice that we passed two arguments to the function </span><span style="color:#252525;">'''pie()'''</span><span style="color:#252525;">. </span>
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Notice that we passed two arguments to the function '''pie()'''.  
  
  
<span style="color:#252525;">First one being the </span><span style="color:#252525;">'''values,'''</span><span style="color:#252525;"> which is </span><span style="color:#252525;">'''profit'''</span><span style="color:#252525;"> in our case.</span>
+
First one being the '''values,''' which is '''profit''' in our case.
  
  
<span style="color:#252525;">The next one being the set of </span><span style="color:#252525;">'''labels'''</span><span style="color:#252525;"> to be used in the </span><span style="color:#252525;">'''pie chart'''</span><span style="color:#252525;">, which is </span><span style="color:#252525;">'''year'''</span><span style="color:#252525;"> in our case.</span>
+
The next one being the set of '''labels''' to be used in the '''pie chart''', which is '''year''' in our case.
 +
 
 
|-
 
|-
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;" | <div style="color:#252525;">Show Slide </div>
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Show Slide  
  
<div style="color:#252525;">Exercise 2</div>
+
Exercise 2
  
  
  
| style="border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;" | <div style="color:#252525;">Pause the video here, try out the following exercise and resume the video.</div>
+
| style="border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Pause the video here, try out the following exercise and resume the video.
  
<span style="color:#000000;">interpreter - this word is at the end in the slide. Delete this. Give different color for "pie?"</span><span style="color:#000000;">Black is missing in the slide</span><span style="color:#000000;">change the slide</span>Plot a '''pie''' '''chart''' with the same data with the following colors for each wedges.
+
interpreter - this word is at the end in the slide. Delete this. Give different color for "pie?"
  
 +
interpreter - this word is at the end in the slide. Delete this. Give different color for "pie?"
  
* <div style="margin-left:1.27cm;margin-right:0cm;">'''white, red, black, magenta,'''</div>
+
interpreter - this word is at the end in the slide. Delete this. Give different color for "pie?"
* <div style="margin-left:1.27cm;margin-right:0cm;">'''yellow, blue, green, cyan,'''</div>
+
* <div style="margin-left:1.27cm;margin-right:0cm;">'''yellow, magenta '''and''' blue'''.</div>
+
  
 +
Plot a '''pie''' '''chart''' with the same data with the following colors for each wedges.
  
  
<div style="color:#252525;"></div>
+
* '''white, red, black, magenta,'''
 +
* '''yellow, blue, green, cyan,'''
 +
* '''yellow, magenta '''and''' blue'''.
  
<span style="color:#ff0000;">Hint: </span><span style="color:#ff0000;">In your </span><span style="color:#ff0000;">'''ipython interpreter, '''</span><span style="color:#ff0000;">t</span><span style="color:#ff0000;">ry typing </span><span style="color:#ff0000;">'''pie?'''</span>
+
Hint: In your '''ipython interpreter, '''try typing '''pie?'''
|- style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"
+
 
| <div style="color:#252525;">[IPython Terminal] </div>
+
|-
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| [IPython Terminal]  
  
  
Line 206: Line 232:
  
  
| <div style="color:#252525;">Solution to Exercise 2</div>
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Solution to Exercise 2
  
  
Line 212: Line 238:
  
  
<span style="color:#000000;">let us share and see the reply from Nirmala</span><span style="color:#000000;">Will it be relevant, the requirements are already mentioned in the question.</span>
+
let us share and see the reply from Nirmala
  
<span style="color:#000000;">"Plot a pie chart with the data with colors for each wedges as white, red, black, "</span>
+
let us share and see the reply from Nirmala
  
 +
let us share and see the reply from Nirmala
  
<span style="color:#000000;">I felt it will be repetition of info.</span>
+
let us share and see the reply from Nirmala
  
 +
let us share and see the reply from Nirmala
  
<span style="color:#000000;">Most of the earlier tutorials do not have the description of the output</span><span style="color:#000000;">There is no relation between pie chart created using: pie(profit,labels=year)</span>
+
let us share and see the reply from Nirmala
  
 +
let us share and see the reply from Nirmala
  
<span style="color:#000000;">That will be cleared using clf()</span>
+
Type '''pie(profit,labels=year,colors=('w','r','k','m','y','b','g','c','y','m','b')) '''
  
  
<span style="color:#000000;">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 output</span><span style="color:#000000;">Can we add that narration of whatever output we get after executing the command</span><span style="color:#000000;">same applies for the bar graphs</span><span style="color:#000000;">I 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 technically</span><span style="color:#000000;">Anyway, I addedthe changes that you requested at the all the points</span>Type '''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.
  
  
<div style="color:#333333;">This will display a pie chart with each wedge having the color as specified in the parameters.</div>
 
  
  
 +
|-
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Show Slide
  
|- style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"
+
'''Bar chart'''
| <div style="color:#252525;">Show Slide</div>
+
  
<div style="color:#252525;">'''Bar chart'''</div>
 
  
  
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| A '''bar chart''' is a '''chart'''
  
| <span style="color:#252525;">A </span><span style="color:#252525;">'''bar chart'''</span><span style="color:#252525;"> is a </span><span style="color:#252525;">'''chart'''</span>* <div style="color:#252525;margin-left:1.27cm;margin-right:0cm;">with rectangular bars </div>
+
* with rectangular bars  
* <div style="color:#252525;margin-left:1.27cm;margin-right:0cm;">with lengths proportional to the values that they represent.</div>
+
* with lengths proportional to the values that they represent.
  
  
|- style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"
 
| <div style="color:#252525;">Show Slide</div>
 
  
 +
|-
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Show Slide
  
<span style="color:#252525;">'''bar()'''</span><span style="color:#252525;"> function</span>
 
| <span style="color:#252525;">The s</span><span style="color:#252525;">yntax</span><span style="color:#252525;"> for bar graph is:</span>
 
  
<div style="color:#252525;">'''bar(x, y)'''</div>* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#252525;">'''x –'''</span><span style="color:#252525;"> a sequence of data</span></div>
+
'''bar()''' function
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#252525;">'''y '''</span><span style="color:#252525;">- a sequence of data, the same length of x</span></div>
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| The syntax for bar graph is:
  
 +
'''bar(x, y)'''
  
|- style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"
+
* '''x –''' a sequence of data
| <div style="color:#252525;">Show Slide</div>
+
* '''y '''- a sequence of data, the same length of x
  
<div style="color:#252525;">Exercise 3</div>
 
  
  
 +
|-
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Show Slide
  
| * <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#252525;">Plot a </span><span style="color:#252525;">'''bar chart'''</span><span style="color:#252525;"> representing the profit percentage of company A. </span></div>
+
Exercise 3
  
  
  
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#252525;">Use the data from file </span><span style="color:#252525;">'''company-a-data.txt'''</span><span style="color:#252525;">.</span></div>
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| * Plot a '''bar chart''' representing the profit percentage of company A.  
  
 +
* Use the data from file '''company-a-data.txt'''.
  
|- style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"
+
 
| <div style="color:#252525;">[IPython Terminal]</div>
+
 
 +
|-
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| [IPython Terminal]
  
  
Line 279: Line 311:
  
  
| <div style="color:#252525;">Solution to Exercise 3</div>
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Solution to Exercise 3
  
  
<span style="color:#252525;">Clear the </span><span style="color:#252525;">'''plot window '''</span><span style="color:#252525;">by typing </span><span style="color:#252525;">'''clf()'''</span>
+
Clear the '''plot window '''by typing '''clf()'''
  
  
<span style="color:#000000;">same as above</span><span style="color:#252525;">Type </span><span style="color:#252525;">'''bar(year, profit)'''</span>
+
same as above
  
 +
Type '''bar(year, profit)'''
  
<div style="color:#252525;">This will display a bar chart representing the profit percentage of company A</div>
 
|- style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"
 
| <div style="color:#252525;">Show Slide </div>
 
  
<div style="color:#252525;">Exercise 4</div>
+
This will display a bar chart representing the profit percentage of company A
  
 +
|-
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Show Slide
  
 +
Exercise 4
  
| <span style="color:#ff0000;">Plot a </span><span style="color:#ff0000;">'''bar chart '''</span><span style="color:#ff0000;">as shown in the image:</span>
 
  
  
<span style="color:#ff0000;"><u>'''Hint'''</u></span><span style="color:#ff0000;">:</span><span style="color:#ff0000;"> </span><span style="color:#ff0000;">Bar chart</span><span style="color:#ff0000;"> is not filled and which is </span><span style="color:#ff0000;">'''hatched'''</span><span style="color:#ff0000;"> with 45</span><span style="color:#ff0000;"><sup>o</sup></span><span style="color:#ff0000;"> slanting lines</span><span style="color:#ff0000;">,</span>
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Plot a '''bar chart '''as shown in the image:
  
<span style="color:#ff0000;">The data for the chart may be obtained from the file </span><span style="color:#ff0000;">'''company-a-data.txt'''</span><span style="color:#ff0000;">.</span>
 
  
 +
<u>'''Hint'''</u>: Bar chart is not filled and which is '''hatched''' with 45<sup>o</sup> slanting lines,
  
<span style="color:#ff0000;">Hint: In your </span><span style="color:#ff0000;">'''ipython interpreter, '''</span><span style="color:#ff0000;">try typing </span><span style="color:#ff0000;">'''bar?'''</span>
+
The data for the chart may be obtained from the file '''company-a-data.txt'''.
  
  
<span style="color:#000000;">Hint: In ipython interpreter, try typing bar? change the slide. Give different color for the command "bar?"</span><span style="color:#000000;">done</span><span style="color:#000000;">narration is not clear</span><span style="color:#252525;">Clue </span><span style="color:#252525;">is to </span><span style="color:#252525;">try typing </span><span style="color:#252525;">'''bar? '''</span><span style="color:#252525;">In </span><span style="color:#252525;">'''ipython interpreter.'''</span>
+
Hint: In your '''ipython interpreter, '''try typing '''bar?'''
|- style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"
+
| <span style="color:#252525;">'''['''</span><span style="color:#252525;">Ipython Terminal</span><span style="color:#252525;">''']'''</span>
+
  
<div style="color:#252525;">'''clf()'''</div>
+
 
 +
Hint: In ipython interpreter, try typing bar? change the slide. Give different color for the command "bar?"
 +
 
 +
Hint: In ipython interpreter, try typing bar? change the slide. Give different color for the command "bar?"
 +
 
 +
Hint: In ipython interpreter, try typing bar? change the slide. Give different color for the command "bar?"
 +
 
 +
Clue is to try typing '''bar? '''In '''ipython interpreter.'''
 +
 
 +
|-
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| '''['''Ipython Terminal''']'''
 +
 
 +
'''clf()'''
  
  
Line 318: Line 360:
  
  
| <div style="color:#252525;">Solution to Exercise 4</div>
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Solution to Exercise 4
  
  
<span style="color:#252525;">Clear the </span><span style="color:#252525;">'''plot window '''</span><span style="color:#252525;">by typing </span><span style="color:#252525;">'''clf()'''</span>
+
Clear the '''plot window '''by typing '''clf()'''
  
  
<span style="color:#000000;">same as above. Also mention the change in the output</span><span style="color:#252525;">Type </span><span style="color:#252525;">'''bar(year,profit,fill=False,hatch='/')'''</span>
+
same as above. Also mention the change in the output
  
 +
Type '''bar(year,profit,fill=False,hatch='/')'''
  
<span style="color:#252525;">This will display a bar chart </span><span style="color:#252525;">which is not filled and which is </span><span style="color:#252525;">'''hatched'''</span><span style="color:#252525;"> with 45</span><span style="color:#252525;"><sup>o</sup></span><span style="color:#252525;"> slanting lines</span>
 
|- style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"
 
| <div style="color:#252525;">Show Slide </div>
 
  
<div style="color:#252525;">"Getting help on matplotlib"</div>
+
This will display a bar chart which is not filled and which is '''hatched''' with 45<sup>o</sup> slanting lines
  
<span style="background-color:#ffff00;color:#252525;">{SHOW WEBSITE while </span><span style="background-color:#ffff00;color:#252525;">narrating</span><span style="background-color:#ffff00;color:#252525;">}</span><span style="color:#252525;"> </span>
+
|-
| <div style="color:#252525;"></div>
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Show Slide
  
<span style="color:#252525;">Help about </span><span style="color:#252525;">'''matplotlib'''</span><span style="color:#252525;"> can be obtained from </span><span style="color:#252525;">'''matplotlib.sourceforge.net/contents.html'''</span>
+
"Getting help on matplotlib"
  
<div style="color:#252525;">More plots can be seen at</div>
+
{SHOW WEBSITE while narrating}
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Help about '''matplotlib''' can be obtained from '''matplotlib.sourceforge.net/contents.html'''
  
<div style="color:#252525;">'''matplotlib.sourceforge.net/users/screenshots.html'''</div>
+
More plots can be seen at
  
<span style="color:#252525;">and also at </span><span style="color:#252525;">'''matplotlib.sourceforge.net/gallery.html'''</span>
+
'''matplotlib.sourceforge.net/users/screenshots.html'''
  
 +
and also at '''matplotlib.sourceforge.net/gallery.html'''
  
  
|- style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"
 
| <div style="color:#252525;">Show Slide </div>
 
  
<div style="color:#252525;">Summary slide</div>
 
  
 +
|-
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Show Slide
  
 +
Summary slide
  
| <div style="color:#252525;">This brings us to the end of this tutorial.</div>
 
  
  
<div style="color:#252525;">In this tutorial we learnt to,</div>
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| This brings us to the end of this tutorial.
  
  
* <div style="margin-left:0cm;margin-right:0cm;"><span style="color:#252525;">Plot</span><span style="color:#252525;"> a </span><span style="color:#252525;">'''pie'''</span><span style="color:#252525;"> </span><span style="color:#252525;">'''chart'''</span><span style="color:#252525;"> </span><span style="color:#252525;">using</span><span style="color:#252525;"> </span><span style="color:#252525;">'''pie()'''</span><span style="color:#252525;"> </span><span style="color:#252525;">function</span></div>
+
In this tutorial we learnt to,
* <div style="margin-left:0cm;margin-right:0cm;"><span style="color:#252525;">Plot a </span><span style="color:#252525;">'''bar'''</span><span style="color:#252525;"> </span><span style="color:#252525;">'''chart'''</span><span style="color:#252525;"> using </span><span style="color:#252525;">'''bar()'''</span><span style="color:#252525;"> </span><span style="color:#252525;">function</span></div>
+
* <div style="margin-left:0cm;margin-right:0cm;"><span style="color:#252525;">Access the</span><span style="color:#252525;"> </span><span style="color:#252525;">'''matplotlib '''</span><span style="color:#252525;">online help</span></div>
+
  
  
|- style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"
+
* Plot a '''pie''' '''chart''' using '''pie()''' function
| <div style="color:#252525;">Show Slide </div>
+
* Plot a '''bar''' '''chart''' using '''bar()''' function
 +
* Access the '''matplotlib '''online help
  
<div style="color:#252525;">Assignment</div>
 
  
  
 +
|-
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Show Slide
  
| <div style="color:#252525;">Here are some self assessment questions for you to solve.</div>
+
Assignment
  
  
<span style="color:#252525;">1.What statement can be issued to generate a bar chart with vertical line hatching</span><span style="color:#252525;">?</span>* <div style="color:#252525;margin-left:1.27cm;margin-right:0cm;">'''bar(x, y, color='w', hatch='/')'''</div>
 
* <div style="color:#252525;margin-left:1.27cm;margin-right:0cm;">'''bar(x, y, fill=False, hatch='//')'''</div>
 
* <div style="color:#252525;margin-left:1.27cm;margin-right:0cm;">'''bar(x, y, fill=False, hatch='|')'''</div>
 
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#252525;">'''bar(x, y, color='w', hatch='\'</span><span style="color:#252525;">'')'''''</span></div>
 
  
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Here are some self assessment questions for you to solve.
  
|- style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"
 
| <div style="color:#252525;">Show Slide </div>
 
  
<div style="color:#252525;">Solution to assignment</div>
+
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='\''')'''''
  
  
| <div style="color:#252525;">And the answers,</div>
 
  
 +
|-
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Show Slide
  
# <div style="margin-left:0cm;margin-right:0cm;"><span style="color:#252525;">'''bar(x, y, fill=False, hatch='|')'''</span><span style="color:#252525;"> </span><span style="color:#252525;">is the correct option to generate a </span><span style="color:#252525;">'''bar'''</span><span style="color:#252525;"> </span><span style="color:#252525;">'''chart'''</span><span style="color:#252525;"> with vertical </span><span style="color:#252525;">'''line'''</span><span style="color:#252525;"> </span><span style="color:#252525;">'''hatching'''</span><span style="color:#252525;">.</span></div>
+
Solution to assignment
  
  
  
<div style="color:#252525;"></div>
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| And the answers,
|- style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"
+
| <div style="color:#000000;">Show Slide</div>
+
  
<div style="color:#000000;">Forum</div>
 
| style="color:#000000;" | Please post your timed queries in this forum.
 
|- style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"
 
| <div style="color:#000000;">Show Slide</div>
 
  
<div style="color:#000000;">Fossee Forum</div>
+
# '''bar(x, y, fill=False, hatch='|')''' is the correct option to generate a '''bar''' '''chart''' with vertical '''line''' '''hatching'''.
| style="color:#000000;" | Please post your general queries on Python in this forum.
+
|- style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"
+
| <div style="color:#000000;">Show Slide</div>
+
  
<div style="color:#000000;">Textbook Companion</div>
 
| style="color:#000000;" | FOSSEE team coordinates the TBC project.
 
|- style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"
 
| <div style="color:#000000;">Show Slide</div>
 
  
<div style="color:#000000;">Acknowledgment</div>
 
  
<div style="color:#000000;">http://spoken-tutorial.org</div>
+
|-
| <div style="color:#000000;">Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.</div>
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Show Slide
  
<div style="color:#000000;">For more details, visit this website.</div>
+
Forum
|- style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Please post your timed queries in this forum.
| style="color:#000000;" | Previous slide
+
| <div style="color:#000000;">This is _________ from IIT Bombay (or FOSSEE, if you wish) signing off.</div>
+
  
<div style="color:#000000;">Thank you.</div>
 
 
|-
 
|-
|}
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Show Slide
  
 +
Fossee Forum
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Please post your general queries on Python in this forum.
  
<div style="color:#000000;margin-left:0cm;margin-right:0cm;"></div>
+
|-
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Show Slide
  
<div style="color:#000000;margin-left:0cm;margin-right:0cm;"></div><div style="margin-left:0cm;margin-right:0cm;"></div>
+
Textbook Companion
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| FOSSEE team coordinates the TBC project.
 +
 
 +
|-
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Show Slide
 +
 
 +
Acknowledgment
 +
 
 +
http://spoken-tutorial.org
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.
 +
 
 +
For more details, visit this website.
 +
 
 +
|-
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Previous slide
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| This is _________ from IIT Bombay (or FOSSEE, if you wish) signing off.
 +
 
 +
Thank you.
 +
 
 +
|}

Revision as of 15:51, 7 June 2017

'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 slide
Narrator name is missing in the first slide
Narrator name is missing in the first slide
Narrator name is missing in the first slide
Narrator name is missing in the first slide

charts

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 slide

to 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?"
interpreter - this word is at the end in the slide. Delete this. Give different color for "pie?"
interpreter - this word is at the end in the slide. Delete this. Give different color for "pie?"

Plot 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 Nirmala
let us share and see the reply from Nirmala
let us share and see the reply from Nirmala
let us share and see the reply from Nirmala
let us share and see the reply from Nirmala
let us share and see the reply from Nirmala
let us share and see the reply from Nirmala

Type 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 above

Type 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?"
Hint: In ipython interpreter, try typing bar? change the slide. Give different color for the command "bar?"
Hint: In ipython interpreter, try typing bar? change the slide. Give different color for the command "bar?"

Clue 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 output

Type 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

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