不保存脚本输出中的所有 matplotlib 图?
Not saving all matplotlib graphs from script output?
我有一个 运行 在 IPython 中的脚本,基本上采用 gene_names
的输入 .csv 文件并将它们推入此 for 循环,其中
with open('C:\Users\Work\Desktop\Book1.csv', 'rU') as f:
reader = csv.reader(f)
with PdfPages('poopyheadjoe04.pdf') as pdf:
for row in reader:
gene_name = row
probe_exclusion_keyword = []
print gene_name
然后将此列表(在 .csv 文件中)中的 gene_name 值输入一行,if inference_method == "approximate_random"
:(在 Scripts.py 中)
with open('C:\Users\Work\Desktop\Book1.csv', 'rU') as f:
reader = csv.reader(f)
with PdfPages('poopyheadjoe04.pdf') as pdf:
for row in reader:
gene_name = row
probe_exclusion_keyword = []
print gene_name
print "Fetching probe ids for gene %s" % gene_name
probes_dict = get_probes_from_genes(gene_name)
print "Found %s probes: %s" % (len(probes_dict), ", ".join(probes_dict.values()))
if probe_exclusion_keyword:
probes_dict = {probe_id: probe_name for (probe_id, probe_name) in probes_dict.iteritems() if not args.probe_exclusion_keyword in probe_name}
print "Probes after applying exclusion cryterion: %s" % (", ".join(probes_dict.values()))
print "Fetching expression values for probes %s" % (", ".join(probes_dict.values()))
expression_values, well_ids, donor_names = get_expression_values_from_probe_ids(
probes_dict.keys())
print "Found data from %s wells sampled across %s donors" % (len(well_ids), len(set(donor_names)))
print "Combining information from selected probes"
combined_expression_values = combine_expression_values(
expression_values, method=probes_reduction_method)
print "Translating locations of the wells to MNI space"
mni_coordinates = get_mni_coordinates_from_wells(well_ids)
print "Checking values of the provided NIFTI file at well locations"
nifti_values = get_values_at_locations(
stat_map, mni_coordinates, mask_file=mask, radius=radius, verbose=True)
# preparing the data frame
names = ["NIFTI values", "%s expression" % gene_name, "donor ID"]
data = pd.DataFrame(np.array(
[nifti_values, combined_expression_values, donor_names]).T, columns=names)
data = data.convert_objects(convert_numeric=True)
len_before = len(data)
data.dropna(axis=0, inplace=True)
nans = len_before - len(data)
if nans > 0:
print "%s wells fall outside of the mask" % nans
if inference_method == "fixed":
print "Performing fixed effect analysis"
fixed_effects(data, ["NIFTI values", "%s expression" % gene_name])
**if inference_method == "approximate_random":**
print "Performing approximate random effect analysis"
approximate_random_effects(
data, ["NIFTI values", "%s expression" % gene_name], "donor ID")
print "poopy"
pdf.savefig()
plt.ion() #should i add ion() here?
if inference_method == "bayesian_random":
print "Fitting Bayesian hierarchical model"
bayesian_random_effects(
data, ["NIFTI values", "%s expression" % gene_name], "donor ID", n_samples, n_burnin)
# if __name__ == '__main__': #What exactly does this do? Start trigger for the script to run?
# main()
触发 approximate_random_effects
(在 Analysis.py 中)绘制两个图 ,violinplot 和 lmplot:
def approximate_random_effects(data, labels, group):
correlation_per_donor = {}
for donor_id in set(data[group]):
correlation_per_donor[donor_id], _, _, _, _ = linregress(list(data[labels[0]][data[group] == donor_id]),
list(data[labels[1]][data[group] == donor_id]))
average_slope = np.array(correlation_per_donor.values()).mean()
t, p_val = ttest_1samp(correlation_per_donor.values(), 0)
print "Averaged slope across donors = %g (t=%g, p=%g)"%(average_slope, t, p_val)
sns.violinplot([correlation_per_donor.values()], inner="points", names=["donors"])
plt.ylabel("Linear regression slopes between %s and %s"%(labels[0],labels[1]))
plt.axhline(0, color="red")
sns.lmplot(labels[0], labels[1], data, hue=group, col=group, col_wrap=3)
plt.ion()
return average_slope, t, p_val
我正在尝试通过大致遵循“”和 matplotlib.PdfPages
方法将所有 gene_name 的两个图表保存到 pdf 文件中。
但是,在 pdf 文件中,我只得到了我所有 gene_name 的 lmplot 而不是小提琴图。 有趣的是,将打印添加到 def approximate_random_effects
(属于 Analysis.py)不反映任何输出。我该怎么做才能解决这个问题?
谢谢!非常感谢您的帮助!
您需要为图创建两个不同的轴,一个用于小提琴图,一个用于 imshow 图。在 approximate_random_effects 函数的顶部添加
fig, (ax_left, ax_right) = plt.subplots(1,2, figsize=(6,3)) # change figsize to the size you want, default is ugly with two plots
或
fig, (ax_top, ax_bottom) = plt.subplots(2, figsize=(3, 6)) # change figsize to the size you want, default is ugly with two plots
取决于您想要上下还是并排。对于此答案的其余部分,并排假设。
现在您需要通过添加 ax=ax_left 和 ax=ax_right
来更改绘图调用
sns.violinplot([correlation_per_donor.values()], inner="points", names=["donors"], ax=ax_left)
和
sns.lmplot(labels[0], labels[1], data, hue=group, col=group, col_wrap=3, ax=ax_right)
我有一个 运行 在 IPython 中的脚本,基本上采用 gene_names
的输入 .csv 文件并将它们推入此 for 循环,其中
with open('C:\Users\Work\Desktop\Book1.csv', 'rU') as f:
reader = csv.reader(f)
with PdfPages('poopyheadjoe04.pdf') as pdf:
for row in reader:
gene_name = row
probe_exclusion_keyword = []
print gene_name
然后将此列表(在 .csv 文件中)中的 gene_name 值输入一行,if inference_method == "approximate_random"
:(在 Scripts.py 中)
with open('C:\Users\Work\Desktop\Book1.csv', 'rU') as f:
reader = csv.reader(f)
with PdfPages('poopyheadjoe04.pdf') as pdf:
for row in reader:
gene_name = row
probe_exclusion_keyword = []
print gene_name
print "Fetching probe ids for gene %s" % gene_name
probes_dict = get_probes_from_genes(gene_name)
print "Found %s probes: %s" % (len(probes_dict), ", ".join(probes_dict.values()))
if probe_exclusion_keyword:
probes_dict = {probe_id: probe_name for (probe_id, probe_name) in probes_dict.iteritems() if not args.probe_exclusion_keyword in probe_name}
print "Probes after applying exclusion cryterion: %s" % (", ".join(probes_dict.values()))
print "Fetching expression values for probes %s" % (", ".join(probes_dict.values()))
expression_values, well_ids, donor_names = get_expression_values_from_probe_ids(
probes_dict.keys())
print "Found data from %s wells sampled across %s donors" % (len(well_ids), len(set(donor_names)))
print "Combining information from selected probes"
combined_expression_values = combine_expression_values(
expression_values, method=probes_reduction_method)
print "Translating locations of the wells to MNI space"
mni_coordinates = get_mni_coordinates_from_wells(well_ids)
print "Checking values of the provided NIFTI file at well locations"
nifti_values = get_values_at_locations(
stat_map, mni_coordinates, mask_file=mask, radius=radius, verbose=True)
# preparing the data frame
names = ["NIFTI values", "%s expression" % gene_name, "donor ID"]
data = pd.DataFrame(np.array(
[nifti_values, combined_expression_values, donor_names]).T, columns=names)
data = data.convert_objects(convert_numeric=True)
len_before = len(data)
data.dropna(axis=0, inplace=True)
nans = len_before - len(data)
if nans > 0:
print "%s wells fall outside of the mask" % nans
if inference_method == "fixed":
print "Performing fixed effect analysis"
fixed_effects(data, ["NIFTI values", "%s expression" % gene_name])
**if inference_method == "approximate_random":**
print "Performing approximate random effect analysis"
approximate_random_effects(
data, ["NIFTI values", "%s expression" % gene_name], "donor ID")
print "poopy"
pdf.savefig()
plt.ion() #should i add ion() here?
if inference_method == "bayesian_random":
print "Fitting Bayesian hierarchical model"
bayesian_random_effects(
data, ["NIFTI values", "%s expression" % gene_name], "donor ID", n_samples, n_burnin)
# if __name__ == '__main__': #What exactly does this do? Start trigger for the script to run?
# main()
触发 approximate_random_effects
(在 Analysis.py 中)绘制两个图 ,violinplot 和 lmplot:
def approximate_random_effects(data, labels, group):
correlation_per_donor = {}
for donor_id in set(data[group]):
correlation_per_donor[donor_id], _, _, _, _ = linregress(list(data[labels[0]][data[group] == donor_id]),
list(data[labels[1]][data[group] == donor_id]))
average_slope = np.array(correlation_per_donor.values()).mean()
t, p_val = ttest_1samp(correlation_per_donor.values(), 0)
print "Averaged slope across donors = %g (t=%g, p=%g)"%(average_slope, t, p_val)
sns.violinplot([correlation_per_donor.values()], inner="points", names=["donors"])
plt.ylabel("Linear regression slopes between %s and %s"%(labels[0],labels[1]))
plt.axhline(0, color="red")
sns.lmplot(labels[0], labels[1], data, hue=group, col=group, col_wrap=3)
plt.ion()
return average_slope, t, p_val
我正在尝试通过大致遵循“matplotlib.PdfPages
方法将所有 gene_name 的两个图表保存到 pdf 文件中。
但是,在 pdf 文件中,我只得到了我所有 gene_name 的 lmplot 而不是小提琴图。 有趣的是,将打印添加到 def approximate_random_effects
(属于 Analysis.py)不反映任何输出。我该怎么做才能解决这个问题?
谢谢!非常感谢您的帮助!
您需要为图创建两个不同的轴,一个用于小提琴图,一个用于 imshow 图。在 approximate_random_effects 函数的顶部添加
fig, (ax_left, ax_right) = plt.subplots(1,2, figsize=(6,3)) # change figsize to the size you want, default is ugly with two plots
或
fig, (ax_top, ax_bottom) = plt.subplots(2, figsize=(3, 6)) # change figsize to the size you want, default is ugly with two plots
取决于您想要上下还是并排。对于此答案的其余部分,并排假设。
现在您需要通过添加 ax=ax_left 和 ax=ax_right
来更改绘图调用sns.violinplot([correlation_per_donor.values()], inner="points", names=["donors"], ax=ax_left)
和
sns.lmplot(labels[0], labels[1], data, hue=group, col=group, col_wrap=3, ax=ax_right)