如何在 kivy 中开始/使用 matplotlib

2022-01-15 00:00:00 python kivy matplotlib

问题描述

我最近学习了一点 matplotlib,并想在 kivy 中使用它.我在这里和那里阅读了一些关于花园的文件,但并不真正理解它.我已经安装了 kivy garden 和 matplotlib 但不知道如何从这里开始.我只是想将一个已经完成的 matplotlib 图添加到 kivy 中.我将不胜感激逐步简化的说明,说明如何将我已经编码到 kivy 中的内容并显示出来.谢谢

解决方案

这是 kivy-garden matplotlib 和 kivy 的最简单示例.如果您想做更高级的事情,请查看他们的示例:

I have recently learned a bit of matplotlib and would like to use it within kivy. I have read a little documentation on the garden here and there but don't really understand it. I have installed kivy garden and matplotlib but don't know how to proceed from here. I simply want to add a already completed matplotlib graph into kivy. I would appreciate a step by step simplified set of instructions of how to get what I already coded into kivy and get it to display. Thanks

解决方案

Here is the simplest example possible for kivy-garden matplotlib and kivy. If you would like to do more advanced things, check out their examples: https://github.com/kivy-garden/garden.matplotlib/tree/master/examples I think it should be enough to get you started with your plot.

Below I am adding it to a BoxLayout, you can add more widgets to this BoxLayout or add this BoxLayout somewhere else.

python code example.py:

from kivy.garden.matplotlib.backend_kivyagg import FigureCanvasKivyAgg
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
import matplotlib.pyplot as plt

plt.plot([1, 23, 2, 4])
plt.ylabel('some numbers')

class MyApp(App):

    def build(self):
        box = BoxLayout()
        box.add_widget(FigureCanvasKivyAgg(plt.gcf()))
        return box

MyApp().run()

相关文章