我有一个关于 matplotlib 中的 show() 方法的问题。我有一个 for 循环(用于肽),在这个循环中我有另一个循环(用于 TFE)。在 for TFE 循环中,我在两个不同的图形上绘制,退出 TFE 循环后,我想在第三个图形上绘制。问题是,如果我在第二个图上绘制后编写 plt.show() (而不是像下面的代码中那样的Fig1.show() 和Fig2.show() ),它还会显示空的第三个图(它是创建的)在循环之前,但在循环之后绘制)在第一个肽步骤之后。此外,每个肽步骤仅在图 3 上绘制一个图,因为 show() 似乎清除了之前在图 (3) 上完成的工作。我希望在每个肽步骤中绘制图(3),但以某种方式保留先前步骤中的绘图(并且不会被 plt.show() 破坏)并且仅在最后显示。
我想使用fig1.show()和fig2.show(),但是当我关闭第一个TFE循环中的两个图形之一时,图(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()
最佳答案
在 fig1.show()
之后添加 fig1.close()
会发生什么?
此外,我相当确定将 plt.figure(2)
放在 for 循环内会导致问题,更不用说您声明了变量 fig2 = plt.figure(2)
在 for 循环之外,然后也在循环内调用 plt.figure(2)
。自从我使用 matplotlib 以来已经有一段时间了,但我认为您不应该将 plt.figure()
放在 for 循环中,除非您要循环多个数字,即:
for i in range(len(all_figures_to_be_plotted)):
plt.figure(i)
关于python - 在 matplotlib 中显示图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45196822/