[英]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.我试图显示一个包含 3 个图的图形,每个图都是 (8,1) 形子图的 plot。
Essentially, I want one big figure with three sections each containing (8,1)-shaped subplots.本质上,我想要一个包含三个部分的大图,每个部分包含 (8,1) 形的子图。
I'm looking for a way to do this without having to manually set all the proportions and spacings.我正在寻找一种无需手动设置所有比例和间距的方法。 The reason I'm doing this is to visualize an 8-channel neural signal compared to three other pre-defined signals, each signal being 8 channels.
我这样做的原因是为了将一个 8 通道的神经信号与其他三个预定义的信号进行比较,每个信号都是 8 个通道。
If it makes any sense this way, I'm trying for something like this (ficticious code):如果这样有意义,我正在尝试这样的事情(虚构代码):
fig, ax = plt.subplots(n_figures = 3, n_rows = 8, n_cols = 1)
ax[figure_i, row_j, col_k].imshow(image)
Is there a way to do this?有没有办法做到这一点?
Here is an example of what I am talking about.这是我正在谈论的一个例子。 Ideally it would three subplots, and in each of the subplots there is a set of subplots of shape 8x1.
理想情况下,它将是三个子图,并且在每个子图中都有一组形状为 8x1 的子图。 I understand how to plot this all out by going through all the margins and setting the proportions, but I'm wondering if there's a simpler way to do this without having to go through all the additional code and settings as described in the above example code I've written.
我了解如何通过所有边距和设置比例来完成 plot 这一切,但我想知道是否有更简单的方法可以做到这一点,而无需通过上述示例代码中描述的所有附加代码和设置进行 go我写过。
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:您可以通过首先使用
plt.subplots()
function 创建具有适当布局的子图网格,然后通过轴数组循环到 plot 数据来创建这种图形,如下例所示:
import numpy as np # v 1.19.2
import 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 in enumerate(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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.