Data Visualisation using PyPlot [ Class 12 ]


Matplotlib is the whole python package/ library used to create 2D graphs and plots by using python scripts. PyPlot is a module in matplotlib, which supports a very wide variety of graphs and plots namely – histogram, bar charts, power spectra, error charts etc.

It is used along with NumPy to provide an environment for MatLab.

Pyplot provides the state-machine interface to the plotting library in matplotlib.It means that figures and axes are implicitly and automatically created to achieve the desired plot. For example, a calling plot from pyplot will automatically create the necessary figure and axes to achieve the desired plot.

Setting a title will then automatically set that title to the current axes object. The pyplot interface is generally preferred for non-interactive plotting (i.e.,scripting).

Line Chart

The line chart is represented by a series of data points connected with a straight line.Generally, line charts are
used to display trends over time. A line chart or line graph can be created using the plot() function available in pyplot library.We can not only just plot a line but we can explicitly define the grid, the x and y-axis scale and labels, title and display options.

Sample Python Program to Draw Line Chart

# program to print scatter graph on the screen
# made by        : rakesh kumar

import matplotlib.pyplot as plt
import numpy as np
x = ['Delhi', 'Banglore', 'Chennai', 'Pune']
y = [250, 300, 260, 400]
plt.xlabel('City')
plt.ylabel('Sales in Million')
plt.title('Sales Recorded in Major Cities')
plt.plot(x, y)
plt.show()

The output of above line chart python program is as followsline chart using python

Bar Chart using Python PyPlot

A bar chart/bar graph, is a very common two-dimensional data visualization made up of rectangular bars, each for a specific category and it’s length represents the value of that category.

Python Program to draw Bar chart

# program to print bar graph on the screen
# made by        : rakesh kumar

import matplotlib.pyplot as plt
x = ['Delhi', 'Banglore', 'Chennai', 'Pune']
y = [250, 300, 260, 400]
plt.xlabel('City')
plt.ylabel('Sales in Million')
plt.title('Sales Recorded in Major Cities')
plt.bar(x, y)
plt.show()

The output of the above Bargraph program is
Bar chart

Pie Chart using PyPlot

A pie graph/pie chart is a specialized graph used in statistics. The independent variable is plotted around a
circle.Pie Charts shows proportions and percentages between categories, by dividing a circle into proportional
segments/parts. Each arc length represents a proportion of each category, while the full circle represents the total sum of all the data, equal to 100%.

Python Program to draw Pie-chart

# program to print pie graph on the screen
# made by        : rakesh kumar

import matplotlib.pyplot as plt
import numpy as np
x = ['Delhi','Banglore','Chennai','Pune','Ghaziabad','Udupi']
y = [250,300,260,400,599,320]
plt.pie(y,labels=x,autopct='%1.2f',startangle=90,explode=(0,0.1,0,0,0.2,0))
plt.show()

The output of the Python Pie-chart Program

pie-chart-using-python

Well, these are some very simple data visualization using pyplot. I think now you have the idea. If you have any queries regarding data visualization using python Pyplot. Please send us your request via email.

Print Friendly, PDF & Email
If you like CBSEToaday and would like to contribute, you can also write an article using submit article or mail your article to contribute@cbsetoday.com See your article appearing on the cbsetoday.com main page and help other students/teachers.