【发布时间】:2017-12-25 02:16:40
【问题描述】:
我对 matplotlib 中的 show() 方法有疑问。我有一个 for 循环(用于肽),在这个循环中我有另一个循环(用于 TFE)。在 for TFE 循环中,我绘制了 2 个不同的图形,退出 TFE 循环后,我想绘制第三个图形。问题是,如果我在绘制第二个图(而不是 fig1.show() 和 fig2.show() 如下面的代码)之后编写 plt.show(),它还会显示空的第三个图(创建在循环之前,但在它们之后绘制)在第一个 for 肽步骤之后。此外,每个肽步骤图在图 3 上仅绘制一个图,因为 show() 似乎清除了之前在图 (3) 上完成的工作。我希望在每个肽步骤中都绘制该图(3),但以某种方式保留先前步骤中的绘图(并且不会被 plt.show() 破坏)并且仅在最后显示。
我想使用 fig1.show() 和 fig2.show() 但是当我关闭第一个 TFE 循环中的 2 个数字中的一个时,图 (3) 正确显示,只是 for TFE 循环的其他数字未显示,终端中显示以下内容: 无效的命令名称“139620086899400idle_draw” 在执行时 “139620086899400idle_draw” (“之后”脚本)
您能否给我一些关于提到的无效命令名称的见解,我应该怎么做才能克服这个问题?我没有粘贴整个代码,因为它很长而且很乱,但我认为这足以理解一个问题。
编辑:好的,我创建了最少的代码,我的问题是: fig1 和 fig2 显示每个肽步骤的图形(在执行代码时,每个步骤的图形都添加了图形)。我想要 fig3 ,它按预期返回,但对于 fig1 和 fig2 我实际上想要每个肽步骤的 2 个数字,所以我有 fig1 和 fig2 数字和 1 个 fig3 数字的肽数。
import matplotlib.pyplot as plt
from scipy import stats
fig3 = plt.figure(3)
dictionary = {"peptide1": {5: {"deltaG": -15.5, "Fhteor": [0.9, 0.88], "T": [40, 45]}, 10: {"deltaG": -14.5, "Fhteor": [0.85, 0.83], "T": [40, 45]}},
"peptide666":{5: {"deltaG": -5.5, "Fhteor": [0.6, 0.57], "T": [40, 45]}, 10: {"deltaG": 1, "Fhteor": [0.4, 0.33], "T": [40, 45]}}}
for peptide in dictionary:
TFE_list = []
dG_list = []
fig1 = plt.figure(1)
fig2 = plt.figure(2)
for TFE in dictionary[peptide]:
plt.figure(1)
plt.plot(TFE, dictionary[peptide][TFE]["deltaG"], marker = "o") #plotting on fig1
plt.figure(2)
plt.plot(dictionary[peptide][TFE]["T"], dictionary[peptide][TFE]["Fhteor"], marker = "v") #plotting on fig2
if TFE <= 15:
TFE_list.append(TFE)
dG_list.append(dictionary[peptide][TFE]["deltaG"])
plt.figure(1)
lr = stats.linregress(TFE_list, dG_list)
y_list = [lr.intercept + i*lr.slope for i in TFE_list]
plt.plot(TFE_list, y_list) #plotting linear regression on plot 1
fig1.show()
fig2.show()
plt.figure(3)
plt.plot(len(peptide), len(peptide)*3, marker ="o") #plotting some characteristics derived from "for TFE loop" on fig3
plt.show()
【问题讨论】:
-
minimal reproducible example 旨在显示特定问题。您当然不应该使用您的实际数据或完整代码;只需使用一些随机值。
plt.show通常意味着在脚本中只调用一次以显示所有现有图形。不幸的是,这个问题无法让您了解您究竟想在这里实现什么,因此很难提供帮助。
标签: python matplotlib