Subplots Part 2
Subplots Part 2¶
These same things apply when we are actually using two columns instead of two rows.
#If we choose ncols instead of nrows, we will have two columns that work in a similar manner
fig, ax = plt.subplots(ncols=2, figsize=(12,5), sharex=True, sharey=True)
ax[0].plot(X1, Y1)
ax[0].set_xlabel("Series 1 X")
ax[0].set_ylabel("Series 1 Y")
ax[0].set_title("Series 1")
ax[1].plot(X2, Y2)
ax[1].set_xlabel("Series 2 X")
ax[1].set_ylabel("Series 2 Y")
ax[1].set_title("Series 2")
plt.show()
Let’s expand into multiple columns and multiple rows. When we set rows and columns equal to 2, we get back an ax object which has two dimensions, the first being rows and the second being columns. If we make four sets of data, we can plot them in each corner like so:
#Create more new data
X1 = list(range(0,101))
X2 = list(range(50,101))
Y1 = [10 * x for x in X1]
Y2 = [(x-50) ** 2 for x in X2]
X3 = [x*2 for x in X1]
Y3 = [10 * x for x in X3]
X4 = [x*2 for x in X2]
Y4 = [(x-50) ** 2 for x in X4]
#If we use both nrows and ncols then we get a grid
fig, ax = plt.subplots(ncols=2, nrows=2, figsize=(12,12), sharex=True, sharey=True)
#Now we index on two dimensions
ax[0,0].plot(X1, Y1)
ax[0,0].set_xlabel("Series 1 X")
ax[0,0].set_ylabel("Series 1 Y")
ax[0,0].set_title("Series 1")
ax[1,0].plot(X2, Y2)
ax[1,0].set_xlabel("Series 2 X")
ax[1,0].set_ylabel("Series 2 Y")
ax[1,0].set_title("Series 2")
ax[0,1].plot(X3, Y3)
ax[0,1].set_xlabel("Series 3 X")
ax[0,1].set_ylabel("Series 3 Y")
ax[0,1].set_title("Series 3")
ax[1,1].plot(X4, Y4)
ax[1,1].set_xlabel("Series 4 X")
ax[1,1].set_ylabel("Series 4 Y")
ax[1,1].set_title("Series 4")
plt.show()
A modification to the axis sharing is that instead of having all plots share the same x and y axis, we can specify that only the rows should share or that only the columns should share. For example, the first code will have only the rows sharing and the second will have only the columns sharing.
#There is an option to use row or column for subplots so that sharex or sharey is only made for sharing with each row/column
#Sharing with the row the x and y axis
fig, ax = plt.subplots(ncols=2, nrows=2, figsize=(12,12), sharex='row', sharey='row')
ax[0,0].plot(X1, Y1)
ax[0,0].set_xlabel("Series 1 X")
ax[0,0].set_ylabel("Series 1 Y")
ax[0,0].set_title("Series 1")
ax[1,0].plot(X2, Y2)
ax[1,0].set_xlabel("Series 2 X")
ax[1,0].set_ylabel("Series 2 Y")
ax[1,0].set_title("Series 2")
ax[0,1].plot(X3, Y3)
ax[0,1].set_xlabel("Series 3 X")
ax[0,1].set_ylabel("Series 3 Y")
ax[0,1].set_title("Series 3")
ax[1,1].plot(X4, Y4)
ax[1,1].set_xlabel("Series 4 X")
ax[1,1].set_ylabel("Series 4 Y")
ax[1,1].set_title("Series 4")
plt.show()
#Sharing with the columns the x and y axis
fig, ax = plt.subplots(ncols=2, nrows=2, figsize=(12,12), sharex='col', sharey='col')
ax[0,0].plot(X1, Y1)
ax[0,0].set_xlabel("Series 1 X")
ax[0,0].set_ylabel("Series 1 Y")
ax[0,0].set_title("Series 1")
ax[1,0].plot(X2, Y2)
ax[1,0].set_xlabel("Series 2 X")
ax[1,0].set_ylabel("Series 2 Y")
ax[1,0].set_title("Series 2")
ax[0,1].plot(X3, Y3)
ax[0,1].set_xlabel("Series 3 X")
ax[0,1].set_ylabel("Series 3 Y")
ax[0,1].set_title("Series 3")
ax[1,1].plot(X4, Y4)
ax[1,1].set_xlabel("Series 4 X")
ax[1,1].set_ylabel("Series 4 Y")
ax[1,1].set_title("Series 4")
plt.show()
The final part of this lesson focuses on gridspec. Using gridspec does a similar thing except now we are able to actually have plots of different sizes. The gridspec is called with the number of rows and columns first. Then, we can add subplots on the gridspec by add_subplot on the figure and then passing the gridspec locations (in terms of the array). From the code below you will see how the format works.
#An alternative that allows you to pick different sizes for your subplots is gridspec
fig = plt.figure(constrained_layout=True, figsize=(8,8))
#Add a grid spec with 3 rows and 2 columns
gs = fig.add_gridspec(3, 2)
#Add a plot at row 0, and for all columns
ax1 = fig.add_subplot(gs[0, :])
#Add a plot in column 1, taking up spots in the second and third row
ax2 = fig.add_subplot(gs[1:3, 0])
#Add a plot in row 3, column 2
ax3 = fig.add_subplot(gs[2, 1])
plt.show()
Finally, fill in the plots!
#And then you can plot on the axes in the same way
fig = plt.figure(constrained_layout=True, figsize=(8,8))
gs = fig.add_gridspec(3, 2)
ax1 = fig.add_subplot(gs[0, :])
ax2 = fig.add_subplot(gs[1:3, 0])
ax3 = fig.add_subplot(gs[2, 1])
ax1.plot(X1, Y1)
ax1.set_xlabel("Series 1 X")
ax1.set_ylabel("Series 1 Y")
ax1.set_title("Series 1")
ax2.plot(X2, Y2)
ax2.set_xlabel("Series 2 X")
ax2.set_ylabel("Series 2 Y")
ax2.set_title("Series 2")
ax3.plot(X3, Y3)
ax3.set_xlabel("Series 3 X")
ax3.set_ylabel("Series 3 Y")
ax3.set_title("Series 3")
plt.show()