python - Matplotlib:子图

标签 python loops matplotlib subplot

我有几个时间序列信号(8x8),我想使用子图来绘制它们。我的数据存储在名为 H(x, y, N) 的矩阵中,其中 N 是每个信号中的点数。我想使用子图显示 64 个信号。

fig  = figure(figsize=(12,8))
time = np.arange(0, Nt, 1)

for x in range(8):
    for y in range(8):
        subplot(8,y+1,x+1)
        plot(time,H[x,y,:])

我得到的是第一行有 8 个信号,第二行有 4 个信号,然后是 2、2、1、1、1 和 1。

最佳答案

这不是 subplot 索引的工作原理。来自 docs to subplot :

subplot(nrows, ncols, plot_number)

Where nrows and ncols are used to notionally split the figure into nrows * ncols sub-axes, and plot_number is used to identify the particular subplot that this function is to create within the notional grid. plot_number starts at 1, increments across rows first and has a maximum of nrows * ncols.

因此,您希望有 nrows=8ncols=8 以及 1-64 范围内的 plot_number,所以像:

nrows,ncols = 8,8
for y in range(8):
    for x in range(8):
        plot_number = 8*y + x + 1
        subplot(nrows,ncols,plot_number)
        plot(time,H[x,y,:])
        
        # Remove tick labels if not on the bottom/left of the grid
        if y<7: gca().set_xticklabels([])
        if x>0: gca().set_yticklabels([])

要删除刻度标签,请使用 gca() 获取当前坐标区,并将 xticklabelsyticklabels 设置为空列表:[]

关于python - Matplotlib:子图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35510155/

相关文章:

loops - 理解go中的fmt包

matplotlib - 增加 FacetGrid 图上的行之间的空间

python - matplotplotlib 轮廓图的 numpy 模拟?

python - 文件描述符在 Python 中的 fork 子进程中关闭

python - 在虚拟环境中手动安装包并且 pip 要求知道

python - 如何将我放入 Python Pandas 数据框中的重复项写入文件

python - 用 Pandas 合并两个数字

python - 如何在 discord.py 中设置可选参数?

java - 如何在 foreach 循环中为每个值添加一些其他值?

php - 如何一次更新 92 MySQL 行中的 2 列