Matplotlib is a popular Python plotting library that plays a major role in subjects like data science, machine learning, etc. Visualization gives us access to huge amounts of data in easily digestible visuals.
pip install matplotlib
import matplotlib.pyplot as plt`
# importing required librariesimport numpy as npimport pandas as pdimport matplotlib.pyplot as plt# Generation of variablesx=np.arange(0,10) #Array of range 0 to 9y=x**3# Printing the variablesprint(x)print(y)plt.plot(x,y) # Function to plotplt.title('Line Chart') # Function to give title# Functions to give x and y labelsplt.xlabel('X-Axis')plt.ylabel('Y-Axis')# Functionn to show the graphplt.show()
# importing required librariesimport numpy as npimport pandas as pdimport matplotlib.pyplot as plt# Generation of 1 set of variablesx = np.arange(0,11)y = x**3# Generation of 1 set of variablesx2 = np.arange(0,11)y2 = (x**3)/2# Printing all variablesprint(x,y,x2,y2,sep="\n")# "linewidth" is used to specify the width of the lines# "color" is used to specify the colour of the lines# "label"is used to specify the name of axes to represent in the lengendplt.plot(x,y,color='r',label='first data', linewidth=5)plt.plot(x2,y2,color='y',linewidth=5,label='second data')plt.title('Multiline Chart')# Uses the label attribute to display reference in legendplt.ylabel('Y axis')plt.xlabel('X axis')# Shows the legend in the best postion with respect to the graphplt.legend()plt.show()
# Importing required librariesimport numpy as npimport pandas as pdimport matplotlib.pyplot as plt# Generation of variablesx = ["India",'USA',"Japan",'Australia','Italy']y = [6,7,8,9,2]# Printing the variablesprint(x)print(y)plt.bar(x,y, label='Bars1', color ='r') # Function to plot# Function to give x and y labelsplt.xlabel("Country")plt.ylabel("Inflation Rate%")# Function to give heading of the chartplt.title("Bar Graph")# Function to show the chartplt.show()
# importing required librariesimport numpy as npimport pandas as pdimport matplotlib.pyplot as plt# Generation of 1 set of variablesx = ["India",'USA',"Japan",'Australia','Italy']y = [6,7,8,9,5]# Generation of 2 set of variablesx2 = ["India",'USA',"Japan",'Australia','Italy']y2 = [5,1,3,4,2]# Printing all variablesprint(x,y,x2,y2,sep="\n")# Functions to plotplt.bar(x,y, label='Inflation', color ='y')plt.bar(x2,y2, label='Growth', color ='g')# Functions to give x and y labelsplt.xlabel("Country")plt.ylabel("Inflation & Growth Rate%")plt.title("Multiple Bar Graph")plt.legend()plt.show()
import numpy as npimport pandas as pdimport matplotlib.pyplot as plt# Generation of variablestock_prices = [32,67,43,56,45,43,42,46,48,53,73,55,54,56,43,55,54,20,33,65,62,51,79,31,27]# Function to show the chartplt.figure(figsize = (8,5))plt.hist(stock_prices, bins = 5)
# Importing required librariesimport numpy as npimport pandas as pdimport matplotlib.pyplot as plt# Generation of x and y variablesx = [1,2,3,4,5,6,7,8]y = [5,2,4,2,1,4,5,2]# Function to plot the graphplt.scatter(x,y)plt.xlabel('x')plt.ylabel('y')plt.title('Scatter Plot')
# Importing required librariesimport numpy as npimport pandas as pdimport matplotlib.pyplot as plt# Collection of raw dataraw_data={'names':['Nick','Sani','John','Rubi','Maya'],'jan_score':[123,124,125,126,128],'feb_score':[23,24,25,27,29],'march_score':[3,5,7,6,9]}# Segregating the raw data into usuable form/variablesdf=pd.DataFrame(raw_data,columns=['names','jan_score','feb_score','march_score'])df['total_score']=df['jan_score']+df['feb_score']+df['march_score']# Printing the dataprint(df)# Function to plot the graphplt.pie(df['total_score'],labels=df['names'],autopct='%.2f%%')plt.axis('equal')plt.axis('equal')plt.show()
# Importing required librariesimport numpy as npimport pandas as pdimport matplotlib.pyplot as plt# Defining the sixe og the figuresplt.figure(figsize=(10,10))# Generation of variablesx = np.array([1,2,3,4,5,6,7,8])y = np.array([5,2,4,2,1,4,5,2])# Generating 4 subplots in form of 2x2 matrix# In the line below the arguments of plt.subplot are as follows:# 2- no. of rows# 2- no. of columns# 1- position in matrix# Position (0,0)plt.subplot(2,2,1)plt.plot(x,y,'g')plt.title('Sub Plot 1')plt.xlabel('X-Axis')plt.ylabel('Y-Axis')# Position (0,1)plt.subplot(2,2,2)plt.plot(y,x,'b')plt.title('Sub Plot 2')plt.xlabel('X-Axis')plt.ylabel('Y-Axis')# Position (1,0)plt.subplot(2,2,3)plt.plot(y*2,x*2,'y')plt.title('Sub Plot 3')plt.xlabel('X-Axis')plt.ylabel('Y-Axis')# Position (1,1)plt.subplot(2,2,4)plt.plot(x*2,y*2,'m')plt.title('Sub Plot 4')plt.xlabel('X-Axis')plt.ylabel('Y-Axis')# Function for layout and spacingplt.tight_layout(h_pad=5, w_pad=10)