【问题标题】:showing figure in matplotlib在 matplotlib 中显示图形
【发布时间】: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


【解决方案1】:

问题在于 fig(1) 和 fig(2) 的启动。它们是在第一步中启动的,但在下一步中它们只是被调用并叠加了绘图。答案是适当的启动,例如:

对于字典中的肽: fig1 = plt.figure(肽 + str(1)) fig2 = plt.figure(peptide + str(2))

str(1) 和 str(2) 旨在区分这两个数字。仍然不知道执行“139620086899400idle_draw”(“之后”脚本)时出现的消息:“139620086899400idle_draw”是什么意思,但此时它不再重要了。

【讨论】:

    【解决方案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)
    

    【讨论】:

    • 我真的不想关闭数字,因为我想为每个肽步骤显示 2 个数字。但无论如何我都尝试过并得到错误:“Figure”对象没有“close”属性。但是,要立即关闭,它必须是 plt.close()。
    • @BenQuick 对,抱歉。我对 matplotlib 有点生疏。现在怎么样了?
    猜你喜欢
    • 2012-02-21
    • 2015-10-11
    • 1970-01-01
    • 2011-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-28
    • 2015-03-21
    相关资源
    最近更新 更多