Thursday , December 2 2021
Home / Data Science / Sub Plot using Matplotlib

Sub Plot using Matplotlib

8 Subplot

Plot a Sub Plot in Matplotlib

With the subplots() function you can draw multiple plots in one figure:

  • The subplots() function takes three arguments that describes the layout of the figure.
  • The layout is organized in rows and columns, which are represented by the first and second argument.
  • The third argument represents the index of the current plot.
In [6]:
import numpy as np
x = np.arange(10)
y = [78,55,33,90,87,54,34,12,46,78]
y = np.array(y)
In [7]:
import matplotlib.pyplot as plt
plt.rc('figure',figsize=(18,10))
In [8]:
plt.subplot(2,2,1)
plt.title('Plot 1')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.plot(x,y,color='r')

plt.subplot(2,2,2)
plt.title('Plot 2')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.plot(x,y**2,color='k')

plt.subplot(2,2,3)
plt.title('Plot 1')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.plot(x,y**3,color='b')

plt.subplot(2,2,4)
plt.title('Plot 1')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.plot(x,y,color='y')

plt.subplots_adjust(top=0.92,bottom=0.08,left=0.10,right=0.90,hspace=0.25,wspace=0.15)
plt.show()

Subplot Gridspec

In [9]:
import matplotlib.pyplot as plt
from matplotlib import gridspec

fig = plt.figure()
gs = gridspec.GridSpec(3,3)

plt1 = fig.add_subplot(gs[0,:])
plt1.plot([1,2,3,4,5],[77,5,44,22,11],'r--')
plt1.set_title('Plot 1')
plt1.set_xlabel('X axis')
plt1.set_ylabel('Y axis')

plt2 = fig.add_subplot(gs[1,:-1])
plt2.plot([1,2,3,4,5],[99,66,5,44,22],'k--')
plt2.set_title('Plot 1')
plt2.set_xlabel('X axis')
plt2.set_ylabel('Y axis')

plt3 = fig.add_subplot(gs[1:,2])
plt3.plot([1,2,3,4,5],[99,66,5,44,22],)
plt3.set_title('Plot 1')
plt3.set_xlabel('X axis')
plt3.set_ylabel('Y axis')

plt4 = fig.add_subplot(gs[2,0])
plt4.plot([1,2,3,4,5],[99,66,5,44,22],)
plt4.set_title('Plot 1')
plt4.set_xlabel('X axis')
plt4.set_ylabel('Y axis')

plt5 = fig.add_subplot(gs[2,1])
plt5.plot([1,2,3,4,5],[99,66,5,44,22])
plt5.set_title('Plot 1')
plt5.set_xlabel('X axis')
plt5.set_ylabel('Y axis')

gs.update(wspace=0.5,hspace=0.5)

About Machine Learning

Check Also

Scatter Plot using Matplotlib

5. Scatter Plot Plot Scatter Plot Using Matplotlib¶With Pyplot, you can use the scatter() function …

Leave a Reply

Your email address will not be published. Required fields are marked *