The Wayback Machine - https://web.archive.org/web/20240611100244/https://www.geeksforgeeks.org/how-to-add-matplotlib-graph-in-kivy/
Open In App

How to add Matplotlib graph in Kivy ?

Last Updated : 05 Apr, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to add matplotlib graph in the kivy app.

Approach:

  • Import matplotlib pyplot
  • Import numpy
  • Import FigureCanvas KivyAgg
  • Import kivy app
  • Import kivy builder
  • Create App class
  • Return builder string
  • Run an instance of the class

Below is the Implementation.

Python3




# importing pyplot for graph plotting
from matplotlib import pyplot as plt
  
# importing numpy
import numpy as np
from kivy.garden.matplotlib import FigureCanvasKivyAgg
  
# importing kivyapp
from kivy.app import App
  
# importing kivy builder
from kivy.lang import Builder
  
  
# this is the main class which will 
# render the whole application
class uiApp(App):
  
    def build(self):
        self.str = Builder.load_string(""" 
  
BoxLayout:
    layout:layout
      
    BoxLayout:
      
        id:layout
      
                                """)
  
        signal = [7, 89.6, 45.-56.34]
  
        signal = np.array(signal)
          
        # this will plot the signal on graph
        plt.plot(signal)
          
        # setting x label
        plt.xlabel('Time(s)')
          
        # setting y label
        plt.ylabel('signal (norm)')
        plt.grid(True, color='lightgray')
          
        # adding plot to kivy boxlayout
        self.str.layout.add_widget(FigureCanvasKivyAgg(plt.gcf()))
        return self.str
  
  
# running the application
uiApp().run()


Output:

Note: When you run the below code this may throw the error given below

What you have to do is open the file given in the white box by clicking on file while holding ctrl key and comment line underlined by green color in that file and hit save now you will be able to run it!!



Similar Reads

Python | Add Label to a kivy window
Kivy is a platform-independent GUI tool in Python. As it can be run on Android, iOS, Linux and Windows, etc. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktop applications.Label widget - The Label widget is for rendering text. It supports ASCII and unicode strings. The label is the text
4 min read
Python | Add image widget in Kivy
Kivy is a platform-independent GUI tool in Python. As it can be run on Android, IOS, Linux and Windows, etc. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktop applications. 👉🏽 Kivy Tutorial - Learn Kivy with Examples. Image Widget: The Image widget is used to display an
4 min read
Python - Add audio files in kivy
Kivy is a platform independent GUI tool in Python. Kivy is a tool used to build cross-platform applications in Python which can run on android, IOS, Linux, Windows. Audio Widget: This module is used to load audio files in kivy. from kivy.core.audio import SoundLoader Below is the code on how you can import an audio file in kivy. NOTE:You can import
1 min read
Add image button using .kv file in kivy
Kivy is a platform independent GUI tool in Python. As it can be run on Android, IOS, linux and Windows etc. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktops applications.As we have discussed earlier that how to work with images and now in this we will gonna be learn how to use the ima
3 min read
How to add custom fonts in Kivy - Python?
Prerequisites: Kivy Tutorial Kivy is a platform-independent GUI tool in Python. It can run on Android, IOS, Linux and Windows, etc. This is the only GUI library from python which can independently run on the android device even we can use it on Raspberry pi also. It is an open-source Python library for the rapid development of multi-touch applicati
2 min read
How to add drag behavior in kivy widget ?
In this article, we will develop a GUI window using kivy framework of python, and we will add a Label having drag behavior on this window Note: You can follow the same pattern for implementing drag events on other widgets also. Approach: Actually what we are doing is we are using are just using the concept of inheritance, and we are making a new wi
2 min read
How to Add Markers to a Graph Plot in Matplotlib with Python?
In this article, we will learn how to add markers to a Graph Plot in Matplotlib with Python. For that just see some concepts that we will use in our work. Graph Plot: A plot is a graphical technique for representing a data set, usually as a graph showing the relationship between two or more variables.Markers: The markers are shown in graphs with di
2 min read
Python | Kivy .kv File
As when we write the application in Python kivy, to write all the things on the same code make a mess in the code and it is hard to understand that by someone another. Also writing a large code makes hard to maintain the construction of the widget tree and explicit the declaration of bindings.The KV language, allows us to create own widget tree in
3 min read
Introduction to Kivy ; A Cross-platform Python Framework
Kivy is an open-source, cross-platform Python framework used for developing multi-touch applications with a natural user interface. It allows developers to build applications that run on multiple platforms, including Windows, macOS, Linux, iOS, and Android. Kivy is based on the Model-View-Controller (MVC) architecture and uses a declarative languag
4 min read
Python | Ellipse (different polygons) in Kivy
Kivy is a platform independent GUI tool in Python. As it can be run on Android, IOS, linux and Windows, etc. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktops applications. Kivy Tutorial - Learn Kivy with Examples. Ellipse: Ellipse is a vertex canvas instruction. It allows drawing of a
4 min read
Article Tags :
Practice Tags :