我的 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
的值发生变化时,绘图不会更新
仅当我稍微调整应用程序窗口大小时它才会更新
除了 matplotlib 之外,还有其他方法可以添加图形吗?当我将其打包为 Android 版本时,Matplotlib 将无法工作,对吧?
最佳答案
我犯了一个相当愚蠢的错误。在 plot
函数中更新图形后,我实际上并没有调用 bar_canvas.draw()
方法
关于python - Kivy 使用 kivy.clock 更新 matplotlib 图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47282808/