python - 我如何迭代文件列表并将它们绘制为单个图形上的子图?

标签 python for-loop matplotlib plot

我正在尝试将文件绘制到 2 个数字的 8 个子图中。我使用 for 循环和枚举运算符以及 axarray 来执行此操作。 我即将完成最后一步(使用 axarray),但需要有关如何完成它的指导。 这是我的代码:

'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
        ax.hist(gene_means)
        plt.title("MeanGeneExpressionZSPCQNORM")
        plt.xlabel("MedianGeneExpression")
        plt.ylabel("Count")
    elif plot_title == "QNORM":
        gene_medians = GCT_object.data_df.median(axis=1)
        #making histogram of medians
        ax.hist(gene_medians)
        plt.title("MedianGeneExpressionQNORM")
        plt.xlabel("MedianGeneExpression")
        plt.ylabel("Count")
plt.show()
f.savefig("hist_example1.png")



# 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()
plt.show()

# 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)'

最佳答案

你可以试试这个:

import matplotlib.pyplot as plt

plt.figure()

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)
    plt.hist(gene_medians)
    # tweak title, labels, etc.

plt.show()

解释:

  • enumerate 迭代项目 (s),同时返回其索引 (n);
  • 函数subplot(size, column, row)需要以下参数:size是图中子图的总数,row > 和 column 确定当前绘图的位置。需要 n+1 才能将绘图放置在沿着绘图网格的正确位置;
  • 我用您自己的数据编辑了其余代码

关于python - 我如何迭代文件列表并将它们绘制为单个图形上的子图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38727364/

相关文章:

python - 如何提取 DataFrame 的不同对角线?

python - 如何调整子图的高度和宽度以紧密配合?

datetime - 在 Pandas 中绘制 TimeDeltas

python - 加快元组计数

python - 存储最后 3 个分数并删除旧分数并计算平均值?

python - 如何使用 .find() 来定位一个单元格并在循环中开始更新下面行中的单元格?

javascript - 为什么for循环无法识别||运算符(operator)?

c++ - 我得到一个 "string subscript out of range error"。我不明白为什么

python - 如何删除空白以保持平衡?

python - 多个 y 尺度,但只有一个启用了平移和缩放