There are many ways to do, we will see two methods -
a. Predefined No of Subplots
b. Adding No of Subplots
# import
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
# generating some data points
X = np.linspace(-np.pi, np.pi, 35, endpoint=True)
C, S= np.cos(X), np.sin(X)
fig = plt.figure()
f, axes = plt.subplots(1, 3) # 1 row and 3 columns
# Using subplot
axes[0].plot(X, C, '.')
axes[0].set_ylabel('Cos')
axes[1].plot(X, S, '-')
axes[1].set_ylabel('Sin')
axes[2].plot(X, S+0.1, 'o')
axes[2].set_ylabel('MSin')
Text(0,0.5,'MSin')
<matplotlib.figure.Figure at 0x15f133f4f60>
plt.clf() # clear the plot
<matplotlib.figure.Figure at 0x15f133611d0>
fig = plt.figure()
ax=fig.add_subplot(131)
ax.plot(X, C, '.')
ax=fig.add_subplot(133)
ax.plot(X, S)
[<matplotlib.lines.Line2D at 0x15f137e7518>]
plt.clf()
<matplotlib.figure.Figure at 0x15f13544a90>
Chossing Plotting CoOrdinates
fig = plt.figure()
ax=fig.add_subplot(331) # plot coordinates - 1st of (3,3) plot grid
ax.plot(X, C, '.')
ax=fig.add_subplot(323) # plot coordinates - 3rd of (3,2) plot grid
ax.plot(X, S+0.2)
ax=fig.add_subplot(324) # plot coordinates - 4th of (3,2) plot grid
ax.plot(X, S)
ax=fig.add_subplot(338) # plot coordinates - 8th of (3,3) plot grid
ax.plot(X, S)
[<matplotlib.lines.Line2D at 0x15f159a50b8>]
Another Example
plt.clf()
fig = plt.figure()
ax=fig.add_subplot(431) # plot coordinates - 1st of (4,3) plot grid
ax.plot(X, C, '.')
ax=fig.add_subplot(423) # plot coordinates - 3rd of (4,2) plot grid
ax.plot(X, S+0.2)
ax=fig.add_subplot(424) # plot coordinates - 4th of (4,2) plot grid
ax.plot(X, S)
ax=fig.add_subplot(438) # plot coordinates - 8th of (4,3) plot grid
ax.plot(X, S)
ax=fig.add_subplot(234) # plot coordinates - 4th of (2,3) plot grid
ax.plot(X, C*0.4)
[<matplotlib.lines.Line2D at 0x15f15a77ba8>]
<matplotlib.figure.Figure at 0x15f159a5080>
Adding diffrent plots into one subplot
#plt.clf()
plt.plot(X, C)
plt.show()
plt.plot(X, S)
plt.show()
plt.plot(X, S*0.3)
[<matplotlib.lines.Line2D at 0x15f0eb6fcf8>]
20.8 % 10
0.8000000000000007
a = [1,2,3,4]
b = [4,7,3,4]
list(zip(a,b))
[(1, 4), (2, 7), (3, 3), (4, 4)]
a == b
False