I would like to use a for loop
to iterate through to plot graphs of individual groups of gridspec
plots. I define a gridspec
to give me this plot:
I wanted to do a for loop
to repeat these plots in a given nrows
, ncols
. That is, I can specify to do these plots in 3 rows by 2 cols and obtain this:
Here is my code so far:
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()
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')