在matplotlib中显示图形

2024-09-29 19:27:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个关于matplotlib中show()方法的问题。我有一个for循环(针对肽),在这个循环中我有另一个循环(TFE)。在for-TFE循环中,我绘制了两个不同的图形,退出TFE循环后,我想在第三个图形上绘制。问题是,如果我写表演()在第二个图上绘制后(而不是图1.show()和图2)。show()就像下面的代码一样),它还显示了第一个for peptide步骤之后的空的第三个数字(在循环之前创建,但在循环之后绘制)。此外,每个for peptide步骤在图3上只绘制了一个图,因为show()似乎清除了之前在图(3)上所做的工作。我希望图(3)在每一个for peptide步骤中都被绘制出来,但在某种程度上它保留了之前步骤中的绘图(并且不会被中断)表演())并且仅在末尾显示。在

我想使用fig1.show()和fig2.show(),但当我关闭第一个TFE循环中的两个图形中的一个时,图(3)会正确出现,只是for-TFE循环的其他图形没有显示,并且终端中会显示以下内容: 命令名“139620086899400 idle_draw”无效 执行时 “139620086899400空闲绘制” (“后”脚本)

你能告诉我一些关于无效命令名的细节吗?我应该怎么做才能克服这个问题?我没有粘贴整个代码,因为它相当长和凌乱,但我认为它已经足够理解一个问题。在

编辑:好的,我创建了最小的代码,我的问题是:图1和图2显示了每一个肽步骤的图(在执行代码时,每个步骤的图都加在图上)。我想要图3,它会像预期的那样返回,但是对于图1和图2,我实际上希望每个肽步骤有2个数字,所以我有图1和图2的肽数和图3的1个数字。在

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

Tags: 代码图形fordictionaryplotshow绘制步骤
2条回答

fig1.show()之后立即添加fig1.close()会发生什么情况?在

另外,我相当确定将plt.figure(2)放在for循环中会导致问题,更不用说在for循环之外声明了一个变量fig2 = plt.figure(2),然后在循环中也调用了plt.figure(2)。我已经有一段时间没有使用matplotlib了,但是我不认为你应该在for循环中放一个plt.figure(),除非你在多个图形上循环,例如:

for i in range(len(all_figures_to_be_plotted)):
    plt.figure(i)

问题的起因是图(1)和图(2)。它们在第一步中启动,但在下一步中,它们只是被调用,并被叠加在一起。答案是正确的启动,例如:

对于字典中的肽: 图1=plt.图(肽+str(1)) 图2=plt.图(肽+str(2))

str(1)和str(2)是用来区分这两个数字的。我还不知道执行“139620086899400idle_draw”(“after”脚本)时的消息“139620086899400 idle_draw”是什么意思,但此时它已不再重要。在

相关问题 更多 >

    热门问题