The Algorithms logo
The Algorithms
AboutDonate

Matplotlib

import matplotlib as mat
print(mat.__version__)
3.0.3
import numpy as np
import matplotlib 
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

x = np.linspace(0,5,10)
# x = np.sin(x);
y = x**2



plt.figure(figsize= (8,6))

plt.plot(x,y,'g')
plt.xlabel("x axis")
plt.ylabel("y axis")

plt.title(r"plot of $y = X^2$")
plt.show()
x = np.linspace(-np.pi , np.pi ,256, endpoint = True)
c ,s = np.cos(x) , np.sin(x)
plt.plot(x,c)
plt.plot(x,s)

plt.show()
plt.figure(figsize= (8,6) , dpi = 100)
plt.subplot(111)
plt.plot(x,c, color = "purple" , linewidth = 1.0 ,linestyle = "-")

plt.plot(x,s, color = "green" , linewidth = 1.0 ,linestyle = "-")

plt.xlim(-4.0,+4.0)
plt.ylim(-1.0,+1.0)
plt.grid(True)

plt.savefig("./sine.pdf" , format= "pdf", dpi = 72)
plt.show()
plt.figure(figsize= (8,6) , dpi = 100)
plt.subplot(111)
plt.plot(x,c, color = "purple" , linewidth = 1.0 ,linestyle = "-")

plt.plot(x,s, color = "green" , linewidth = 1.0 ,linestyle = "-")

plt.xlim(x.min()*1.1 , x.max()*1.1)
plt.ylim(c.min()*1.1 , c.max()*1.1)
plt.grid(True)

plt.savefig("./sine.pdf" , format= "pdf", dpi = 72)
plt.show()
plt.figure(figsize= (8,6) , dpi = 100)
plt.subplot(111)
plt.plot(x,c, color = "purple" , linewidth = 1.0 ,linestyle = "-")

plt.plot(x,s, color = "green" , linewidth = 1.0 ,linestyle = "-")

plt.xlim(x.min()*1.1 , x.max()*1.1)
plt.ylim(c.min()*1.1 , c.max()*1.1)

plt.xticks([-np.pi , -np.pi/2,0,np.pi , np.pi/2])
plt.yticks([-1 , 0, +1])
plt.grid(True)

plt.savefig("./sine.pdf" , format= "pdf", dpi = 72)
plt.show()
plt.figure(figsize= (8,6) , dpi = 100)
plt.subplot(111)
plt.plot(x,c, color = "purple" , linewidth = 1.0 ,linestyle = "-" , label = "cosine")

plt.plot(x,s, color = "green" , linewidth = 1.0 ,linestyle = "-" , label = "sine")

plt.xlim(x.min()*1.1 , x.max()*1.1)
plt.ylim(c.min()*1.1 , c.max()*1.1)

plt.xticks([-np.pi , -np.pi/2,0,np.pi , np.pi/2] , [r'$-\pi$',r'$-\pi/2$',r'$0$',r'$+\pi$',r'$+\pi/2$'])
plt.yticks([-1 , 0, +1] , [r'$-1$',r'$0$',r'$+1$'])
plt.grid(True)

plt.legend(loc = 'upper left' , frameon= True)
plt.savefig("./sine.pdf" , format= "pdf", dpi = 72)
plt.show()
x = np.linspace(0,5,10)
y = x**2

fig ,axes = plt.subplots(1,2,figsize= (8,4), dpi = 100)
axes[0].plot(x , x**2 , color = "green" , label = "y = x**2")
axes[0].plot(x , x**3 , color = "green" , label = 'y = x**3')
axes[0].set_xlabel('x')
axes[0].set_ylabel('y')
axes[0].set_title("plot of y = x^2 and y = x^3")


axes[1].plot(x , x**2 , color = "red" , label = "y = x**2")
axes[1].plot(x , y**3 , color = "red" , label = 'y = x**3')
axes[1].set_xlabel('x')
axes[1].set_ylabel('y')
axes[1].set_title("plot of y = x^2 and y = x^3")
Text(0.5, 1.0, 'plot of y = x^2 and y = x^3')
#ploting 4 plots 

x = np.linspace(0,5,10)
y = x**2

fig ,axes = plt.subplots(2,2,figsize= (11,9), dpi = 100)
axes[0,0].plot(x , x**2 , color = "green" , label = "y = x**2")
axes[0,0].plot(x , x**3 , color = "green" , label = 'y = x**3')
axes[0,0].set_xlabel('x')
axes[0,0].set_ylabel('y')
axes[0,0].set_title("plot of y = x^2 and y = x^3")


axes[0,1].plot(x , x**2 , color = "red" , label = "y = x**2")
axes[0,1].plot(x , y**3 , color = "red" , label = 'y = x**3')
axes[0,1].set_xlabel('x')
axes[0,1].set_ylabel('y')
axes[0,1].set_title("plot of y = x^2 and y = x^3")

axes[1,0].plot(x , x**2 , color = "red" , label = "y = x**2")
axes[1,0].plot(x , y**3 , color = "red" , label = 'y = x**3')
axes[1,0].set_xlabel('x')
axes[1,0].set_ylabel('y')
axes[1,0].set_title("plot of y = x^2 and y = x^3")

axes[1,1].plot(x , x**2 , color = "red" , label = "y = x**2")
axes[1,1].plot(x , y**3 , color = "red" , label = 'y = x**3')
axes[1,1].set_xlabel('x')
axes[1,1].set_ylabel('y')
axes[1,1].set_title("plot of y = x^2 and y = x^3")
Text(0.5, 1.0, 'plot of y = x^2 and y = x^3')

import numpy as np
import matplotlib.pyplot as plt


x1 = np.linspace(0.0, 5.0)
x2 = np.linspace(0.0, 2.0)

y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
y2 = np.cos(2 * np.pi * x2)

plt.subplot(2, 1, 1)  # plot two graph
plt.plot(x1, y1, 'o-')
plt.title('A tale of 2 subplots')
plt.ylabel('Damped oscillation')

plt.subplot(2, 1, 2)
plt.plot(x2, y2, '.-')
plt.xlabel('time (s)')
plt.ylabel('Undamped')

plt.show()

Histogram

n = np.random.randn(100000)
fig,axes = plt.subplots(1,2, figsize= (12,6))

axes[0].hist(n , color = "blue")
axes[0].set_title("deafault histogram")
axes[0].set_xlim(np.min(n),np.max(n))

axes[1].hist(n , cumulative = True ,bins= 50 ,color = "red")
axes[1].set_title("cumalative histogram")
axes[1].set_xlim(np.min(n),np.max(n))

fig.tight_layout()
plt.show()