In this video we’ll add a MatplotLib Graph to our Kivy App.
Adding matplotlib to a kivy app is pretty easy, we’ll use kivy_garden and it’ll be a piece of cake!
#kivy #codemy
Python Code: matty.py
GitHub Code: matty.py
from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.uix.floatlayout import FloatLayout
from kivy.garden.matplotlib.backend_kivyagg import FigureCanvasKivyAgg
import matplotlib.pyplot as plt
# Define what we want to graph
x = [1,2,3,4,5]
y = [5, 12, 6, 9, 15]
plt.plot(x,y)
plt.ylabel("This is MY Y Axis")
plt.xlabel("X Axis")
class Matty(FloatLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
box = self.ids.box
box.add_widget(FigureCanvasKivyAgg(plt.gcf()))
def save_it(self):
name = self.ids.namer.text
if name:
plt.savefig(name)
class MainApp(MDApp):
def build(self):
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = "BlueGray"
Builder.load_file('matty.kv')
return Matty()
MainApp().run()
Kivy Design Code: matty.kv
GitHub Code: matty.kv
<Matty>
BoxLayout:
id: box
size_hint_y: .8
pos_hint: {"top":1}
BoxLayout:
size_hint_y: .2
TextInput:
id: namer
multiline: False
Button:
text: "Save It!!"
on_release: root.save_it()

Hello John ! How are you ? Thanks, you helped me so much with your videos in kivy.
I have a question : Can we use Matplotlib, drawnow, arduino and kivy together ?
I was trying to read serial data from the arduino and plot it with kivy (real time plotting).
I have been trying a lot of times but it didn’t work. Please could you help me ?
I don’t see why not