matplotlib 如何使用for循环将gridspec与nrows和ncols分组

umuewwlo  于 2023-04-07  发布在  其他
关注(0)|答案(1)|浏览(86)

我想使用一个for loop来迭代绘制各组gridspec图的图形。我定义了一个gridspec来给予我这个图:

我想做一个for loop,在给定的nrowsncols中重复这些图。也就是说,我可以指定在3行2列中做这些图,并获得:

以下是我目前为止的代码:

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

filename = ['file1', 'file2', 'file3', 'file4', 'file5', 'file6', 'file7', 'file8', 'file9', 'file10']
graph_title = ['(a) Title1', '(b) Title2', '(c) Title3', '(d) Title4', '(e) Title5', '(f) Title6', '(g) Title7', '(h) Title8', '(i) Title9', '(j) Title10',]

# define subplot grid
def graph3plots(filename, graph_title):
    fig = plt.figure(figsize=(3, 3))
    gs = GridSpec(6,5, figure=fig)
    ax2 = plt.subplot(gs.new_subplotspec((0, 0), rowspan=4))
    ax3 = plt.subplot(gs.new_subplotspec((0, 1), rowspan=4, colspan=4))
    ax4 = plt.subplot(gs.new_subplotspec((4, 1), colspan=4))
    fig.suptitle(graph_title)
    fig.subplots_adjust(wspace=0.35, hspace=0.25)

n = 0
for i in range(0,10):
    fig.add_subplot(5,2,i+1)
    graph3plots(filename[n], graph_title[n])
    n += 1
  
plt.savefig(f'Total Plots.pdf')
plt.show()
q7solyqu

q7solyqu1#

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

filename = ['file1', 'file2', 'file3', 'file4', 'file5', 'file6', 'file7', 'file8', 'file9', 'file10']
graph_title = ['(a) Title1', '(b) Title2', '(c) Title3', '(d) Title4', '(e) Title5', '(f) Title6', '(g) Title7', '(h) Title8', '(i) Title9', '(j) Title10',]

# define subplot grid
def graph3plots(filename, graph_title,f2,gt2):
    fig = plt.figure(figsize=(8, 3))
    fig.add_subplot(6,6,i+1)
    gs = GridSpec(7,12, figure=fig)
    ax2 = plt.subplot(gs.new_subplotspec((0, 0), rowspan=4))
    ax3 = plt.subplot(gs.new_subplotspec((0, 1), rowspan=4, colspan=4))
    ax4 = plt.subplot(gs.new_subplotspec((4, 1), colspan=4))
    ax5 = plt.subplot(gs.new_subplotspec((0, 6), rowspan=4))
    ax6 = plt.subplot(gs.new_subplotspec((0, 7), rowspan=4, colspan=4))
    ax7 = plt.subplot(gs.new_subplotspec((4, 7), colspan=4))
    fig.suptitle(graph_title+" "*50+gt2)#I can't make it better
    fig.subplots_adjust(wspace=2.15, hspace=1.45)
    plt.show()

for i in range(0,5,2):
    graph3plots(filename[i], graph_title[i],filename[i+1], graph_title[i+1])
  
plt.savefig(f'Total Plots.pdf')

更新

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

filename = ['file1', 'file2', 'file3', 'file4', 'file5', 'file6', 'file7', 'file8', 'file9', 'file10']
graph_title = ['(a) Title1', '(b) Title2', '(c) Title3', '(d) Title4', '(e) Title5', '(f) Title6', '(g) Title7', '(h) Title8', '(i) Title9', '(j) Title10',]
nrows,ncols = 3,3

# define subplot grid
fig = plt.figure(figsize = (3*nrows,3*ncols))
gs = GridSpec(6*nrows,5*ncols, figure=fig)
call = 1
def graphnplots(title,i,j):
    global fig,gs,call
    ax2 = plt.subplot(gs.new_subplotspec((1+i*6, j*5), rowspan=4))
    ax3 = plt.subplot(gs.new_subplotspec((1+i*6, 1+j*5), rowspan=4, colspan=4)).set_title(title+" "*5)
    ax4 = plt.subplot(gs.new_subplotspec((5+i*6, 1+j*5), colspan=4))
    call += 1

for i in range(nrows):
    for j in range(ncols):
        graphnplots(graph_title[i*3+j],i,j)#It was BUGGY!
fig.subplots_adjust(wspace=4, hspace=4)
plt.show()
plt.savefig(f'Total Plots.pdf')

更新

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

filename = ['file1', 'file2', 'file3', 'file4', 'file5', 'file6', 'file7', 'file8', 'file9', 'file10']
graph_title = ['(a) Title1', '(b) Title2', '(c) Title3', '(d) Title4', '(e) Title5', '(f) Title6', '(g) Title7', '(h) Title8', '(i) Title9', '(j) Title10',]
nrows,ncols = 3,1

# define subplot grid
fig = plt.figure(figsize = (4*ncols,4*nrows))
gs = GridSpec(6*nrows,5*ncols, figure=fig)
call = 1
def graphnplots(title,i,j):
    global fig,gs,call
    ax2 = plt.subplot(gs.new_subplotspec((1+i*6, j*5), rowspan=4))
    ax3 = plt.subplot(gs.new_subplotspec((1+i*6, 1+j*5), rowspan=4, colspan=4)).set_title(title+" "*5)
    ax4 = plt.subplot(gs.new_subplotspec((5+i*6, 1+j*5), colspan=4))
    call += 1

for i in range(nrows):
    for j in range(ncols):
        if i*ncols+j > len(graph_title):
            break
        graphnplots(graph_title[i*ncols+j],i,j)
fig.subplots_adjust(wspace=2, hspace=2)
plt.show()
plt.savefig(f'Total Plots.pdf')

I used this.(Made by Zach)

相关问题