user8669 user8669 - 2 months ago 5x
Python Question

How would I iterate over a list of files and plot them as subplots on a single figure?

I'm trying to plot files onto 8 subplots for 2 figures. I am using a for loop and enumerate operator, along with axarray to do this.
I am almost there with the last step (with axarray) but need guidance as to how to finish it.
Here's my code:

'import matplotlib.pyplot as plt
import parse_gctoo
import glob
f, ax1 = plt.subplots()

def histo_plotter(file, plot_title, ax):
# read in file as string
GCT_object = parse_gctoo.parse(file)
# for c in range(9):
# print type(GCT_object.data_df.iloc[0][c])
# computing median of rows in data_df
# gene_medians = GCT_object.data_df.quantile(q=0.5,axis=1)
# plot_title = "Gene expression levels for {}".format(cell)
if plot_title == "ZSPCQNORM":
gene_means = GCT_object.data_df.mean(axis=1)
#making histogram of means
elif plot_title == "QNORM":
gene_medians = GCT_object.data_df.median(axis=1)
#making histogram of medians

# plt.ylim(-1, 1)
# plt.xlim(-1,1)

# histo_plotter("/Users/eibelman/Desktop/ZSCOREDATA- CXA061_SKL_48H_X1_B29_ZSPCQNORM_n372x978.gct.txt", "ZSPCQNORM", ax1)
# histo_plotter("/Users/eibelman/Desktop/NewLJP005_A375_24H_X2_B19_QNORM_n373x978.gct.txt", "QNORM", ax1)

# Create list of x2 LJP005 cell line files

z_list = glob.glob("/Volumes/cmap_obelix/pod/custom/LJP/roast/LJP005_[A375, A549, BT20, HA1E, HC515, HEPG2, HS578T, HT29]*X2*/zs/*ZSPCQNORM*.gct")
q_list = glob.glob("/Volumes/cmap_obelix/pod/custom/LJP/roast/LJP005_[A375, A549, BT20, HA1E, HC515, HEPG2, HS578T, HT29]*_X2_*/*_QNORM_*.gct")

# for loop which allows plotting multiple files in a single figure

f, axarray = plt.subplots(2, 4)
for n, single_q in enumerate(q_list):
# axarray = plt.subplot(len(q_list), 1, n+1)
axarray = histo_plotter(n, "QNORM", ax1)
# axarray[n].plot()

# f, axarray = plt.subplots(2, 4)
# for n, single_z in enumerate(z_list):
# # ax = plt.subplot(len(z_list), 1, n+1)
# histo_plotter(single_z, "ZSPCQNORM", ax1)'


You can try this:

import matplotlib.pyplot as plt


for n, single_q in enumerate(q_list):
    ax = plt.subplot(len(q_list), 1, n+1)
    GCT_object = parse_gctoo.parse(single_q)
    gene_medians = GCT_object.data_df.median(axis=1)
    # tweak title, labels, etc.


  • enumerate iterates over the items (s) while also returning their indices (n);
  • the function subplot(size, column, row) requires these parameters: size is the total amount of subplots in the figure, and row and column determine the position for the current plot. n+1 is necessary to put the plot in the correct position along the plot grid;
  • I edited the rest of the code with your own data