Difference between revisions of "Python-3.4.3/C2/Plotting-Charts/English"
Nancyvarkey (Talk | contribs) |
|||
(One intermediate revision by one other user not shown) | |||
Line 22: | Line 22: | ||
Learning Objectives | 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;"| At the end of this tutorial, you will be able to - | | 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 '''pie charts''' | ||
* Create '''bar charts '''and | * Create '''bar charts '''and | ||
* Find more information on '''matplotlib''' | * Find more information on '''matplotlib''' | ||
− | |||
− | |||
|- | |- | ||
Line 43: | Line 38: | ||
* '''Python 3.4.3''' | * '''Python 3.4.3''' | ||
* '''IPython 5.1.0''' | * '''IPython 5.1.0''' | ||
− | |||
− | |||
|- | |- | ||
Line 62: | Line 55: | ||
'''ipython3''' | '''ipython3''' | ||
− | |||
Line 81: | Line 73: | ||
'''Pie chart''' | '''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 | | 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 | ||
Line 105: | Line 95: | ||
* where '''values are '''the data to be plotted | * where '''values are '''the data to be plotted | ||
* and '''labels''' – the label for each wedge in the pie chart | * and '''labels''' – the label for each wedge in the pie chart | ||
− | |||
|- | |- | ||
Line 115: | Line 104: | ||
* Use the data from file '''company-a-data.txt''' available in the code file link of this tutorial | * Use the data from file '''company-a-data.txt''' available in the code file link of this tutorial | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 124: | Line 110: | ||
Open the file '''company-a-data.txt''' and show the content | 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. | | 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. | ||
− | |||
The first column represents years. The second column represents the profit percentages. | The first column represents years. The second column represents the profit percentages. | ||
Line 130: | Line 115: | ||
|- | |- | ||
| 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: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'''. | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
Type | 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''' | '''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'''. | '''unpack ''equal to'' True --> returns''' the '''transposed array '''of '''data'''. | ||
Line 151: | Line 130: | ||
|- | |- | ||
| 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:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| [Ipython Terminal] | ||
− | |||
'''clf()''' | '''clf()''' | ||
'''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. | | 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 '''pie(profit ''comma'' labels equal to year)''' and press '''Enter'''. | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 173: | Line 145: | ||
Show '''pie chart '''plot | Show '''pie chart '''plot | ||
| 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()'''. | | 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. | 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. | The next one being the set of '''labels''' to be used in the '''pie chart''', which is '''year''' in our case. | ||
Line 185: | Line 155: | ||
Exercise 2 | 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. | |
− | + | ||
− | + | ||
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,''' | * '''white, red, black, magenta,''' | ||
Line 202: | Line 170: | ||
|- | |- | ||
| 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: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 | | 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'''. | 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'''. | 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. | This will display a pie chart with each wedge having the color as specified in the parameters. | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 229: | Line 187: | ||
'''Bar chart''' | '''Bar 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 '''bar chart''' is a '''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 '''bar chart''' is a '''chart''' | ||
Line 236: | Line 192: | ||
* with rectangular bars | * with rectangular bars | ||
* with lengths proportional to the values that they represent. | * 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 | | 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()''' function | '''bar()''' function | ||
Line 250: | Line 203: | ||
* where '''x –''' a sequence of data and | * where '''x –''' a sequence of data and | ||
* '''y '''- a sequence of data, the same length of x | * '''y '''- a sequence of data, the same length of x | ||
− | |||
− | |||
|- | |- | ||
Line 257: | Line 208: | ||
Exercise 3 | 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;"| | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| | ||
Line 264: | Line 213: | ||
* Use the data from file '''company-a-data.txt'''. | * Use the data from file '''company-a-data.txt'''. | ||
− | |||
− | |||
|- | |- | ||
Line 274: | Line 221: | ||
'''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 | | 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''''' | Clear the '''plot window '''by typing '''clf ''open and close parentheses''''' | ||
− | |||
− | |||
Type '''bar ''open and close parentheses'' year ''comma'' profit''' and press '''Enter'''. | 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 | This will display a bar chart representing the profit percentage of company A | ||
Line 293: | Line 234: | ||
Exercise 4 | Exercise 4 | ||
− | |||
− | |||
| 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: | | 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. | <u>'''Hint'''</u>: Bar chart is not filled and which is '''hatched''' with 45<sup>o</sup> slanting lines. | ||
The data for the chart may be obtained from the file '''company-a-data.txt'''. | 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''''' | Hint: In your '''ipython interpreter, '''try typing '''bar ''question mark''''' | ||
− | |||
Line 312: | Line 248: | ||
'''clf()''' | '''clf()''' | ||
− | |||
'''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 | | 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''''' | 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'''. | 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 | This will display a bar chart which is not filled and which is '''hatched''' with 45<sup>o</sup> slanting lines | ||
Line 334: | Line 265: | ||
{SHOW WEBSITE while narrating} | {SHOW WEBSITE while narrating} | ||
− | |||
− | |||
− | |||
'''matplotlib.sourceforge.net/users/screenshots.html''' | '''matplotlib.sourceforge.net/users/screenshots.html''' | ||
Line 342: | Line 270: | ||
and also at '''matplotlib.sourceforge.net/gallery.html''' | and also at '''matplotlib.sourceforge.net/gallery.html''' | ||
+ | | 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''' | ||
− | + | More plots can be seen at the following links | |
|- | |- | ||
Line 349: | Line 278: | ||
Summary slide | Summary 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 brings us to the end of this tutorial. | | 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. | ||
− | |||
In this tutorial we learnt to, | In this tutorial we learnt to, | ||
− | |||
* Plot a '''pie''' '''chart''' using '''pie()''' function | * Plot a '''pie''' '''chart''' using '''pie()''' function | ||
* Plot a '''bar''' '''chart''' using '''bar()''' function | * Plot a '''bar''' '''chart''' using '''bar()''' function | ||
* Access the '''matplotlib '''online help | * Access the '''matplotlib '''online help | ||
− | |||
− | |||
|- | |- | ||
Line 368: | Line 291: | ||
Assignment | Assignment | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
* '''bar(x, y, color='w', hatch='/')''' | * '''bar(x, y, color='w', hatch='/')''' | ||
* '''bar(x, y, fill=False, hatch='//')''' | * '''bar(x, y, fill=False, hatch='//')''' | ||
Line 381: | Line 296: | ||
* '''bar(x, y, color='w', hatch='\''')''''' | * '''bar(x, y, color='w', hatch='\''')''''' | ||
+ | | 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? | ||
Line 387: | Line 305: | ||
Solution to assignment | Solution to assignment | ||
− | |||
− | |||
| 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, | | 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, | ||
Line 394: | Line 310: | ||
# '''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'''. | # '''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'''. | ||
− | |||
Line 427: | Line 342: | ||
|- | |- | ||
| 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;"| 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 | + | | 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. | Thank you. | ||
|} | |} |
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 -
|
Show slide
System Specifications |
To record this tutorial, I am using
|
Show slide
Pre-requisites |
To practice this tutorial, you should know how to
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)
|
The syntax for pie() function is as follows:
pie inside parentheses values comma labels equal to labels
|
Show Slide
Exercise 1 |
|
[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]
|
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.
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
|
Show Slide
bar() function |
The syntax for bar chart is:
bar inside parentheses x comma y
|
Show Slide
Exercise 3 |
|
[IPython Terminal]
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,
|
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?
|
Show Slide
Solution to assignment |
And the answer is,
|
Show Slide
Forum |
Please post your timed queries in this forum. |
Show Slide
Fossee Forum |
Please post your general queries on Python in this forum. |
Show Slide
Textbook Companion |
FOSSEE team coordinates the TBC project. |
Show Slide
Acknowledgment |
Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.
For more details, visit this website. |
Previous slide | This is Trupti Kini from FOSSEE IIT Bombay signing off.
Thank you. |