Hello friends. Welcome to the tutorial on plotting charts
At the end of this tutorial, you will be able to Create pie charts Create bar charts and Find more information on matplotlib
To record this tutorial, I am using Ubuntu Linux 14.04 operating system
Python 3.4.3 and IPython 5.1.0
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.
Let us first open the Terminal by pressing Ctrl+Alt+T keys simultaneously. Now, type ipython3 and press Enter.
Let us initialize the pylab package. Type %pylab and press Enter.
A pie chart is a circular chart divided into sectors, to illustrate numerical proportion
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
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
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.
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.
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.
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.
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.
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.
A bar chart is a chart with rectangular bars with lengths proportional to the values that they represent.
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
Plot a bar chart representing the profit percentage of company A.
Use the data from file company-a-data.txt.
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
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
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
Help about matplotlib can be obtained from matplotlib.sourceforge.net/contents.html More plots can be seen at the following links
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
And Access the matplotlib online help
Here are some self assessment questions for you to solve.
What statement can be issued to generate a bar chart with vertical line hatching?
And the answer is, bar x comma y comma fill is equal to False comma hatch is equal to a vertical line is the correct option to generate a bar chart with vertical line hatching.
Please post your timed queries in this forum.
Please post your general queries on Python in this forum.
FOSSEE team coordinates the TBC project.
Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India. For more details, visit this website.
This is Trupti Kini from FOSSEE IIT Bombay signing off. Thank you.