python - Kivy 使用 kivy.clock 更新 matplotlib 图

标签 python matplotlib kivy

我的 kivy 应用程序中有一个 matplotlib 图表,我想用流数据每秒更新一次。到目前为止,我的行为有点奇怪。它确实会更新,但仅当我稍微调整窗口大小时

fig, ax = plt.subplots()
width = 0.65
ax.set_title('Scores by group and gender')
bar_canvas = fig.canvas

class Test(BoxLayout):
    catA = NumericProperty(0)
    catB = NumericProperty(0)
    catC = NumericProperty(0)

    #plot the bar graph
    def plot(self, dt):
        catA_rects = ax.bar(5, self.catA, width, color='g')
        catB_rects = ax.bar(6, self.catB, width, color='#808080')
        catC_rects = ax.bar(7, self.catC, width, color='r')
        ax.legend((catA_rects[0], catB_rects[0], catC_rects[0]), ('Category A', 'Category B', 'Category C'))


    #update the graph
    def update(self):
        Clock.schedule_interval(self.plot,1)

    def classify_sample(self, test_sample):
        result_prob = classifier.predict_proba(test_sample)[0]
        result = classifier.predict(test_sample)[0]

        if result_prob.max() > 0.55:
            if result == 1:
                self.catA += 1

            else:
                self.catC += 1

        else:
            self.catB += 1



class ClassificationApp(App):
    def build(self):
        res = Test()
        res.bar_tab.add_widget(bar_canvas)
        res.update()
        return res


if __name__ == '__main__':
    ClassificationApp().run()

在“kv”文件中,我调用classify_sample来显示RecycleView View 类中的结果

Label:
    text: app.root.classify_sample(data)

我正在使用 Clock.schedule_interval(self.plot,1) 更新 plot 函数,当我在屏幕上打印输出时,该函数正在工作,但当 catA、catB 或 catC 的值发生变化时,绘图不会更新 仅当我稍微调整应用程序窗口大小时它才会更新 enter image description here

enter image description here

enter image description here

除了 matplotlib 之外,还有其他方法可以添加图形吗?当我将其打包为 Android 版本时,Matplotlib 将无法工作,对吧?

最佳答案

我犯了一个相当愚蠢的错误。在 plot 函数中更新图形后,我实际上并没有调用 bar_canvas.draw() 方法

关于python - Kivy 使用 kivy.clock 更新 matplotlib 图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47282808/

相关文章:

python - Seaborn中如何通过log对x轴和y轴进行等比例缩放?

python - 如何使用 matplotlib 插值并创建更好的等高线图?

python - 从 Python 的 Bokeh 到 Latex 的交互式 HTML 绘图

python - 在kivy中制作切换到不同屏幕的按钮 ListView

python - Kivy - 如何获取 TextInput 的内容

python - 强制更新 kivy 中的 GUI

python - 具有连接信息的 VTK 着色管过滤器

python - 为什么对 numpy 和 python list 进行相同的操作会得到不同的结果?

python - sql python 上查询字符串的 WHERE 动态绑定(bind)

python - 无法将 numpy 数组转换为 JSON