kivy-garden / garden.matplotlib

Matplotlib backends using kivy
MIT License
104 stars 50 forks source link

Please add this example: How to use in kv file #65

Open escallonia opened 4 years ago

escallonia commented 4 years ago

This example shows how to extend FigureCanvasKivyAgg for use in a kv file.

extend4kv.py:

#!/usr/bin/env python3                                                                
from kivy.garden.matplotlib.backend_kivyagg import FigureCanvasKivyAgg                
from kivy.app import App                                                              
import matplotlib.pyplot as plt                                                                                                                                             

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

# A class in which __init__ has no positional parameters, so it can be used in a kv file:
class MyFigure(FigureCanvasKivyAgg):                                                      
    def __init__(self, **kwargs): 
        super().__init__(plt.gcf(), **kwargs)    

class Extend4Kv(App):
    pass

Extend4Kv().run()

Now use in the file extend4kv.kv:

# kv file
MyFigure: