【问题标题】:How to update multiple different figures with different data如何用不同的数据更新多个不同的图形
【发布时间】:2020-02-29 09:09:00
【问题描述】:

我认为问这个问题可能很愚蠢,但我在谷歌上找不到任何匹配我的问题的解决方案。希望有人能帮忙。

简单地说,我想在同一个图上绘制不同的函数,而不是子图,因为我发现大多数在线解决方案都使用子图来做到这一点。我的目的是为了更好地比较不同的数据线在同一个图上相同的情节,但具有不同的颜色和不同的图例。我有 2 个数字,因为第二个数字只是具有相同想法的其他东西。

我尝试使用以下代码。但是每次循环都会覆盖子图。我不想删除旧行。我希望每个循环都分别更新图 1 和图 2,而不擦除之前的数据线。

    fig1 = plt.figure(1)
    fig2 = plt.figure(2)
    for diffSteps,listpair in fixed_table_plots.items():
        ax1 = fig1.add_subplot(1,1,1)
        ax1.scatter(listpair[0],listpair[1],s=2000,label="error_step_"+str(diffTableSteps))
        ax1.legend()
        plt.show()

        ax2 = fig2.add_subplot(1,1,1)
        accuracy_bw = [x - math.log(y,2) for x,y in zip(listpair[0],listpair[1])]
        ax2.scatter(listpair[0],accuracy_bw,s=200,label="accuracy bw_step_"+str(diffTableSteps))
        ax2.set_xlabel("input int bw")
        ax2.set_ylabel("total bw accuracy")
        ax2.legend()
        plt.show()

非常感谢任何帮助!

更新:

    for diffSteps,listpair in fixed_table_plots.items():
        plt.figure(1)
        ax1.scatter(listpair[0],listpair[1],s=2000,label="error_step_"+str(diffTableSteps))
        ax1.legend()

        plt.figure(2)
        accuracy_bw = [x - math.log(y,2) for x,y in zip(listpair[0],listpair[1])]
        ax2.scatter(listpair[0],accuracy_bw,s=200,label="accuracy bw_step_"+str(diffTableSteps))
        ax2.set_xlabel("input int bw")
        ax2.set_ylabel("total bw accuracy")

这对我来说效果很好,唯一的问题是 plt.show() 不能在中间调用。有没有办法动态显示图?重新激活情节也感觉很奇怪。如果这是一个好方法,请告诉我...

谢谢!

【问题讨论】:

  • @finefoot 我不知道。我刚试过。我认为我犯的一个错误是调用 plt.show() 会清除所有内容。

标签: python matplotlib


【解决方案1】:

这个问题类似于How to keep the previous plot and not clear the figure after calling plt.show() without simply repeating the call? 但是,链接的帖子是关于更新单个数字的,而您询问的是多个数字。我认为您将能够将以下最小示例改编为您的代码:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 2 * np.pi)

fig1 = plt.figure()
fig2 = plt.figure()
plt.close(fig1)
plt.close(fig2)

for i in range(3):

    plt.figure()
    fm = plt.get_current_fig_manager()
    fm.canvas.figure = fig1
    fig1.canvas = fm.canvas
    plt.plot(x, np.sin(x) + i)

    plt.figure()
    fm = plt.get_current_fig_manager()
    fm.canvas.figure = fig2
    fig2.canvas = fm.canvas
    plt.plot(x, np.cos(x) + i)

    plt.show()
    plt.close()

【讨论】:

  • 这仅给出 1 个数字。我最多可以有 n 个数字,我需要在循环中指定要更新的数字
  • @user3431800 哎呀,对不起,是的,当然。所以这个问题是关于更新 multiple 数字(我已经修改了标题)并且我添加了另一个关于更新 1 个数字 here 的问题。花了我一段时间,但如果我理解正确的话,我认为另一篇文章中的方法 D 在这里适用。更新答案,等待反馈。 :)
  • 您好,非常感谢您的回答。如果我正确理解了答案以及其他帖子的答案, plt.get_current_fig_manager() 和 fm.canvas.figure 将交替执行与激活 fig1 和 fig2 相同的操作。而不是用 plt.figure(id) 来做,这允许我在前面的工作中传递一个预定义的对象,而不是使用 id 来做后台工作。请允许我问:有什么好处或使用经理?
  • @user3431800 来自文档:“图形管理器是在屏幕上显示图形的实际后端依赖窗口的容器” - 显然,图形本身(包含所有曲线和数据)是仍然存在,但从plt.show 关闭窗口后,必须重新创建其图形管理器才能再次显示图形。循环内的plt.figure() 仅用于到达下一个plt.show 的“新”图形管理器。只是在再次显示之前,在本例中,我们添加了另一个绘图。我假设这个例子对你有用吗?这就是您对代码的想法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-01-08
  • 1970-01-01
  • 1970-01-01
  • 2018-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多