Is There A Way In Python Using Matplotlib To Create A Figure With Subplots Of Subplots?
I'm trying to display a figure that contains 3 plots, and each of the plots is a plot of (8,1)-shaped subplots. Essentially, I want one big figure with three sections each containi
Solution 1:
You can create this kind of figure by first creating a subplot grid with the appropriate layout using the plt.subplots()
function and then looping through the array of axes to plot the data, like in this example:
import numpy as np # v 1.19.2import matplotlib.pyplot as plt # v 3.3.2# Create sample signal data as a 1-D list of arrays representing 3x8 channels
signal_names = ['X1', 'X2', 'X3']
nsignals = len(signal_names) # ncols of the subplot grid
nchannels = 8# nrows of the subplot grid
nsubplots = nsignals*nchannels
x = np.linspace(0, 14*np.pi, 100)
y_signals = nsubplots*[np.cos(x)]
# Set subplots width and height
subp_w = 10/nsignals # 10 corresponds the figure width in inches
subp_h = 0.25*subp_w
# Create figure and subplot grid with the appropriate layout and dimensions
fig, axs = plt.subplots(nchannels, nsignals, sharex=True, sharey=True,
figsize=(nsignals*subp_w, nchannels*subp_h))
# Optionally adjust the space between the subplots: this can also be done by# adding 'gridspec_kw=dict(wspace=0.1, hspace=0.3)' to the above function# fig.subplots_adjust(wspace=0.1, hspace=0.3)# Loop through axes to create plots: note that the list of axes is transposed# in this example to plot the signals one after the other column-wise, as# indicated by the colors representing the channels
colors = nsignals*plt.get_cmap('tab10').colors[:nchannels]
for idx, ax inenumerate(axs.T.flat):
ax.plot(x, y_signals[idx], c=colors[idx])
if ax.is_first_row():
ax.set_title(signal_names[idx//nchannels], pad=15, fontsize=14)
plt.show()
Post a Comment for "Is There A Way In Python Using Matplotlib To Create A Figure With Subplots Of Subplots?"