21

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

4
  • 2
    I did read it. What is the problem? Title is relevant, I outlined the problem and added tags. Commented Jul 4, 2017 at 14:17
  • 1
    Stackoverflow is more targeted to specific questions like "why does this line produce an error" or "how can I turn the background of a kivy widget blue". You can save a png from matplotlib and display it with an image from kivy. Commented Jul 4, 2017 at 14:58
  • Oh I see, this was my second post here so I didn't know. Guess I'll have to look into animating the graph instead then. Commented Jul 4, 2017 at 15:01
  • Are there convenient solutions for interactive figures in Kivy after 4 years? Related question: stackoverflow.com/questions/68345620/… Commented Jul 13, 2021 at 12:17

1 Answer 1

38

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

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

But the problem is that it is not an interactive figure!!! it's just a picture with no activity to zoom or cursor!
matplotlib is not the right library for interactive figures IMHO. I would suggest to look into bokeh or plotly for a start. (not sure why so many !)
I wished to use an interactive figure in raspberry pi and touch screen do you still believe bokeh and plotly are better in touch screen and raspberry pi?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.