加速matplotlib interactive p

2024-09-27 19:21:13 发布

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

我的数据在一个python列表中,由numpy数组组成。我的想法是使用一个交互式绘图来查看这些数据,其中一个滑块处理列表中的元素。使用ipywidgets和matplotlib,其工作原理如下所示。缺点是地块刷新率高。由于matplotlib在更改滑块时绘制整个图形,因此速度非常慢。你知道吗

import matplotlib.pyplot as plt
from ipywidgets import interactive

def f(data_nr, layer_nr):    
    xmin = 0
    xmax = 3

    fig, axs = plt.subplots(2, 2,figsize=(10,10))
    axs[0,0].minorticks_on()

    axs[0,0].plot(data[data_nr][layer_nr])
    axs[0,0].set_xlim(xmin, xmax)
    axs[0,0].set_ylim(-10, 0)
    axs[0,0].set_xlabel('x unit')
    axs[0,0].set_ylabel('Quantity_1')
    axs[0,0].grid(True)

    axs[0,1].minorticks_on()
    axs[0,1].plot(data[data_nr][layer_nr*3+1])
    axs[0,1].set_xlim(xmin, xmax)
    axs[0,1].set_ylim(-10, 0)
    axs[0,1].set_xlabel('x unit')
    axs[0,1].set_ylabel('Quantity_2')
    axs[0,1].grid(True)

    axs[1,0].minorticks_on()
    axs[1,0].plot(data[data_nr][layer_nr*3+2])
    axs[1,0].set_xlim(xmin, xmax)
    axs[1,0].set_ylim(-10, 0)
    axs[1,0].set_xlabel('x unit')
    axs[1,0].set_ylabel('Quantity_3')
    axs[1,0].grid(True)

    axs[1,1].minorticks_on()
    axs[1,1].plot(data[data_nr][layer_nr*3+3])
    axs[1,1].set_xlim(xmin, xmax)
    axs[1,1].set_ylim(-10, 0)
    axs[1,1].set_xlabel('x unit')
    axs[1,1].set_ylabel('Quantity_4')
    axs[1,1].grid(True)

    fig.tight_layout()

    plt.show()   

interactive_plot = interactive(f, data_nr=(0, 10,1), layer_nr=(1, 10, 1))
output = interactive_plot.children[-1]
interactive_plot

现在我了解到,只使用draw\u artistblit重新绘制数据可以提高绘图速度,但老实说,我不明白如何在上面的示例中同时应用这两种方法。Mybe值得注意的是,我使用的是jupyter笔记本。 有人能告诉我如何更改上面的代码以提高刷新率吗? 谢谢


Tags: layerdataplotonunitnrinteractivexmin

热门问题