python中有没有一种方法可以使用matplotlib创建具有子地块的地物?

2024-05-23 22:41:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图显示一个包含3个图的图形,每个图都是(8,1)形子图的图

基本上,我想要一个有三个部分的大数字,每个部分都包含(8,1)形状的子图

我正在寻找一种方法来做到这一点,而不必手动设置所有的比例和间距。我这样做的原因是将一个8通道的神经信号与其他三个预定义的信号进行对比,每个信号都是8通道

如果这样做有意义的话,我正在尝试这样的东西(虚构代码):

fig, ax = plt.subplots(n_figures = 3, n_rows = 8, n_cols = 1)
ax[figure_i, row_j, col_k].imshow(image)

有办法做到这一点吗


这是我所说的一个例子。理想情况下,它会有三个子图,每个子图中都有一组8x1形状的子图。我知道如何通过查看所有的边距和设置比例来绘制所有这些内容,但我想知道是否有一种更简单的方法可以做到这一点,而不必查看我在上面编写的示例代码中所述的所有附加代码和设置

enter image description here


Tags: 方法代码图形信号原因数字手动ax
1条回答
网友
1楼 · 发布于 2024-05-23 22:41:17

通过首先使用^{}函数创建具有适当布局的子地块网格,然后通过轴阵列循环绘制数据,可以创建此类地物,如本例所示:

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()

subplot_grid

相关问题 更多 >