且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 Python 中使用 matplotlib 和 kivy 进行实时绘图

更新时间:2023-02-26 16:54:54

根据@ImportanceOfBeingErnest 的建议,需要更新坐标区限制.

As suggested by @ImportanceOfBeingErnest, the axes limits need to be updated.

以下代码应该可以解决您的问题:

The following code should solve your problem:

import matplotlib
matplotlib.use('module://kivy.garden.matplotlib.backend_kivy')
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import Clock
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
canvas = fig.canvas


class MyApp(App):
    def build(self):
        box = BoxLayout()
        self.i = 0
        self.line = [self.i]
        box.add_widget(canvas)
        plt.show()
        Clock.schedule_interval(self.update, 1)
        return box

    def update(self, *args):
        plt.plot(self.line, self.line)
        self.i += 1
        self.line.append(self.i)
        canvas.draw_idle()


MyApp().run()