如何解决Kivy结合Matplotlib时出现的KeyError: 'kivy.garden.matplotlib'?
Hey there, that KeyError pops up when Python can't locate the kivy.garden.matplotlib module, which almost always boils down to the component not being installed properly or its path not being recognized by your environment. Let's walk through the fixes step by step:
1. Ensure Kivy Garden and Matplotlib Component are Installed
First, make sure you have the Kivy Garden tool set up, then install the matplotlib garden component:
- If you haven't installed Kivy Garden yet, run this in your terminal (make sure you're in your virtual environment if you're using one):
pip install kivy-garden - Then install the matplotlib component via Garden:
This will download the component to yourgarden install matplotlib.kivy/gardendirectory (usually in your home folder).
2. Verify and Add the Garden Path to Python's sys.path
Sometimes Python doesn't automatically pick up the Kivy Garden directory. You can manually add it at the start of your script to fix the import issue:
import sys from kivy.garden import garden # Add the garden directory to Python's path sys.path.append(garden.garden_dir) # Now try importing again from kivy.garden.matplotlib.backend_kivyagg import FigureCanvasKivyAgg
If that doesn't work, you can directly add the full path to the matplotlib garden folder. For example, if it's installed at /root/.kivy/garden/matplotlib, add this:
sys.path.append("/root/.kivy/garden/matplotlib")
3. Check PyCharm's Interpreter and Path Configuration
Since you're using PyCharm:
- Make sure your project is using the correct virtual environment (the one where you installed Kivy and Garden). Go to
File > Settings > Project: [Your Project] > Python Interpreterto confirm. - If PyCharm still can't find the module, add the garden directory to your project's path manually:
- Right-click your project in the Project Explorer >
Open Module Settings - Go to
Libraries> Click the+button > SelectPython Library - Navigate to your
.kivy/garden/matplotlibfolder and add it
- Right-click your project in the Project Explorer >
4. Alternative: Use the Direct PyPI Package
If the Garden approach keeps failing, you can try installing the matplotlib integration directly via pip:
pip install kivy-garden-matplotlib
Then your import should work without extra path adjustments.
Here's the error traceback you shared for reference:
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'
内容的提问来源于stack exchange,提问作者Yash Mathur

