Matplotlib: Visualization with Python
Matplotlib: A library for creating static,animated and interactive visualizations in python.It provides an object-oriented programming methods for embedding plots into applications using GUI toolkits such as Tkinter and wxPython.
#can be installed using below command
pip install matplotlib
Line graphs: These type of graphs are used to track the changes over the short and long period of time.
import matplotlib.pyplot as pltx=[1,2,3]
y=[2,4,6]
plt.plot(x,y)plt.figure(figsize=(5,3),dpi=100) #determining the size of the graph
(length,breadth) in inches in figsize and dpi pixel per inch #setting up the title and axis of the graph
plt.title('First Graph',fontdict={'fontsize':15}) #fontdict is optional
plt.xlabel('x-axis',fontdict={'fontsize':12})
plt.ylabel('y-axis',fontdict={'fontsize':12})
plt.show()
--(Fig.Image1)#we can also change the axis' ticks
plt.xticks([1,2,3,4])
plt.yticks([0,2,4,6,8,10])
Modifications:
plt.plot(x,y,label='legend',color='grey',linewidth=3,marker='^',markersize=8,markeredgecolor='blue',linestyle='--')
plt.legend()
plt.show()
--(Fig.Image2)
Instead of writing so many arguments, matplotlib has shorthand notation for this [color][marker][line].
plt.plot(x,y,'y^--',label=”legend”) #color:y,marker:^,line:--
plt.legend()
plt.show()
--(Fig.Image3)
plt.savefig('graph.png',dpi=300) #saves the graph in the current directory
Higher order line graphs:
x1=np.arange(0,4.5,0.5)
plt.plot(x1[:4],x1[:4]**4,'r.--')
plt.plot(x1[3:],x1[3:]**4,'y.--')
plt.show()
--(Fig.Image4)
Bar Charts: Bar charts are used when we have to show segments of information.Vertical bar charts are useful to compare different categorical or discrete variables for example: age-groups,classes etc.
labels=[‘X’,’Y’,’Z’]
values=[2,4,1]bars=plt.bar(labels,values)
bars[1].set_hatch(‘O’) # can try out *,+,/plt.show()
--(Fig.Image5)
Histograms:These graphs are used to summarize discrete or categorical variables that are measured on an interval scale.for example:inflation on scale of year etc.
x=np.random.randint(150,size=10)
plt.hist(x)
plt.show()
--(Fig.Image6)
Pie Charts: A pie chart expresses a part-to-whole realtionship in our data.
values=[4234,34324]
labels=[‘Males’,’Females’]
colors=[‘#eddcb2’,’#b2d6ed’]plt.pie(values,labels=labels,colors=colors,autopct=’%0.2f %%’)
plt.title(“Ratio of Males and Females in an area”)plt.show()
--(Fig.Image7)
We can separate each piece in pie charts using the ‘explode’ property.
values=[4234,34324,4000,3300,4100]
labels=[‘A’,’B’,’C’,’D’,’E’]
colors=[‘#eddcb2’,’#b2d6ed’,’#deedd3',’#d4a7db’,’#f2999f’]explode=[0,0,0.4,0.4,0]plt.pie(values,labels=labels,colors=colors,autopct=’%0.2f %%’,pctdistance=0.8,explode=explode) #pctdistance property maintains the distance of written %ages inside pie chartsplt.title('Market Share')
plt.show()
--(Fig.Image8)
Box Plots: These graphs are used to show distribution of numeric data values,especially when we want to compare them between multiple groups.
- Take a look at the image below to find out how box plots are simplified.
team1=[300,310,340,230,210,160,320]
team2=[400,240,300,320,187,410,370]
team3=[200,100,210,170,180,140,220]plt.figure(figsize=(5,7))
labels=[‘TEAM1’,’TEAM2',’TEAM3']
plt.boxplot([team1,team2,team3],labels=labels)plt.title(‘Team Comparision’)
plt.xlabel(‘Teams’)
plt.ylabel(‘Overall Ratings’)plt.show()
--(Fig.Image9)