Python Matplotlib Tutorial:-
Why Data Visualization?
What is Data Visualization?
What is Matplotlib?
Types Of Plots
Getting Started
1.Why Data Visualizaton?:-
Human brain can process
information easily when it is in pictorial or
graphical form.
Data visualization allows us to quickly
interpret the data and adjust different
variables to see their effect.
2.What is Data Visualization?
Data visualization is the presentation of
data in a pictorial or graphical format.
V i
s u
a l
iz A
e n
a l
D o c y
u m s
e n t e
I n s i
g h t
3.What is Matplotlib?
Matplotlib is a Python package used for 2D
graphics.
4.Types of Plots:-
1.Bar Graph
2.Histograms
3.Scatter Plot
4.Pie Plot
5.Hexagonal Bin Plot
6.Area Plot
5.Getting Started:-
Here's some asic code to generate one of the
most simple graph.
Example:-
from matplotlib import pyplot as plt
#Plotting to our canvas
plt.plot([1,2,3],[4,5,1])
#Showing what we plotted
plt.show()
Output:-
Let's add title and labels to our graph:-
Example:-
from matplotlib import pyplot as plt
x=[5,3,6]
y=[3,5,2]
plt.plot(x,y)
plt.title("Information")
plt.ylabel("Y-Axis")
plt.xlabel("X-Axis")
plt.show()
Adding Style To Our Graph:-
from matplotlib import pyplot as plt
from matplotlib import style
style.use("ggplot")
x=[5,3,2]
y=[4,2,5]
x2=[4,3,7]
y2=[6,7,9]
plt.plot(x,y,'g',label='line one',linewidth=5)
plt.plot(x2,y2,'c',label='line two',linewidth=5)
plt.title('Epic Info')
plt.ylabel("Y-Axis")
plt.xlabel("X-Axis")
plt.legend()
plt.grid(True,color='k')
plt.show()
Bar Graph:-
import matplotlib.pyplot as plt
plt.bar([3,2,4,5],[6,3,2,6],label="Example
One")
plt.bar([4,2,5,6],[9,0,8,7],label="Example
two",color='g')
plt.legend()
plt.xlabel('bar number')
plt.ylabel('bar height')
plt.title('My plot yo!')
plt.show()
Histogram:-
import matplotlib.pyplot as plt
population_ages=
[22,33,44,33,44,55,34,64,54,66,77,55,34,54,64,
64,34]
bins=[0,1,20,30,40,50,60,70,80]
plt.hist(population_ages,bins,histtype='bar',rwi
dth=0.8)
plt.xlabel('x')
plt.ylabel('y')
plt.title('Histogram')
plt.legend()
plt.show()
Scatter Plot:-
Example:-
import matplotlib.pyplot as plt
x=[2,1,3,5,6,4,6,7]
y=[4,3,5,6,8,9,8,5]
plt.scatter(x,y,label='skitscat',color='k',s=25,m
arker="0")
plt.xlabel('x')
plt.ylabel('y')
plt.title('Scatter Plot')
plt.legend()
plt.show()
Stack Plot(Area Graph):-
import matplotlib.pyplot as plt
days=[1,2,3,4,5]
sleeping=[7,8,8,4,3]
eating=[4,3,5,2,6]
working=[4,6,3,1,4]
playing=[4,6,3,7,8]
plt.plot([],
[],color='m',label='Sleeping',linewidth=5)
plt.plot([],
[],color='c',label='Eating',linewidth=5)
plt.plot([],
[],color='c',label='Working',linewidth=5)
plt.plot([],
[],color='c',label='Playing',linewidth=5)
plt.stackplot(days,sleeping,eating,working,playi
ng,colors=['m','c','r','k']
plt.xlabel('x')
plt.ylabel('y')
plt.title('Area Plot')
plt.legend()
plt.show()
Pie Chart:-
import matplotlib.pyplot as plt
slices=[7,2,2,13]
activities=['Sleeping','Eating','Working','Playin
g']
cols=['c','m','r','b']
plt.pie(slices,labels=activities,colors=cols,start
angle=90,shadow=True,explode=(0,0.1,0,0),au
topct='%1.1f%%)
plt.title('Pie Plot')
plt.show()
Working With Multiple Plots:-
import numpy as np
import matplotlib.pyplot as plt
def f(t):
return np.exp(-t)*np.cos(2*np.pi*t)
t1=np.arange(0.0,5.0,0.1)
t2=np.arange(0.0,5.0,0.02)
plt.subplot(211)
plt.plot(t1,f(t1),'bo',t2,f(t2))
plt.subplot(212)
plt.plot(t2,np.cos(2*np.pi*t2))
plt.show()