如何摆脱KeyError:"kivy.garden.matplotlib"?



当我运行我的文件时,我正在使用matplotlib with kivy,我收到了这个错误,任何人都可以提出建议吗。

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'

使用这些它帮助挖掘的错误命令from kivy.garden.matplotlib:

python 3.7使用anaconda

  1. pip安装kivy
  2. pip安装kivy花园
  3. 花园安装matplotlib
  4. pip安装matplotlib==2.2.2

如果我是你,我会克隆"https://github.com/kivy-garden/garden.matplotlib"到";virt(带有虚拟环境的目录(/lib/python/kivy/garden/";"git克隆https://github.com/kivy-garden/garden.matplotlib"并将目录重命名为";garden.matplotlib";到";matplotlib";

Kivy中有一种显示任何matplotlib图的替代方法。使用类似于的python自模块io((

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