Matplotlib Tutorials

How to Plot Multiple Bars in Single Bar Graph in Matplotlib 0

How to Plot Multiple Bars in Single Bar Graph in Matplotlib

Program 1 import matplotlib.pyplot as mat years=[2019,2020,2021,2022,2023] frooti=[300,500,200,600,400] pepsi=[300,200,400,500,350] limca=[400,200,300,600,500] wd=0.2 f_bar=[0,1,2,3,4] #print(f_bar) p_bar=[i+wd for i in f_bar] l_bar=(i+wd for i in p_bar) #print(p_bar) mat.bar(f_bar,frooti,width=wd,color=’red’,label=”Frooti”) mat.bar(p_bar,pepsi,width=wd,color=’blue’,label=”Pepsi”) mat.bar(l_bar,limca,width=wd,color=’yellow’,label=”Limca”) mat.title(“Produt Details(5 Year)”) mat.legend() mat.show()  

xticks(), yticks(), xlabel(), ylabel(), xlim(), ylim() Methods in Matplotlib 0

xticks(), yticks(), xlabel(), ylabel(), xlim(), ylim() Methods in Matplotlib

Program 1 import matplotlib.pyplot as mat x=[0,10,20,30,40,50] y=[0,100,200,300,400,500] mat.plot(x,y) mat.xlabel(“Product”) mat.ylabel(“Price”) mat.title(“Product Range Graph”) # mat.xticks(x,[‘A’,’B’,’C’,’D’,’E’,’F’]) # mat.yticks(y,[‘0-10′,’10-20′,’20-30′,’30-40′,’40-50′,’50-60’]) mat.xlim([0,100]) mat.ylim([0,1000]) mat.show()  

How to Plot Histogram in Matplotlib 0

How to Plot Histogram in Matplotlib

Program 1 import matplotlib.pyplot as mat prod_price=[0,10,25,30,70,100,150,400,350,500,700] prod_range=[0,30,100,150,300,500,800] mat.hist(prod_price,prod_range,rwidth=0.3,histtype=”bar”,facecolor=’r’) mat.xticks([0,30,50,100,300,500,800]) mat.title(“Product Price Range”) mat.xlabel(“Product Price”) mat.ylabel(“Product Range”) mat.show()  

How to Create Vertical and Horizontal Bar Graph in Matplotlib 0

How to Create Vertical and Horizontal Bar Graph in Matplotlib

Program 1 import matplotlib.pyplot as mat x=[2018,2019,2020,2021,2022] y=[4000,3000,6000,5000,1500] mycolor=[‘red’,’green’,’black’,’blue’,’yellow’] mat.bar(x,y,width=0.2,color=mycolor) #mat.barh(x,y,height=0.2,color=mycolor) mat.title(“Product Details(5 Year)”) mat.xlabel(“Year”) mat.ylabel(“Frooti (Product”) mat.show()  

Python Matplotlib Program for Pie Chart 0

Python Matplotlib Program for Pie Chart

Program 1 import matplotlib.pyplot as mat st_result=[“I Divsion”,”II Divsion”,”III Divsion”,”Supp”,”Fail”] st_values=[250,170,50,40,24] mat.figure(figsize=(8,11)) mat.pie(st_values,labels=st_result,startangle=0,explode=[0.2,0.1,0.1,0.1,0.1],colors=[‘red’,’green’,’blue’,’yellow’,’black’],shadow=True,autopct=”%2.1f%%”) mat.legend() mat.title(“Student Result”) mat.show()  

Cmap and ColorBar in Scatter Plot using Matplotlib 0

Cmap and ColorBar in Scatter Plot using Matplotlib

Program 1 import matplotlib.pyplot as mat x=[0,1,2,3,4,5] y=[0,1,4,9,16,25] #mycolor=[‘red’,’blue’,’green’,’black’,’blue’,’yellow’] mycolor=[10,20,30,40,50,60] mysize=[100,250,150,200,300,250] mat.scatter(x,y,c=mycolor,s=mysize,cmap=”Accent”) mat.colorbar() mat.show()  

Python Matplotlib Program for Scatter Plot 0

Python Matplotlib Program for Scatter Plot

Program 1 import matplotlib.pyplot as mat x=[0,1,2,3,4,5] y=[i*2 for i in x] y1=[i**2 for i in x] y2=[i**3 for i in x] mat.scatter(x,y,marker=’s’,label=”Sales”,s=20,linewidth=4,color=’red’,alpha=1) mat.scatter(x,y1,marker=’o’,label=”Product”,s=20,linewidth=4,color=’blue’,alpha=1) mat.scatter(x,y2,marker=’o’,label=”Company”,s=20,linewidth=4,color=’yellow’,alpha=1) mat.legend() mat.show()  

Python Matplotlib Program for Subplot 0

Python Matplotlib Program for Subplot

Program 1 import matplotlib.pyplot as mat x=[0,1,2,3,4,5] y=[0,1,4,9,16,25] x1=[0,10,20,30,40,50] y1=[0,20,10,40,10,50] x2=[0,10,20,30,40,50] y2=[0,20,10,40,10,50] x3=[0,10,20,30,40,50] y4=[0,20,10,40,10,50] mat.subplot(3,2,1) mat.plot(x,y,marker=’.’,mfc=’r’,mec=’k’,ms=10) mat.subplot(3,2,2) mat.plot(x1,y1,marker=’.’,mfc=’y’,mec=’r’,ms=10) mat.subplot(3,2,3) mat.plot(x2,y1,marker=’.’,mfc=’y’,mec=’r’,ms=10) mat.subplot(3,2,4) mat.plot(x3,y4,marker=’.’,mfc=’y’,mec=’r’,ms=10) mat.title(“Line Chart of SubPlot”) mat.xlabel(“X-Axis”) mat.ylabel(“Y-Axis”) mat.show()  

Python Matplotlib Program for Marker 0

Python Matplotlib Program for Marker

Program 1 import matplotlib.pyplot as mat x=[0,1,2,3,4,5] y=[0,1,2,3,4,5] mat.plot(x,y) mat.xlabel(“X-Axis”) mat.ylabel(“Y-Axis”) mat.title(“Line Chart”) mat.show() Program 2 import matplotlib.pyplot as mat x=[0,1,2,3,4,5] y=[0,1,2,3,4,5] mat.plot(x,y,marker=’h’,mec=’y’,mfc=’g’,ms=15) mat.xlabel(“X-Axis”) mat.ylabel(“Y-Axis”) mat.title(“Marker Example”) mat.show()  

Matplotlib Program for Legend Function 0

Matplotlib Program for Legend Function

Program 1 import matplotlib.pyplot as mat x=[0,1,2,3,4,5] y=[0,1,4,9,16,25] y1=[0,10,20,30,40,50] y2=[0,20,10,40,30,50] #mat.plot(x,y,x,y1) # mat.legend([‘Sales’,’Product’]) mat.plot(x,y,label=”Product”,marker=”.”,mfc=’r’,mec=’k’) mat.plot(x,y1,label=”Sales”,marker=”*”,mfc=’y’,mec=’k’) mat.plot(x,y2,label=”Company”,marker=”.”,mfc=’c’,mec=’k’) mat.legend(loc=2,facecolor=’cyan’,edgecolor=’red’,framealpha=0.5,shadow=True,fancybox=True) mat.show()