我试着把文件打印到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)'
首先,在循环开始时调用
plt.figure()
一次就足够了。在其次,您需要正确使用
subplot
。以下是subplot
函数的文档:如果您希望每个文件都有一个新的图形,那么在每次迭代中,您应该不带参数地调用
plt.figure()
。在你可以试试这个:
解释:
enumerate
迭代项目(s
),同时返回它们的索引(n
)subplot(size, column, row)
需要这些参数:size
是图中子图的总数,row
和{n+1
是将绘图沿绘图网格放置在正确位置所必需的相关问题 更多 >
编程相关推荐