19

I want to use matplotlib instead kivy_garden.graph. Actually, I tried this code to check if it works for me. I've had some problems with installing matplotlib but I have successfully(or not) done that.

When I started the code I got from matplotlib import _png ImportError: cannot import name '_png' from 'matplotlib' (D:\PyCharmProjects\kivyApp\venv\lib\site-packages\matplotlib\__init__.py) I reinstalled matplotlib and pip, tried another version of matplotlib and I don't know why it is not working for me. I have Python 3.7.5, pip 20.2.4 and matplotlib 3.3.3

Sylvaus
  • 844
  • 6
  • 13
WASP-21b
  • 307
  • 1
  • 2
  • 7
  • it looks like your project is installed within a virtual environment. you should activate that virtual environment before installing matplotlib. the instructions for that are specific to your OS and specific to the particular shell be it cmd.exe or powershell. – Mark Dec 11 '22 at 18:19

9 Answers9

29

Reverting to matplotlib version 3.0.2 didn't work for me, but with 3.1.3 it did.

python -m pip uninstall matplotlib
pip install matplotlib==3.1.3

Python 3.8.2

Diego BM
  • 409
  • 4
  • 5
  • 3
    What kind of solution is this? A proper answer requires a solution that works for the most current stable version of matplotlib. – Martin Jul 13 '21 at 12:34
6

I was having this problem in Google Colab and couldn't solve it. The simple solution that I found was to install the stable version that is pip install -U matplotlib and restarted the runtime and it worked.

S.B
  • 13,077
  • 10
  • 22
  • 49
2

I'm also facing this issue in Google Colab

try this pip install matplotlib==3.1.3 work for me in Google Colab

Zeeshan Ch
  • 21
  • 2
1

It's work now. I executed py -m pip uninstall matplotlib and after py -m pip install matplotlib --version=3.0.2 from terminal in PyCharm. The same commands in cmd and git bash didn't work.

WASP-21b
  • 307
  • 1
  • 2
  • 7
1

Just import matplotlib library

pip install matplotlib==3.1.3

and, this version

Akash Kumar
  • 540
  • 2
  • 4
  • 15
0

If you are using a linux distribution the problem is in the installation of matplotlib.

uninstall the current version: pip uninstall matplotlib

The correct installation can be found at: https://matplotlib.org/stable/users/installing.html

Choose the correct code for your distribution from below:

Linux package manager If you are using the Python version that comes with your Linux distribution, you can install Matplotlib via your package manager, e.g.:

Debian / Ubuntu: sudo apt-get install python3-matplotlib

Fedora: sudo dnf install python3-matplotlib

Red Hat: sudo yum install python3-matplotlib

Arch: sudo pacman -S python-matplotlib

after this it should work fine.

0

You also can coment line where this import is :) (for me it is C:\Users\{username}\.kivy\garden\garden.matplotlib\backend_kivy.py , line 256) But i dont know what problems it can bring in the future.

User
  • 1
  • That seems like an unsafe hack to me, though still an interesting one to document. Does your code using this library still work after having done this? – joanis Sep 29 '21 at 12:59
0

Since this bug remains for so many years I gave up waiting for the kivy-garden community to fix it. So when deploying my kivy-based software, I also distribute the following quick-and-dirty fix. It finds the file and comments out that line automatically.

from pathlib import Path
# Join user's home directory with Garden's file path
file = Path.home().joinpath('.kivy/garden/garden.matplotlib/backend_kivy.py')
# Read file
lines = []
with open(file, 'r') as f:
    for line in f:
        lines.append(line)
# Write file
with open(file, 'w') as f:
    for line in lines:
        if line.startswith('from matplotlib import _png'):
            # comment out this troublesome line
            line = '#' + line
        f.write(line)
Martin
  • 1,395
  • 1
  • 11
  • 33
0

I had this issue and can confirm that upgrading to the most recent matplotlib version fixed the issue:

pip install --upgrade matplotlib
pip show matplotlib

Name: matplotlib
Version: 3.6.2
Summary: Python plotting package
Home-page: https://matplotlib.org
Author: John D. Hunter, Michael Droettboom
Author-email: matplotlib-users@python.org
License: PSF
Location: /usr/local/lib/python3.8/dist-packages
Requires: python-dateutil, pyparsing, pillow, numpy, fonttools, cycler, contourpy, kiwisolver, packaging
Required-by: yellowbrick, wordcloud, weightwatcher, seaborn, scikit-image, pycocotools, prophet, powerlaw, plotnine, pandas-profiling, mlxtend, mizani, missingno, matplotlib-venn, keras-vis, imgaug, fastai, descartes, datascience, daft, arviz
James Hirschorn
  • 7,032
  • 5
  • 45
  • 53