0

Please I need help with a plot. I am making a 3x3 dimension figure containing 7 subplots. I want two(2) of the subplots (ax6 and ax7) to be stacked plots. Does anyone have an idea how I can make this work? I used the code below to make the grid.

fig = plt.figure()
fig.set_figheight(8)
fig.set_figwidth(10)
gs = gridspec.GridSpec(3, 3)
ax1 = plt.subplot(gs[0, 0])
ax2 = plt.subplot(gs[0, -2])
ax3 = plt.subplot(gs[0, -1])
ax4 = plt.subplot(gs[1, 0])
ax5 = plt.subplot(gs[-1, 0])
ax6 = plt.subplot(gs[1:, -2])
ax7 = plt.subplot(gs[1:, -1])

I tried making the stacked plot for ax6 using the code below

ax6[0].plot(s[['xa']], s[['ac1']], label = "Data")
ax6[0].plot(s[['xa']], s[['ac2']], label = "C-C")
ax6[0].plot(s[['xa']], s[['ac3']], label = "C-O")
ax6[0].plot(s[['xa']], s[['ac4']], label = "C=C")
ax6[0].plot(s[['xa']], s[['ea1']], label = "Envelope")
ax6[0].text(0.08, 0.70, 'C', ha='center', va='baseline', wrap=True, fontsize= 10, fontweight='bold', color='darkgreen', transform=ax6[0].transAxes)

ax6[1].plot(s[['xb']], s[['bc1']], label = "Data")
ax6[1].plot(s[['xb']], s[['bc2']], label = "C-C")
ax6[1].plot(s[['xb']], s[['bc3']], label = "C-O")
ax6[1].plot(s[['xb']], s[['bc4']], label = "C=C")
ax6[1].plot(s[['xb']], s[['be1']], label = "Envelope")
ax6[1].text(0.08, 0.70, 'm.C', ha='center', va='baseline', wrap=True, fontsize= 10, fontweight='bold', color='darkgreen', transform=ax6[1].transAxes)

enter image description here

  • Welcome to SO! By stacked, do you mean ax6 and ax7 subplots should share x-axis but not y-axis? – medium-dimensional Oct 31 '22 at 04:01
  • 1
    @medium-dimensional Not quite. ax6 and ax7 should be individual subplots. I said stacked because ax6 would have 2 plots that share same x-axis but not y-axis. ax7 will also have the same subplots but standalone. – olly_day Oct 31 '22 at 18:19

1 Answers1

1

Please look at the comments in the code:

import matplotlib.pyplot as plt 
from matplotlib import gridspec
import numpy as np

fig = plt.figure(figsize=(10, 8))
g = gridspec.GridSpec(3, 3)

ax1 = plt.subplot(g[0, 0])
ax2 = plt.subplot(g[0, 1])
ax3 = plt.subplot(g[0, 2])
ax4 = plt.subplot(g[1, 0])
ax5 = plt.subplot(g[2, 0])

# Create another grid
g2 = gridspec.GridSpec(3, 3)
g2.update(hspace=0.00)

# Generate data for three subplots in g2
x = np.linspace(0, 2 * np.pi, 400)
ya = np.sin(x)
yb = np.cos(x)
y7 = np.sin(x) ** 2

# Get three different Axes objects
ax6a = plt.subplot(g2[1, 1])
ax6b = plt.subplot(g2[2, 1], sharex=ax6a)
ax7 = plt.subplot(g2[1:, -1])

# Hide the xticklabels of top subplot in the shared plots
plt.setp(ax6a.get_xticklabels(), visible=False)

# Set xticks for lower subplots in the shared plots
ax6b.set_xticks(np.pi * np.array([0, 1/2, 1, 3/2, 2]))

# Try plotting 
ax6a.plot(x, ya)
ax6b.plot(x, yb, 'g')
ax7.plot(x, y7, 'r')

plt.tight_layout()
plt.show()

This gives:

enter image description here

This answer was motivated by this answer and examples from older documentation of matplotlib.

If you want ax7 (red color subplot here) represented in to two separate subplots, either create a new Gridspec or use g depending on attributes you want to assign them e.g. in the code above:

# ax7 = plt.subplot(g2[1:, -1])
# ax7.plot(x, y7, 'r')

ax7a = plt.subplot(g[1, 2])
ax7b = plt.subplot(g[2, 2])

ax7a.plot(x, y7, 'r')
ax7b.plot(x, y7, 'r')

This gives:

enter image description here

medium-dimensional
  • 1,974
  • 10
  • 19