Matplotlib ошибка при импорте

Contents

Obtaining Matplotlib version#

To find out your Matplotlib version number, import it and print the
__version__ attribute:

>>> import matplotlib
>>> matplotlib.__version__
'0.98.0'

matplotlib install location#

You can find what directory Matplotlib is installed in by importing it
and printing the __file__ attribute:

>>> import matplotlib
>>> matplotlib.__file__
'/home/jdhunter/dev/lib64/python2.5/site-packages/matplotlib/__init__.pyc'

matplotlib configuration and cache directory locations#

Each user has a Matplotlib configuration directory which may contain a
matplotlibrc file. To
locate your matplotlib/ configuration directory, use
matplotlib.get_configdir():

>>> import matplotlib as mpl
>>> mpl.get_configdir()
'/home/darren/.config/matplotlib'

On Unix-like systems, this directory is generally located in your
HOME directory under the .config/ directory.

In addition, users have a cache directory. On Unix-like systems, this is
separate from the configuration directory by default. To locate your
.cache/ directory, use matplotlib.get_cachedir():

>>> import matplotlib as mpl
>>> mpl.get_cachedir()
'/home/darren/.cache/matplotlib'

On Windows, both the config directory and the cache directory are
the same and are in your Documents and Settings or Users
directory by default:

>>> import matplotlib as mpl
>>> mpl.get_configdir()
'C:\Documents and Settings\jdhunter\.matplotlib'
>>> mpl.get_cachedir()
'C:\Documents and Settings\jdhunter\.matplotlib'

If you would like to use a different configuration directory, you can
do so by specifying the location in your MPLCONFIGDIR
environment variable — see
Setting environment variables in Linux and macOS. Note that
MPLCONFIGDIR sets the location of both the configuration
directory and the cache directory.

Getting help#

There are a number of good resources for getting help with Matplotlib.
There is a good chance your question has already been asked:

  • The mailing list archive.

  • GitHub issues.

  • Stackoverflow questions tagged matplotlib.

If you are unable to find an answer to your question through search, please
provide the following information in your e-mail to the mailing list:

  • Your operating system (Linux/Unix users: post the output of uname -a).

  • Matplotlib version:

    python -c "import matplotlib; print(matplotlib.__version__)"
    
  • Where you obtained Matplotlib (e.g., your Linux distribution’s packages,
    GitHub, PyPI, or Anaconda).

  • Any customizations to your matplotlibrc file (see
    Customizing Matplotlib with style sheets and rcParams).

  • If the problem is reproducible, please try to provide a minimal, standalone
    Python script that demonstrates the problem. This is the critical step.
    If you can’t post a piece of code that we can run and reproduce your error,
    the chances of getting help are significantly diminished. Very often, the
    mere act of trying to minimize your code to the smallest bit that produces
    the error will help you find a bug in your code that is causing the
    problem.

  • Matplotlib provides debugging information through the logging library, and
    a helper function to set the logging level: one can call

    plt.set_loglevel("info")  # or "debug" for more info
    

    to obtain this debugging information.

    Standard functions from the logging module are also applicable; e.g. one
    could call logging.basicConfig(level="DEBUG") even before importing
    Matplotlib (this is in particular necessary to get the logging info emitted
    during Matplotlib’s import), or attach a custom handler to the «matplotlib»
    logger. This may be useful if you use a custom logging configuration.

If you compiled Matplotlib yourself, please also provide:

  • any changes you have made to setup.py or setupext.py.

  • the output of:

    rm -rf build
    python setup.py build
    

    The beginning of the build output contains lots of details about your
    platform that are useful for the Matplotlib developers to diagnose your
    problem.

  • your compiler version — e.g., gcc --version.

Including this information in your first e-mail to the mailing list
will save a lot of time.

You will likely get a faster response writing to the mailing list than
filing a bug in the bug tracker. Most developers check the bug
tracker only periodically. If your problem has been determined to be
a bug and can not be quickly solved, you may be asked to file a bug in
the tracker so the issue doesn’t get lost.

Problems with recent git versions#

First make sure you have a clean build and install (see How to completely remove Matplotlib),
get the latest git update, install it and run a simple test script in debug
mode:

rm -rf /path/to/site-packages/matplotlib*
git clean -xdf
git pull
python -m pip install -v . > build.out
python -c "from pylab import *; set_loglevel('debug'); plot(); show()" > run.out

and post build.out and run.out to the matplotlib-devel
mailing list (please do not post git problems to the users list).

Of course, you will want to clearly describe your problem, what you
are expecting and what you are getting, but often a clean build and
install will help. See also Getting help.

Bug report

Bug summary
I am unable to import matplotlib.pyplot starting in version 3.0.0. Might be related to Issue #9485?

Code for reproduction

import matplotlib.pyplot as plt

Actual outcome

I get the following traceback when executing the above import

Traceback (most recent call last):
  File "C:PythonPython35libsite-packagesmatplotlibfont_manager.py", line 1353, in <module>
    fontManager = json_load(_fmcache)
  File "C:PythonPython35libsite-packagesmatplotlibfont_manager.py", line 888, in json_load
    with open(filename, 'r') as fh:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\Users\username\.matplotlib\fontlist-v300.json'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    import matplotlib.pyplot as plt
  File "C:PythonPython35libsite-packagesmatplotlibpyplot.py", line 32, in <module>
    import matplotlib.colorbar
  File "C:PythonPython35libsite-packagesmatplotlibcolorbar.py", line 32, in <module>
    import matplotlib.contour as contour
  File "C:PythonPython35libsite-packagesmatplotlibcontour.py", line 18, in <module>
    import matplotlib.font_manager as font_manager
  File "C:PythonPython35libsite-packagesmatplotlibfont_manager.py", line 1363, in <module>
    _rebuild()
  File "C:PythonPython35libsite-packagesmatplotlibfont_manager.py", line 1344, in _rebuild
    fontManager = FontManager()
  File "C:PythonPython35libsite-packagesmatplotlibfont_manager.py", line 978, in __init__
    ttffiles = findSystemFonts(paths) + findSystemFonts()
  File "C:PythonPython35libsite-packagesmatplotlibfont_manager.py", line 264, in findSystemFonts
    fontfiles.update(win32InstalledFonts(fontext=fontext))
TypeError: 'NoneType' object is not iterable

Matplotlib version

  • Operating system: Windows 10 64-bit
  • Matplotlib version: 3.0.0
  • Matplotlib backend: TkAgg
  • Python version: 3.5.4
  • Jupyter version (if applicable): NA
  • Other libraries: NA

Installed via pip

In today’s post i would like to provide some troubleshooting information for people installing the matplotlib library package in their computers.

ModuleNotFoundError: no module named ‘matplotlib’ pyplot

This error is thrown when Python can’t find the Matplotlib package in your development environment. Most probably you’ll be able to troubleshoot this error by downloading the library from the Python repository using the PIP utility.

Install matplotlib with pip

In order to troubleshoot this error follow this process (explained for Windows, might require minor adjustments for MAC and Linux). Note that the process is identical for most Python add on libraries / packages such as Seaborn, NumPy, Pandas and many others.

  • Save your work, and close your Jupyter Notebooks/Lab, Spyder or other development environment you might be using.
  • Open your Command Prompt.
  • Type cmd.
  • Then in the command prompt type, type the following command:
pip install matplotlib
  • Now, go ahead and hit Enter.
  • Python will download the latest version of the matplotlib library from the Python package repository.
  • Once done, open your development environment and import matplotlib.
  • That’s it!

Anaconda environments

If you are have installed Anaconda, then by default, the basic Data Analysis packages, including Pandas and Matplotlib are already installed in your base environment. If for some reason you still receive an import error proceed as following.

  • From the Start mane, open Anaconda Navigator.
  • On the left hand side, hit the Environments tab (step 1)
  • Then highlight your Anaconda environment (step 2).
  • Then in the dropdown box (step 3), select Not Installed.
  • In the Search box, type matplotlib and hit Enter.
  • Then check the box near the package and hit Apply.
  • Then in the Install Packages dialog, click Apply again.
  • Once Anaconda Navigator is done, close it and reopen your development environment and download matplotlib.

Note: for completeness, you can also update your Anaconda environment manually (this is specially useful if you use MiniConda.

  • From the Windows Start Menu open the Anaconda Prompt
  • Type the following command
conda activate <path_to_your_environment>
  • Hit Enter.
  • Now type
conda install matplotlib
  • Hit Enter.
  • Matplotlib will be downloaded and installed.
  • That’s it.

Importerror no module named matplotlib.pyplot / matplotlib.path

This error is the base class of the ModuleNotFound error, and available since Python 3.3. The troubleshooting process we just described will remedy this error as well.

Questions? Feel free to leave us a comment

Ezoic

A common error you may encounter when using Python is modulenotfounderror: no module named ‘matplotlib’. This error occurs when Python cannot detect the Matplotlib library in your current environment. This tutorial goes through the exact steps to troubleshoot this error for the Windows, Mac and Linux operating systems.

Table of contents

  • ModuleNotFoundError: no module named ‘matplotlib’
    • What is ModuleNotFoundError?
    • What is Matplotlib?
    • How to Install Matplotlib on Windows Operating System
    • How to Install Matplotlib on Mac Operating System
    • How to Install Matplotlib on Linux Operating Systems
      • Installing pip for Ubuntu, Debian, and Linux Mint
      • Installing pip for CentOS 8 (and newer), Fedora, and Red Hat
      • Installing pip for CentOS 6 and 7, and older versions of Red Hat
      • Installing pip for Arch Linux and Manjaro
      • Installing pip for OpenSUSE
    • Check Matplotlib Version
    • Installing Matplotlib Using Anaconda
    • Importing matplotlib.pyplot
  • Summary

ModuleNotFoundError: no module named ‘matplotlib’

What is ModuleNotFoundError?

The ModuleNotFoundError occurs when the module you want to use is not present in your Python environment. There are several causes of the modulenotfounderror:

The module’s name is incorrect, in which case you have to check the name of the module you tried to import. Let’s try to import the re module with a double e to see what happens:

import ree
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
1 import ree

ModuleNotFoundError: No module named 'ree'

To solve this error, ensure the module name is correct. Let’s look at the revised code:

import re

print(re.__version__)
2.2.1

You may want to import a local module file, but the module is not in the same directory. Let’s look at an example package with a script and a local module to import. Let’s look at the following steps to perform from your terminal:

mkdir example_package

cd example_package

mkdir folder_1

cd folder_1

vi module.py

Note that we use Vim to create the module.py file in this example. You can use your preferred file editor, such as Emacs or Atom. In module.py, we will import the re module and define a simple function that prints the re version:

import re

def print_re_version():

    print(re.__version__)

Close the module.py, then complete the following commands from your terminal:

cd ../

vi script.py

Inside script.py, we will try to import the module we created.

import module

if __name__ == '__main__':

    mod.print_re_version()

Let’s run python script.py from the terminal to see what happens:

Traceback (most recent call last):
  File "script.py", line 1, in <module>
    import module
ModuleNotFoundError: No module named 'module'

To solve this error, we need to point to the correct path to module.py, which is inside folder_1. Let’s look at the revised code:

import folder_1.module as mod

if __name__ == '__main__':

    mod.print_re_version()

When we run python script.py, we will get the following result:

2.2.1

Lastly, you can encounter the modulenotfounderror when you import a module that is not installed in your Python environment.

What is Matplotlib?

Matplotlib is a data visualization and graphical plotting library for Python. Matplotlib is an open-source alternative to MATLAB. Pyplot is a Matplotlib module, which provides a MATLAB-like interface. You can use pyplot to create various plots, including line, histogram, scatter, 3D, image, contour, and polar.

The simplest way to install Matplotlib is to use the package manager for Python called pip. The following installation instructions are for the major Python version 3.

How to Install Matplotlib on Windows Operating System

First, you need to download and install Python on your PC. Ensure you select the install launcher for all users and Add Python to PATH checkboxes. The latter ensures the interpreter is in the execution path. Pip is automatically on Windows for Python versions 2.7.9+ and 3.4+.

You can check your Python version with the following command:

python3 --version

You can install pip on Windows by downloading the installation package, opening the command line and launching the installer. You can install pip via the CMD prompt by running the following command.

python get-pip.py

You may need to run the command prompt as administrator. Check whether the installation has been successful by typing.

pip --version

To install matplotlib with pip, run the following command from the command prompt.

pip3 install matplotlib

How to Install Matplotlib on Mac Operating System

Open a terminal by pressing command (⌘) + Space Bar to open the Spotlight search. Type in terminal and press enter. To get pip, first ensure you have installed Python3:

python3 --version
Python 3.8.8

Download pip by running the following curl command:

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

The curl command allows you to specify a direct download link. Using the -o option sets the name of the downloaded file.

Install pip by running:

python3 get-pip.py

From the terminal, use pip3 to install Matplotlib:

pip3 install matplotlib

How to Install Matplotlib on Linux Operating Systems

All major Linux distributions have Python installed by default. However, you will need to install pip. You can install pip from the terminal, but the installation instructions depend on the Linux distribution you are using. You will need root privileges to install pip. Open a terminal and use the commands relevant to your Linux distribution to install pip.

Installing pip for Ubuntu, Debian, and Linux Mint

sudo apt install python-pip3

Installing pip for CentOS 8 (and newer), Fedora, and Red Hat

sudo dnf install python-pip3

Installing pip for CentOS 6 and 7, and older versions of Red Hat

sudo yum install epel-release

sudo yum install python-pip3

Installing pip for Arch Linux and Manjaro

sudo pacman -S python-pip

Installing pip for OpenSUSE

sudo zypper python3-pip

Once you have installed pip, you can install Matplotlib using:

pip3 install matplotlib

Check Matplotlib Version

Once you have successfully installed Matplotlib, you can use two methods to check the version of Matplotlib. First, you can use pip show from your terminal. Remember that the name of the package is Matplotlib.

pip show matplotlib
Name: matplotlib
Version: 3.3.4
Summary: Python plotting package
Home-page: https://matplotlib.org

Second, within your python program, you can import Matlotlib and then reference the __version__ attribute:

import matplotlib

print(matplotlib.__version__)
3.3.4

Installing Matplotlib Using Anaconda

Anaconda is a distribution of Python and R for scientific computing and data science. You can install Anaconda by going to the installation instructions. Once you have installed Anaconda, you can install Matplotlib using the following command:

conda install -c conda-forge matplotlib

Importing matplotlib.pyplot

You can import the Pyplot API to create plots using the following lines in your program

import matplotlib.pyplot as plt

It is common to abbreviate the pyplot import to plt.

Summary

Congratulations on reading to the end of this tutorial. The modulenotfounderror occurs if you misspell the module name, incorrectly point to the module path or do not have the module installed in your Python environment. If you do not have the module installed in your Python environment, you can use pip to install the package. However, you must ensure you have pip installed on your system. You can also install Anaconda on your system and use the conda install command to install Matplotlib.

Go to the online courses page on Python to learn more about Python for data science and machine learning.

Have fun and happy researching!

Понравилась статья? Поделить с друзьями:
  • Maximum call stack size exceeded как исправить react
  • Maximum call stack size exceeded как исправить google chrome
  • Max payne ошибка jpeg error
  • Max payne runtime error
  • Max payne an error has occurred in the graphics engine