Yash Mathur
Yash Mathur

Reputation: 111

how to get rid of KeyError: 'kivy.garden.matplotlib'?

i am using matplotlib with kivy when i am running my file i am getting this error can anyone suggest something.

Traceback (most recent call last):
   File "<input>", line 1, in <module>
   File "/root/pycharm-2019.3.3/plugins/python/helpers/pydev/_pydev_bundle/pydev_umd.py", line 197, in runfile
     pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
   File "/root/pycharm-2019.3.3/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
     exec(compile(contents+"\n", file, 'exec'), glob, loc)
   File "/root/PycharmProjects/vsts/venv/main.py", line 17, in <module>
     from kivy.garden.matplotlib.backend_kivyagg import FigureCanvasKivyAgg
   File "/root/pycharm-2019.3.3/plugins/python/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
     module = self._system_import(name, *args, **kwargs)
   File "<frozen importlib._bootstrap>", line 983, in _find_and_load
   File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
   File "<frozen importlib._bootstrap>", line 668, in _load_unlocked
   File "<frozen importlib._bootstrap>", line 640, in _load_backward_compatible
 KeyError: 'kivy.garden.matplotlib'

Upvotes: 1

Views: 5223

Answers (3)

berk berk
berk berk

Reputation: 42

There is altarnative way to display any matplotlib graph in Kivy. Use python self module io() like,

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.lang.builder import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.image import Image,CoreImage      #for create a image with coreimage

import io   #I used io it's simple and useful!

import matplotlib.pyplot as plt   #need matplotlib library

kivy=Builder.load_string("""

<Image_Example>:

    BoxLayout:
        orientation:'vertical'
   
        Button:
            id:click
            text:'display image'
            on_press:root.display_img()  #when it click display image
        Label:
            text:'That is your image'

        Image:
            id:img
            source:''  #image source
           
            allow_stretch:True
           
            keep_ratio:True

""")

class Image_Example(BoxLayout):

    def display_img(self):
        X=[ 1, 2, 3, 4, 5]
        Y= [5, 14, 7, 20, 11]  #your dataset
       
        plt.plot(X,Y)  #craete yourdataset graph
        plt.title("title here")
        plt.xlabel("x-axis label here)")
        plt.ylabel("y-axis label here")
       
        buf=io.BytesIO()
        plt.savefig(buf)  #save your dataset to IoByte
       
        buf.seek(0) #seek your graph
        self.ids.img.texture=CoreImage(buf, ext="png").texture     #display your image
        self.ids.img.reload()
        del buf #then delete created buf

class ImageApp(App):
    def build(self):
        return Image_Example()
       

kv=ImageApp().run()    

https://groups.google.com/g/kivy-users/c/Z5RC-j2r3pU/m/1fD1h6BLAwAJ?pli=1

Upvotes: 0

Anatol
Anatol

Reputation: 1

If I were you, I would clone "https://github.com/kivy-garden/garden.matplotlib" to the "virt(directory with your virtual environment)/lib/python/kivy/garden/" "git clone https://github.com/kivy-garden/garden.matplotlib" and rename the directory "garden.matplotlib" to the "matplotlib"

Upvotes: 0

Neda
Neda

Reputation: 77

use these commands it helped mine that had error for from kivy.garden.matplotlib :

python 3.7 using anaconda

  1. pip install kivy
  2. pip install kivy-garden
  3. garden install matplotlib
  4. pip install matplotlib==2.2.2

Upvotes: 1

Related Questions