首页 > 解决方案 > 数据计算可视化的Kivy显示速度

问题描述

对于一个大学项目,我需要创建一个应用程序来显示包含很多点(100,000 到 1,000,000 个点)的交互式绘图。

所以我必须处理数据然后显示它。目前构建与kivy / kivy garden的界面,我正在测试计算时间和显示速度。为此,我只需在刷新图形时计算一个 for 循环。

问题是在 500,000 点的情况下,显示时间约为 8-9 秒,这是相当高的,因为缺少缩放、移动图形或一些用户输入和一些计算的工具!您有什么建议或者您知道如何优化以下代码以加快应用程序的速度吗?

这是代码:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen

from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.garden.matplotlib.backend_kivyagg import FigureCanvasKivyAgg
import matplotlib.pyplot as plt
import numpy as np
import time


from kivy.core.window import Window 
Window.clearcolor = (1, 1, 1, 1) 
Window.size = (960, 540) 


kv = """
#:import SlideTransition kivy.uix.screenmanager.SlideTransition

<Manager>:
    transition: SlideTransition()

    StartPage:
        name: 'StartPage'

    SecondPage:
        name: 'SecondPage'

<StartPage>:

    canvas:

        Color:
            rgba: 0.1, 0.1, 1, 0.7

        Rectangle:
            pos: 0, 11*root.height/12
            size: root.width, root.height/12

        Rectangle:
            pos: 0, 0
            size: root.width, root.height/12


    BoxLayout:
        orientation: 'vertical'
        size: root.width, root.height


    Button:
        size_hint: 0.15, 0.08
        pos_hint: {'left': 0, 'top': 1 }
        text: 'Go to Second Page'
        on_release:
            root.manager.current = 'SecondPage'

    Button:
        size_hint: 0.15, 0.08
        pos_hint: {'right': 1, 'bottom': 1 }
        text: 'Quit'
        on_release: app.stop()


    Label:
        text: 'Start Screen'
        font_size: '20sp'
        color: 0,0,0,1
        pos_hint:{'x': 0.42, 'y': 0.46 }



<SecondPage>:
    box: box

    canvas.before:
        Color:
            rgba: 1, 1, 1, 1
        Rectangle:
            pos: self.pos
            size: self.size

    BoxLayout:
        id: box
        size_hint: 0.8, 0.75
        pos_hint:{'x': 0, 'y': 0.1 }

    Button:
        text:'Start Menu'
        font_size: 20
        bold: True
        size_hint: 0.15, 0.08
        pos_hint: {'left': 0, 'top': 1 }
        color: 1, 1, 1, 1
        on_press: root.manager.current = 'StartPage'

    Button:
        text:'Update'
        font_size: 20
        bold: True
        size_hint: 0.15, 0.08
        pos_hint: {'x':0.8, 'y': 0.5 }
        color: 1, 1, 1, 1
        on_press: root.Update()
"""

Builder.load_string(kv)

# Start Page
class StartPage(Screen):      
    pass

#Second Page
class SecondPage(Screen):
    box = ObjectProperty(None)


    def add_plot(self, N):

        phase = np.random.normal(-np.pi/2, +np.pi/2)
        noise = np.random.normal(0, 1, N)
        nbr = np.linspace(0,1,N)
        func = 0.01*noise+np.sin(nbr/0.05+phase)

        plt.plot(nbr,func,label='Line1',color='r')
        plt.ylabel('Sinus')
        plt.xlabel('Range')
        plt.grid(True)
        self.fig1 = plt.gcf()

        return self.fig1

    def on_pre_enter(self, *args):
        plt.cla()
        self.box.clear_widgets()
        self.fig1 = SecondPage.add_plot(self,10)
        self.box.add_widget(FigureCanvasKivyAgg(self.fig1))

    def Update(self):
        start = time.process_time()
        N = int(2**19)
        # test = 0.0
        for i in range(N):
            test = i/10+np.cos(i/(i+1)*2)

        print("Number of points in the plot : ",N)
        print("boucle for : ",test)
        print("\nElapsed Time : ", time.process_time() - start)
        plt.cla()
        self.box.clear_widgets()
        self.fig1 = SecondPage.add_plot(self,N)
        self.box.add_widget(FigureCanvasKivyAgg(self.fig1))


class Manager(ScreenManager):
    pass


class MyNewApp(App):
    title = "Matplolib - plot Update"

    def build(self):
        return Manager()    

MyNewApp().run()

非常感谢 !

标签: pythonperformancematplotlibplotkivy

解决方案


推荐阅读