Matplotlib
Matplotlib is an amazing visualization library in Python
It helps us in interpreating the data in visual form of representation(eg. plotting a graph etc.)
Python Matplotlib : Types of Plots
There are various plots which can be created using python matplotlib. Some of them are
listed below:
Install Matplotlib with pip
The python package manager pip is also used to install matplotlib. Open the command
prompt window, and type the following command:
pip install matplotlib
In [1]: import matplotlib as mp
mp.__version__
Out[1]: '3.5.2'
Basic Example of plotting Graph
In [2]: #line graph
import matplotlib.pyplot as plt
# x-axis values
x = [5, 2, 9, 4, 7]
# Y-axis values
y = [10, 5, 8, 4, 5]
# Function to plot
plt.plot(x,y)
plt.grid(True)
# function to show the plot
plt.show()
In [3]: #Bar plot
import matplotlib.pyplot as plt
x = [5, 2, 9, 4, 7] # x-axis value
y = [10, 5, 8, 4, 2] # Y-axis value
plt.bar(x,y,label="English Books", color='r',width=.2) # Function to
plt.grid(True)
plt.legend()
plt.xlabel("Days")
plt.ylabel("Nos. of books sold")
plt.title("Books sold in days")
plt.show() # function to show the plot
In [4]: import matplotlib.pyplot as plt
population_age = [22,55,62,45,21,22,34,42,42,4,2,80,75,55,55,44,43,42,48]
age_range = [0,10,20,30,40,50,60,70,80,90,100]
plt.hist(population_age, age_range, width=9) #histtype='bar'
plt.xlabel('age groups')
plt.ylabel('Number of people')
plt.title('Histogram')
plt.grid(True)
plt.show()
In [5]: import matplotlib.pyplot as plt
# Data to plot
labels = 'Python', 'C++', 'Ruby', 'Java'
sizes = [215, 130, 245, 210]
colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue']
explode = (0.1, 0.1, 0.2, 0.1) # explode 1st slice
#plt.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True
plt.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f
plt.legend()
plt.axis('equal')
plt.show()
In [6]: import numpy as np
import matplotlib.pyplot as plt
x = [1,1.5,2,2.5,3,3.5,3.6]
y = [7,8,9,10,12,15,17]
x1=np.arange(0,10,1)
#x1=np.linspace(0,10,10)
y1=[17,15,12,10,9,7,4,3,2,0]
fig=plt.figure(figsize=(10,6),dpi=80)
plt.scatter(x,y, label='high temperature high selling of AC',color='r')
plt.scatter(x1,y1,label='low temperature high selling of heater',color='b')
plt.xlabel('temperature-->')
plt.ylabel('sellings-->')
plt.title('Scatter Plot')
plt.legend()
plt.show()
print(fig)
Figure(800x480)
In [7]: import matplotlib.pyplot as plt
import numpy as np
import math #needed for definition of pi
x = np.arange(0, math.pi*2, 0.1)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = np.tan(x)
plt.figure(figsize=(10,4),dpi=80)
plt.subplot(1,3,1)
plt.plot(x,y1)
#plt.stem(x,y1) # for discrete plot
plt.xlabel("angle")
plt.ylabel("sine")
plt.title('sine wave')
plt.subplot(1,3,2)
plt.plot(x,y2)
plt.xlabel("angle")
plt.ylabel("cosine")
plt.title('cosine wave')
plt.subplot(1,3,3)
plt.plot(x,y2)
plt.xlabel("angle")
plt.ylabel("cosine")
plt.title('cosine wave')
plt.show()
In [8]: import matplotlib.pyplot as plt
import numpy as np
x=np.arange(0,10,0.2)
y1=np.sqrt(4*2*x)
y2=-1*np.sqrt(4*2*x)
ax=plt.subplot()
plt.plot(x,y1,'b--')
plt.plot(x,y2,'r-*')
ax.spines['left'].set_position(('data',0))
ax.spines['bottom'].set_position(('data',0))
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
#plt.axvline(0,color='r',linewidth=1)
plt.ylim(-3,3)
plt.xlim(-5,10)
plt.show()
Thank you
In [9]: from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np
#if using a Jupyter notebook, include:
%matplotlib notebook
x = np.arange(-5,5,0.1)
y = np.arange(-5,5,0.1)
X,Y = np.meshgrid(x,y)
Z = X*np.exp(-X**2 - Y**2)
fig = plt.figure(figsize=(6,4))
ax = fig.add_subplot(111, projection='3d')
# Plot a 3D surface
ax.plot_surface(X, Y, Z)
Figure 1
Out[9]: <mpl_toolkits.mplot3d.art3d.Poly3DCollection at 0x1a59df76e50>
In [10]: import numpy as np
import matplotlib.pyplot as plt
plt.figure() # Create a new figure window
xlist = np.linspace(-2.0, 2.0, 100) # Create 1-D arrays for x,y dimensions
ylist = np.linspace(-2.0, 2.0, 100)
X,Y = np.meshgrid(xlist, ylist) # Create 2-D grid xlist,ylist values
F = X**2 + Y**2 - 1 # 'Circle Equation
plt.contour(X, Y, F, [0], colors = 'k', linestyles = 'solid')
plt.show()
Figure 2
In [ ]: