How to use Matplotlib as the Plotting Library
Matplotlib is a plotting library used in python and written in python. Matplotlib is cross platform and released under Matplotlib license. We can get MATLAB like interface using Pyplot, which is a module in Matplotlib. It is free and Open-Source. Several tool kits are available which extend Matplotlib functionality such as,
Basemap
Cartopy
Excel tools
GTK tools
Natgrid and more …
By using Matplotlib we can visualize data in,
Bar Graph
Histogram
Scatter Plot
Area Plot
Pie Plot and some other ways
Let’s see what we can do with this powerful library.
Import
import matplotlib.pyplot as plt
from matplotlib import style
Graph
Basic Plot
plt.plot([1,2,3],[2,5,7])
plt.show()
Add Title and Labels
x=[2,4,8]
y=[4,8,24]
x1=[3,6,9]
y1=[20,12,15]
plt.plot(x,y)
plt.plot(x1,y1)
plt.title("Simple Graph")
plt.xlabel("Student ID")
plt.ylabel("Attendence")
plt.show()
Add Style : bmh
plt.style.use("bmh")
x=[2,4,8]
y=[4,8,24]
x1=[3,6,9]
y1=[20,12,15]
plt.plot(x,y)
plt.plot(x1,y1)
plt.title("Simple Graph")
plt.xlabel("Student ID")
plt.ylabel("Attendence")
plt.show()
Add Style : Classic
plt.style.use("classic")
x=[2,4,8]
y=[4,8,24]
x1=[3,6,9]
y1=[20,12,15]
plt.plot(x,y)
plt.plot(x1,y1)
plt.title("Simple Graph")
plt.xlabel("Student ID")
plt.ylabel("Attendence")
plt.show()
Add Style : Dark Background
plt.style.use("dark_background")
x=[2,4,8]
y=[4,8,24]
x1=[3,6,9]
y1=[20,12,15]
plt.plot(x,y)
plt.plot(x1,y1)
plt.title("Simple Graph")
plt.xlabel("Student ID")
plt.ylabel("Attendence")
plt.show()
Bar Graph
Basic Plot with Classic
plt.style.use("classic")
x1=[2,4,6]
y1=[3,6,9]
x2=[13,6,9]
y2=[20,12,15]
x3=[12,14,12]
y3=[2,3,12]
plt.bar(x1,y1)
plt.bar(x2,y2)
plt.bar(x3,y3)
plt.title("Simple Bar Graph")
plt.xlabel("Student ID")
plt.ylabel("Attendence")
plt.show()
Add Style: bmh
plt.style.use("bmh")
x1=[2,4,6]
y1=[3,6,9]
x2=[13,6,9]
y2=[20,12,15]
x3=[12,14,12]
y3=[2,3,12]
plt.bar(x1,y1)
plt.bar(x2,y2)
plt.bar(x3,y3)
plt.title("Simple Bar Graph")
plt.xlabel("Student ID")
plt.ylabel("Attendence")
plt.show()
Add Style : Dark Background
plt.style.use("dark_background")
x1=[2,4,6]
y1=[3,6,9]
x2=[13,6,9]
y2=[20,12,15]
x3=[12,14,12]
y3=[2,3,12]
plt.bar(x1,y1)
plt.bar(x2,y2)
plt.bar(x3,y3)
plt.title("Simple Bar Graph")
plt.xlabel("Student ID")
plt.ylabel("Attendence")
plt.show()
Histogram — Bar
Basic Plot with Classic
plt.style.use("classic")
numbers=[12,89,97,56,34,54,32,84,34,65,23,87,12,65,87,34]
jumps=[0,15,30,45,60,75,90,105]
plt.title("Simple Hostogram")
plt.xlabel("Marks")
plt.ylabel("No. of Students")
plt.hist(numbers,jumps,histtype='bar')
plt.show()
Histogram — Step
Basic Plot with Classic
plt.style.use("classic")
numbers=[12,89,97,56,34,54,32,84,34,65,23,87,12,65,87,34]
jumps=[0,15,30,45,60,75,90,105]
plt.title("Simple Hostogram")
plt.xlabel("Marks")
plt.ylabel("No. of Students")
plt.hist(numbers,jumps,histtype='step')
plt.show()
Pie Chart
Basic Plot with Classic
plt.style.use("classic")
foods=['pizza','ice cream','burgers']
sold=[20,35,12]
colors=['red','blue','yellow']
plt.pie(sold,labels=foods,colors=colors)
plt.show()
Add Style : Dark Background
plt.style.use("dark_background")
foods=['pizza','ice cream','burgers']
sold=[20,35,12]
colors=['red','blue','yellow']
plt.pie(sold,labels=foods,colors=colors)
plt.show()
Scatter Plot
Basic Plot with Classic
plt.style.use("classic")
x=[1,2,3,4,5]
y=[12,11,4,7,1]
plt.title("Simple Scatter Plot")
plt.xlabel("ID")
plt.ylabel("Attendance")
plt.scatter(x,y)
plt.show()
We can really do amazing things using Matplotlib. If you want to learn more, please refer to documentation of Matplotlib