How to use subplots in matplotlib DataVisualization using python – part 2

I have already written about using subplots using matplot lib in this article. While working further on matplotlib, I found more convenient and easy to understand way for doing the same thing hence I decide to put part of of this port.

Here we are going to use subplot2grid() . It is a helper function that is similar to subplot() but uses 0-based indexing and let subplot to occupy multiple cells. The grid is specified by shape, at location of loc, spanning rowspan, colspan cells in each direction.

subplot2grid(shape, loc, rowspan=1, colspan=1,sharex=ax1)

Let us get our hands dirty. Below is simple code to plot three charts on the same plot.

################################################################################################
# name: subplot-11.py
# desc: Subplot using matplotlib
# date: 2018-12-24
# Author: conquistadorjd
################################################################################################
import matplotlib.pyplot as plt
import numpy as np
print('*** Program Started ***')
t= np.arange(1,17,1)
y1= np.random.randint(1, 16,size=16)
y2= np.random.randint(1, 32,size=16)
y3= np.random.randint(1, 48,size=16)
ax1 = plt.subplot2grid((3,3), (0,0), colspan=3, rowspan=1)
plt.plot(t, y1)
# share x only
ax2 = plt.subplot2grid((3,3), (1,0), colspan=3,rowspan=1,sharex=ax1)
plt.plot(t, y2)
# share x and y
ax3 = plt.subplot2grid((3,3), (2, 0), colspan=3,rowspan=1,sharex=ax1)
plt.plot(t, y3)
# Saving image
plt.savefig('subplot-11.png')
# In case you dont want to save image but just displya it
plt.show()
print('*** Program ended ***')

view raw
subplot-11.py
hosted with ❤ by GitHub

Here is the output

Here a bit complex subplots

################################################################################################
# name: subplot-12.py
# desc: Subplot using matplotlib
# date: 2018-12-24
# Author: conquistadorjd
################################################################################################
import matplotlib.pyplot as plt
import numpy as np
print('*** Program Started ***')
t= np.arange(1,17,1)
t2= np.arange(1,49,1)
y1= np.random.randint(1, 16,size=16)
y2= np.random.randint(1, 32,size=48)
y3= np.random.randint(1, 48,size=16)
y4= np.random.randint(1, 64,size=16)
y5= np.random.randint(1, 124,size=16)
ax1 = plt.subplot2grid((5,3), (0,0), colspan=3, rowspan=1)
plt.plot(t, y1)
ax2 = plt.subplot2grid((5,3), (1,0), colspan=2,rowspan=1)
plt.plot(t2, y2)
ax3 = plt.subplot2grid((5,3), (1, 2), colspan=2,rowspan=2,sharex=ax1)
plt.plot(t, y3)
ax4 = plt.subplot2grid((5,3), (2, 0), colspan=2,rowspan=1)
plt.plot(t, y4)
ax5 = plt.subplot2grid((5,3), (3, 0), colspan=3,rowspan=2,sharex=ax1)
plt.plot(t*2, y5)
# ax6 = plt.subplot2grid((5,3), (4, 0), colspan=3,rowspan=1,sharex=ax1)
# plt.plot(t, y3)
# Saving image
plt.savefig('subplot-12.png')
# In case you dont want to save image but just displya it
plt.show()
print('*** Program ended ***')

view raw
subplot-12.py
hosted with ❤ by GitHub

Here is the output

 

Code for generating plot is as below

ax5 = plt.subplot2grid((5,3), (3, 0), colspan=3,rowspan=2,sharex=ax1)
  • First bracket (5,3) indicates the number of rows and columns on the graph
  • second bracket (3,0) indicates location by row, column. Since this is zero based indexing, (0,0) indicates first row and first column
  • Plot can cover multiple rows and columns and can be specided by using colspan and rowspan
  • Axis can be shared between two plots and its indicated by sharex

 

 

 

 

 

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.