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...")
 
 
(21 intermediate revisions by 2 users not shown)
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>
+
'''Title of script: Plotting Charts'''
  
<div style="color:#000000;margin-left:0cm;margin-right:0cm;">'''Author: Anoop Jacob Thomas, Thirumalesh H S'''</div>
+
'''Author: Trupti Kini, 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 charts'''
  
 +
|-
 +
| 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
  
| <div style="color:#252525;">At the end of this tutorial, you will be able 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;"| At the end of this tutorial, you will be able to -  
  
 +
* Create '''pie charts'''
 +
* Create '''bar charts '''and
 +
* Find more information on '''matplotlib'''
  
* <div style="margin-left:0cm;margin-right:0cm;"><span style="color:#252525;">Create </span><span style="color:#252525;">'''pie charts'''</span></div>
+
|-
* <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>
+
| 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;">Find more information on </span><span style="color:#252525;">'''matplotlib'''</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;"
+
| 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;"| Open [Terminal] and type
  
 
'''ipython3'''
 
'''ipython3'''
  
  
 
+
| 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.  
| 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.  
+
 
+
  
 
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]
 
  
'''%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;"| [IPython console]
  
 +
type '''%pylab '''and press '''Enter.'''
 +
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| Let us initialize 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'''
  
 +
| 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, to illustrate numerical proportion
  
| <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;"
+
| 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;">Show Slide</div>
+
  
<span style="color:#252525;">'''pie()'''</span><span style="color:#252525;"> </span><span style="color:#252525;">function</span>
+
'''pie()''' function
  
<div style="color:#ff0000;">Syntax:</div>
+
Syntax:
  
<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>
+
'''pie(values, labels= labels)'''
* <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>
+
  
 +
* '''values – '''the data to be plotted
 +
* '''labels''' – the label for each wedge in the pie chart
  
  
<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;"| The syntax for pie() function is as follows:
| <span style="color:#252525;">The s</span><span style="color:#252525;">yntax</span><span style="color:#252525;"> for pie() function is as follows:</span>
+
  
 +
'''pie ''inside parentheses'' values ''comma'' labels ''equal to'' labels'''
  
<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>
+
* where '''values are '''the data to be plotted
* <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>
+
* and '''labels''' – the label for each wedge in the pie chart
  
 +
|-
 +
| 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;"
+
Exercise 1
| <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;"|
 +
* Plot a '''pie''' '''chart''' representing the profit percentage of company A.
  
<div style="color:#252525;">Exercise 1</div>
+
* Use the data from file '''company-a-data.txt''' available in the code file link of this tutorial
| * <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>
+
  
 +
|-
 +
| 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;"></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>
+
The first column represents years. The second column represents the profit percentages.
 
+
 
+
 
+
available in the code file link of this tutorial
+
 
+
 
+
 
+
|- 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>
+
 
+
 
+
<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;"
+
| <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]
  
 
'''year, profit = loadtxt('company-a-data.txt', unpack = True)'''
 
'''year, profit = loadtxt('company-a-data.txt', unpack = True)'''
  
 +
| 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, we first need to load the data from the file using '''loadtxt''' command.
  
 +
Let us clear the '''terminal'''.
  
| <span style="color:#ff0000;">To produce </span><span style="color:#ff0000;">a</span><span style="color:#ff0000;"> scatter plot, </span>
+
Type
  
 +
'''year ''comma'' profit ''equal to'' loadtxt ''inside parentheses inside single quotes'' add the path of the file company-a-data.txt ''comma'' unpack ''equal to'' True''' and press '''Enter'''
  
<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>
+
'''unpack ''equal to'' True --> returns''' the '''transposed array '''of '''data'''.
 
+
<div style="color:#ff0000;">Type</div>
+
 
+
 
+
<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>
+
 
+
 
+
<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>
+
|- 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]
  
 
'''clf()'''
 
'''clf()'''
Line 153: Line 135:
 
'''pie(profit,labels=year)'''
 
'''pie(profit,labels=year)'''
  
 +
| 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 ''open and close parentheses'' '''to clear the plot window, if any plots executed previously.
  
 +
Type '''pie(profit ''comma'' labels equal to year)''' and press '''Enter'''.
  
| 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;"| [Plot Window]
 
+
Type '''pie(profit,labels=year)'''
+
 
+
 
+
 
+
|- 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]
+
  
  
 
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()'''.  
  
 +
First one being the '''values,''' which is '''profit''' in our case.
  
<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>
+
The next one being the set of '''labels''' to be used in the '''pie chart''', which is '''year''' 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>
 
 
|-
 
|-
| 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: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.
  
| 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>
 
  
<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.
+
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'''.
  
* <div style="margin-left:1.27cm;margin-right:0cm;">'''white, red, black, magenta,'''</div>
+
Hint: In your '''ipython interpreter, '''try typing '''pie question mark'''.
* <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>
+
 
+
 
+
 
+
<div style="color:#252525;"></div>
+
 
+
<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>
+
|- 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]
  
 
'''clf()'''
 
'''clf()'''
 
  
 
'''pie(profit,labels=year,colors=('w','r','k','m','y','b','g','c','y','m','b'))'''
 
'''pie(profit,labels=year,colors=('w','r','k','m','y','b','g','c','y','m','b'))'''
  
 +
| 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
  
 +
Clear the '''plot window '''by typing '''clf open and close parentheses''' and press '''Enter'''.
  
| <div style="color:#252525;">Solution to Exercise 2</div>
+
Type '''pie inside parentheses profit comma labels equal to year comma colors equal to inside parentheses the color codes ''' and press '''Enter'''.
  
 +
This will display a pie chart with each wedge having the color as specified in the parameters.
  
Clear the '''plot window '''by typing '''clf()'''
+
|-
 +
| 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
  
 +
'''Bar chart'''
  
<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>
+
| 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:#000000;">"Plot a pie chart with the data with colors for each wedges as white, red, black, "</span>
+
* with rectangular bars
 +
* with lengths proportional to the values that they represent.
  
 +
|-
 +
| 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:#000000;">I felt it will be repetition of info.</span>
+
'''bar()''' function
 +
| 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 chart is:
  
 +
'''bar ''inside parentheses'' x ''comma'' y'''
  
<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>
+
* where '''x –''' a sequence of data and
 +
* '''y '''- a sequence of data, the same length of x
  
 +
|-
 +
| 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:#000000;">That will be cleared using clf()</span>
+
Exercise 3
  
 +
| 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.
  
<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')) '''
+
* Use the data from file '''company-a-data.txt'''.
  
 
+
|-
<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;"| [IPython Terminal]
 
+
 
+
 
+
|- 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;">'''Bar chart'''</div>
+
 
+
 
+
 
+
| <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>
+
* <div style="color:#252525;margin-left:1.27cm;margin-right:0cm;">with lengths proportional to the values that they represent.</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>
+
 
+
 
+
<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>
+
* <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:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"
+
| <div style="color:#252525;">Show Slide</div>
+
 
+
<div style="color:#252525;">Exercise 3</div>
+
 
+
 
+
 
+
| * <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>
+
 
+
 
+
 
+
* <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:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"
+
| <div style="color:#252525;">[IPython Terminal]</div>
+
  
  
Line 277: Line 222:
 
'''bar(year, profit)'''
 
'''bar(year, profit)'''
  
 +
| 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
  
 +
Clear the '''plot window '''by typing '''clf ''open and close parentheses'''''
  
| <div style="color:#252525;">Solution to Exercise 3</div>
+
Type '''bar ''open and close parentheses'' year ''comma'' profit''' and press '''Enter'''.
  
 +
This will display a bar chart representing the profit percentage of company A
  
<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>
+
|-
 +
| 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:#000000;">same as above</span><span style="color:#252525;">Type </span><span style="color:#252525;">'''bar(year, profit)'''</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:
  
 +
<u>'''Hint'''</u>: Bar chart is not filled and which is '''hatched''' with 45<sup>o</sup> slanting lines.
  
<div style="color:#252525;">This will display a bar chart representing the profit percentage of company A</div>
+
The data for the chart may be obtained from the 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;">Show Slide </div>
+
  
<div style="color:#252525;">Exercise 4</div>
+
Hint: In your '''ipython interpreter, '''try typing '''bar ''question mark'''''
  
  
 +
|-
 +
| 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''']'''
  
| <span style="color:#ff0000;">Plot a </span><span style="color:#ff0000;">'''bar chart '''</span><span style="color:#ff0000;">as shown in the image:</span>
+
'''clf()'''
 
+
 
+
<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>
+
 
+
<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>
+
 
+
 
+
<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>
+
 
+
 
+
<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>
+
|- 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>
+
 
+
  
 
'''bar(year,profit,fill=False,hatch='/')'''
 
'''bar(year,profit,fill=False,hatch='/')'''
  
 +
| 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
  
 +
Clear the '''plot window '''by typing '''clf ''open and close parentheses'''''
  
| <div style="color:#252525;">Solution to Exercise 4</div>
+
Type '''bar ''open and close parentheses'' year ''comma'' profit ''comma'' fill ''equal to'' False ''comma'' hatch ''equal to inside single quotes'' ''slanting line''''' and press '''Enter'''.
  
 +
This will display a bar chart which is not filled and which is '''hatched''' with 45<sup>o</sup> slanting lines
  
<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>
+
|-
 +
| 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
  
 +
"Getting help on matplotlib"
  
<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>
+
{SHOW WEBSITE while narrating}
  
 +
'''matplotlib.sourceforge.net/users/screenshots.html'''
  
<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>
+
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;">"Getting help on matplotlib"</div>
+
| 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'''
  
<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>
+
More plots can be seen at the following links
| <div style="color:#252525;"></div>
+
  
<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>
+
|-
 +
| 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;">More plots can be seen at</div>
+
Summary slide
  
<div style="color:#252525;">'''matplotlib.sourceforge.net/users/screenshots.html'''</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.
  
<span style="color:#252525;">and also at </span><span style="color:#252525;">'''matplotlib.sourceforge.net/gallery.html'''</span>
+
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
  
 +
|-
 +
| 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;"
+
Assignment
| <div style="color:#252525;">Show Slide </div>
+
* '''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;">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;"| 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?
  
  
| <div style="color:#252525;">This brings us to the end of this tutorial.</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
  
 +
Solution to assignment
  
<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;"| And the answer is,
  
  
* <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>
+
# '''bar x ''comma'' y ''comma'' fill ''equal to'' False ''comma'' hatch ''is equal to'' a vertical line)''' is the correct option to generate a '''bar''' '''chart''' with vertical '''line''' '''hatching'''.
* <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;"
+
|-
| <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;">Assignment</div>
+
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 timed queries in this forum.
  
 +
|-
 +
| 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:#252525;">Here are some self assessment questions for you to solve.</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
  
 +
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.
  
<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>
+
| 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;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>
+
  
 +
Acknowledgment
  
|- style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"
+
http://spoken-tutorial.org
| <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;"| Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.
  
<div style="color:#252525;">Solution to assignment</div>
+
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 Trupti Kini from FOSSEE IIT Bombay signing off.
  
 +
Thank you.
  
| <div style="color:#252525;">And the answers,</div>
 
 
 
# <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>
 
 
 
 
<div style="color:#252525;"></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:#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>
 
| 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>
 
 
<div style="color:#000000;">For more details, visit this website.</div>
 
|- style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"
 
| 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>
 
|-
 
 
|}
 
|}
 
 
<div style="color:#000000;margin-left:0cm;margin-right:0cm;"></div>
 
 
<div style="color:#000000;margin-left:0cm;margin-right:0cm;"></div><div style="margin-left:0cm;margin-right:0cm;"></div>
 

Latest revision as of 22:16, 7 September 2017

Title of script: Plotting Charts

Author: Trupti Kini, 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 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.

Open [Terminal] and type

ipython3


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

Now, type ipython3 and press Enter.

[IPython console]

type %pylab and press Enter.

Let us initialize the pylab package.

Type %pylab and press Enter.

Show Slide

Pie chart

A pie chart is a circular chart divided into sectors, 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 inside parentheses values comma labels equal to labels

  • where values are the data to be plotted
  • and 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 column 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.

Let us clear the terminal.

Type

year comma profit equal to loadtxt inside parentheses inside single quotes add the path of the file company-a-data.txt comma unpack equal to True and press Enter

unpack equal to True --> returns the transposed array of data.

[Ipython Terminal]

clf()

pie(profit,labels=year)

Type clf open and close parentheses to clear the plot window, if any plots executed previously.

Type pie(profit comma labels equal to year) and press Enter.

[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.


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 question mark.

[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 open and close parentheses and press Enter.

Type pie inside parentheses profit comma labels equal to year comma colors equal to inside parentheses the color codes and press Enter.

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 chart is:

bar inside parentheses x comma y

  • where x – a sequence of data and
  • 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 open and close parentheses

Type bar open and close parentheses year comma profit and press Enter.

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 question mark


[Ipython Terminal]

clf()

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

Solution to Exercise 4

Clear the plot window by typing clf open and close parentheses

Type bar open and close parentheses year comma profit comma fill equal to False comma hatch equal to inside single quotes slanting line and press Enter.

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}

matplotlib.sourceforge.net/users/screenshots.html

and also at matplotlib.sourceforge.net/gallery.html

Help about matplotlib can be obtained from matplotlib.sourceforge.net/contents.html

More plots can be seen at the following links

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

  • bar(x, y, color='w', hatch='/')
  • bar(x, y, fill=False, hatch='//')
  • bar(x, y, fill=False, hatch='|')
  • bar(x, y, color='w', hatch='\')
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?


Show Slide

Solution to assignment

And the answer is,


  1. bar x comma y comma fill equal to False comma hatch is equal to a vertical line) 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 Trupti Kini from FOSSEE IIT Bombay signing off.

Thank you.

Contributors and Content Editors

Nancyvarkey, Nirmala Venkat, Trupti