Initiating subplots as axes with plt.subplot()

To initiate the axes plot instances that actually frame each plot, we can use plt.subplot(). It takes three parameters: number of rows, number of columns, and plot number. When the total number of plots is less than 10, we can omit the delimiting commas in the input arguments. Here is a code snippet example:

import matplotlib.pyplot as plt
# Initiates a figure area for plotting
fig = plt.figure()

# Initiates six subplot axes
ax1 = plt.subplot(231)
ax2 = plt.subplot(232)
ax3 = plt.subplot(233)
ax4 = plt.subplot(234)
ax5 = plt.subplot(235)
ax6 = plt.subplot(236)

# Print the type of ax1
print(type(ax1))

# Label each subplot with corresponding identities
ax1.text(0.3,0.5,'231',fontsize=18)
ax2.text(0.3,0.5,'232',fontsize=18)
ax3.text(0.3,0.5,'233',fontsize=18)
ax4.text(0.3,0.5,'234',fontsize=18)
ax5.text(0.3,0.5,'234',fontsize=18)
ax6.text(0.3,0.5,'236',fontsize=18)

plt.show()

The preceding code generates the following figure. Note how the subplots are ordered from left to right, top to bottom. When adding actual plot elements, it is essential to place them accordingly:

Also note that printing the type of one of the axes returns <class 'matplotlib.axes._subplots.AxesSubplot'> as a result.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.0.127