Multiplt Plots into One Figure

There are many ways to do, we will see two methods -
a. Predefined No of Subplots
b. Adding No of Subplots

In [2]:
# import
import matplotlib.pyplot as plt
import numpy as np

%matplotlib inline
In [60]:
# generating some data points
X = np.linspace(-np.pi, np.pi, 35, endpoint=True)
C, S= np.cos(X), np.sin(X)

Predefined No of Subplots = 1 row, 3 plot

In [64]:
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')
Out[64]:
Text(0,0.5,'MSin')
<matplotlib.figure.Figure at 0x15f133f4f60>

Adding no of plots

In [65]:
plt.clf()  # clear the plot
<matplotlib.figure.Figure at 0x15f133611d0>
In [77]:
fig = plt.figure()

ax=fig.add_subplot(131)
ax.plot(X, C, '.')

ax=fig.add_subplot(133)
ax.plot(X, S)
Out[77]:
[<matplotlib.lines.Line2D at 0x15f137e7518>]

Using this way you can allot different size to plots

In [78]:
plt.clf()
<matplotlib.figure.Figure at 0x15f13544a90>

Chossing Plotting CoOrdinates

In [96]:
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)
Out[96]:
[<matplotlib.lines.Line2D at 0x15f159a50b8>]

Another Example

In [97]:
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)
Out[97]:
[<matplotlib.lines.Line2D at 0x15f15a77ba8>]
<matplotlib.figure.Figure at 0x15f159a5080>

Adding diffrent plots into one subplot

In [137]:
#plt.clf()
plt.plot(X, C)
plt.show()
In [138]:
plt.plot(X, S)
plt.show()
In [134]:
plt.plot(X, S*0.3)
Out[134]:
[<matplotlib.lines.Line2D at 0x15f0eb6fcf8>]
In [121]:
20.8 % 10
Out[121]:
0.8000000000000007
In [130]:
a = [1,2,3,4]
b = [4,7,3,4]
list(zip(a,b))
Out[130]:
[(1, 4), (2, 7), (3, 3), (4, 4)]
In [131]:
a == b
Out[131]:
False