我将如何迭代一个文件列表,并将它们绘制成一个图上的子图? [英] How would I iterate over a list of files and plot them as subplots on a single figure?

查看:148
本文介绍了我将如何迭代一个文件列表,并将它们绘制成一个图上的子图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将文件绘制到8个子图上2个数字。我正在使用for循环和枚举运算符,以及axarray来做到这一点。
我几乎在最后一步(用斧头),但需要指导如何完成它。
这里是我的代码:

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

def histo_plotter(file,plot_title,ax):
#以字符串形式读入文件
GCT_object = parse_gctoo.parse (文件)
#为范围(9)中的c:
#打印类型(GCT_object.data_df.iloc [0] [c])
#计算data_df中行的中位数$ b $格式(单元格)
如果plot_title ==ZSPCQNORM:b#gene_medians = GCT_object.data_df.quantile(q = 0.5,axis = 1)
#plot_title =
gene_means = GCT_object.data_df.mean(axis = 1)
#制作手段的直方图
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)
#制作媒体直方图
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)
#########



#创建x2 LJP005单元格文件列表

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循环允许绘制多个文件(q_list):

f,axarray = plt.subplots(2,4)
),1,n + 1)
axarray = histo_plotter(n,QNORM,ax1)
#axarray [n] .plot()
plt.show()

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




  import matplotlib.pyplot as plt 
$>

b $ b plt.figure()

for n,枚举中的single_q(q_list):
ax = plt.subplot(len(q_list),1,n + 1)$ b $ (b)GCT_object = parse_gctoo.parse(single_q)
gene_medians = GCT_object.data_df.median(axis = 1)
plt.hist(gene_medians)
#调整标题,标签等

plt.show()

解释:


  • enume ( n );同时返回它们的下标( n)。

  • 函数子图(size,column,row)需要这些参数: size 是图中子图的总数,而 row 确定当前图的位置。 n + 1 有必要将图表沿着图形网格放置在正确的位置上;
  • 我编辑了其余的代码您自己的数据


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

解决方案

You can try this:

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()

Explaining:

  • 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

这篇关于我将如何迭代一个文件列表,并将它们绘制成一个图上的子图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆