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()
		
			

John Elder

John is the CEO of Codemy.com where he teaches over 100,000 students how to code! He founded one of the Internet's earliest advertising networks and sold it to a publicly company at the height of the first dot com boom. After that he developed the award-winning Submission-Spider search engine submission software that's been used by over 3 million individuals, businesses, and governments in over 42 countries. He's written several Amazon #1 best selling books on coding, and runs a popular Youtube coding channel.

View all posts

2 comments

Leave a Reply to Mahmoud WATT Cancel reply

Your email address will not be published. Required fields are marked *

  • 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 ?

John Elder