【问题标题】:Update data on matplotlib更新 matplotlib 上的数据
【发布时间】:2020-12-27 17:27:55
【问题描述】:

我想更新matplolib 图上的数据,因此我已经修补了几个示例中的代码。仅出于说明目的,我有两个不同的矩阵数据,它们将被绘制在两个不同的子图中交替。这是代码:

import numpy as np
import matplotlib.pyplot as plt
import time

matrix1 = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
matrix2 = np.array([[2,3,4,5],[6,7,8,9],[10,11,12,13]])

plt.ion()

fig = plt.figure()
ax1 = fig.add_subplot(222)
ax2 = fig.add_subplot(221)

def update(mat1, mat2):

    ax1.plot(mat1.T, 'ro', color='red')
    ax2.plot(mat2.T, 'ro', color='blue')

    fig.canvas.draw()
    fig.canvas.flush_events()

    time.sleep(1)
    
    ax1.plot(mat2.T, 'ro', color='blue')
    ax2.plot(mat1.T, 'ro', color='red')

    fig.canvas.draw()
    fig.canvas.flush_events()

    time.sleep(1)

for i in range(10):
    update(matrix1, matrix2)

这显然行不通。如下图所示,matrix1 (red) 和 matrix2 (blue) 的数据不能同时出现在每个 subplot 中,而是每个 subplot 一个矩阵并在每个 subplot 之后反转位置第二个。

任何建议将不胜感激。

P.S.我知道有类似的问题我尝试过咨询,但没有成功。

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    matplotlib 提供animation library。最好使用它,因为动画事件循环具有更好的错误处理,例如当您从脚本运行时关闭绘图图。

    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.animation import FuncAnimation
    
    matrix1 = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
    matrix2 = np.array([[2,3,4,5],[6,7,8,9],[10,11,12,13]])
    matrices = [matrix1.T, matrix2.T]
    
    fig, (ax1, ax2) = plt.subplots(1, 2, sharex=True, sharey=True)
    lines1 = ax1.plot(matrices[0], 'ro', color='red')
    lines2 = ax2.plot(matrices[1], 'ro', color='blue')
    
    def update(frame):
        matrices.reverse()
        for index, l1, l2 in zip(range(3), lines1, lines2):
            l1.set_data(range(4), matrices[0][:, index])
            l2.set_data(range(4), matrices[1][:, index])
    
    ani = FuncAnimation(fig, update, frames=range(10), interval=1000)
    plt.show()
    

    为了使用FuncAnimation(),您需要跟踪ax.plot() 返回的Line2D 对象,在这种情况下lines1lines2Line2D 对象的列表(总共6 个)因为每个矩阵有三行)。然后在update() 函数中,您可以.set_data() 或做任何您需要更改数据的操作。您可以查看documentation for Line2D,了解如何使用line.set_color() 等更改绘制数据的颜色。

    【讨论】:

    • 我复制了代码但没有任何反应,没有出现任何情节。我做错了吗?
    • @user3060854 我忘了plt.show() 它应该可以工作:)
    【解决方案2】:

    试试这个

    import numpy as np
    import matplotlib.pyplot as plt
    import time
    
    from numpy.core.shape_base import block
    
    matrix1 = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
    matrix2 = np.array([[2,3,4,5],[6,7,8,9],[10,11,12,13]])
    
    plt.ion()
    
    fig = plt.figure()
    ax1 = fig.add_subplot(222)
    ax2 = fig.add_subplot(221)
    
    
    
    def update(mat1, mat2):
    
        ax1.plot(mat1.T, 'ro', color='red')
        ax2.plot(mat2.T, 'ro', color='blue')
        plt.show()
        plt.pause(1)
        ax1.clear()
        ax2.clear()
        
        ax1.plot(mat2.T, 'ro', color='blue')
        ax2.plot(mat1.T, 'ro', color='red')
        plt.show()
        plt.pause(1)
    
        ax1.clear()
        ax2.clear()
    
    for i in range(10):
        update(matrix1, matrix2)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多