Import matplotlib pyplot as plt ошибка

Hello, I'm trying to run the CNTK tutorial notebook: CNTK_101_LogisticRegression. I cannot import matplotlib.pyplot (base) C:CNTK-Samples-2-3-1Tutorials>python Python 3.6.3 |Anaconda custo...

Hello,

I’m trying to run the CNTK tutorial notebook: CNTK_101_LogisticRegression.
I cannot import matplotlib.pyplot

(base) C:CNTK-Samples-2-3-1Tutorials>python
Python 3.6.3 |Anaconda custom (64-bit)| (default, Oct 15 2017, 03:27:45) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
>>> import matplotlib.pyplot as plt
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:UsersCharlesAnaconda3libsite-packagesmatplotlibpyplot.py", line 32, in <module>
    import matplotlib.colorbar
  File "C:UsersCharlesAnaconda3libsite-packagesmatplotlibcolorbar.py", line 36, in <module>
    import matplotlib.contour as contour
  File "C:UsersCharlesAnaconda3libsite-packagesmatplotlibcontour.py", line 21, in <module>
    import matplotlib.font_manager as font_manager
  File "C:UsersCharlesAnaconda3libsite-packagesmatplotlibfont_manager.py", line 58, in <module>
    from matplotlib import afm, cbook, ft2font, rcParams, get_cachedir
ImportError: DLL load failed: The specified procedure could not be found.
>>> quit()

(base) C:CNTK-Samples-2-3-1Tutorials>conda install matplotlib
Solving environment: done

# All requested packages already installed.

(base) C:CNTK-Samples-2-3-1Tutorials>python
Python 3.6.3 |Anaconda custom (64-bit)| (default, Oct 15 2017, 03:27:45) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib.pyplot as plt
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:UsersCharlesAnaconda3libsite-packagesmatplotlibpyplot.py", line 32, in <module>
    import matplotlib.colorbar
  File "C:UsersCharlesAnaconda3libsite-packagesmatplotlibcolorbar.py", line 36, in <module>
    import matplotlib.contour as contour
  File "C:UsersCharlesAnaconda3libsite-packagesmatplotlibcontour.py", line 21, in <module>
    import matplotlib.font_manager as font_manager
  File "C:UsersCharlesAnaconda3libsite-packagesmatplotlibfont_manager.py", line 58, in <module>
    from matplotlib import afm, cbook, ft2font, rcParams, get_cachedir
ImportError: DLL load failed: The specified procedure could not be found.

>>> import matplotlib
>>> matplotlib.__file__
'C:\Users\Charles\Anaconda3\lib\site-packages\matplotlib\__init__.py'
>>> print(matplotlib.__version__)
2.1.1

Any help will be greatly appreciated.

Charles

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!

The ImportError: No module named matplotlib.pyplot occurs if you have not installed the Matplotlib library in Python and trying to run the script which has matplotlib related code. Another issue might be that you are not importing the matplotlib.pyplot properly in your Python code.

In this tutorial, let’s look at installing the matplotlib module correctly in different operating systems and solve No module named matplotlib.pyplot.  

Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python.

Matplotlib is not a built-in module (it doesn’t come with the default python installation) in Python, you need to install it explicitly using the pip installer and then use it.

If you looking at how to install pip or if you are getting an error installing pip checkout pip: command not found to resolve the issue.

Matplotlib releases are available as wheel packages for macOS, Windows and Linux on PyPI. Install it using pip:

Install Matplotlib in OSX/Linux 

The recommended way to install the matplotlib module is using pip or pip3 for Python3 if you have installed pip already.

Using Python 2

$ sudo pip install matplotlib

Using Python 3

$ sudo pip3 install matplotlib

Alternatively, if you have easy_install in your system, you can install matplotlib using the below command.

Using easy install

$ sudo easy_install -U matplotlib

For CentOs

$ yum install python-matplotlib

For Ubuntu

To install matplotlib module on Debian/Ubuntu :

$ sudo apt-get install python3-matplotlib

Install Matplotlib in Windows

In the case of windows, you can use pip or pip3 based on the Python version, you have to install the matplotlib module.

$ pip3 install matplotlib

If you have not added the pip to the environment variable path, you can run the below command in Python 3, which will install the matplotlib module. 

$ py -m pip install matplotlib

Install Matplotlib in Anaconda

Matplotlib is available both via the anaconda main channel and it can be installed using the following command.

$ conda install matplotlib

You can also install it via the conda-forge community channel by running the below command.

$ conda install -c conda-forge matplotlib

In case you have installed it properly but it still throws an error, then you need to check the import statement in your code.

In order to plot the charts properly, you need to import the matplotlib as shown below.

# importing the matplotlib 
import matplotlib.pyplot as plt
import seaborn as sns

# car sales data
total_sales = [3000, 2245, 1235, 5330, 4200]

location = ['Bangalore', 'Delhi', 'Chennai', 'Mumbai', 'Kolkatta']

# Seaborn color palette to plot pie chart
colors = sns.color_palette('pastel')

# create pie chart using matplotlib
plt.pie(total_sales, labels=location, colors=colors)
plt.show()

Avatar Of Srinivas Ramakrishna

Srinivas Ramakrishna is a Solution Architect and has 14+ Years of Experience in the Software Industry. He has published many articles on Medium, Hackernoon, dev.to and solved many problems in StackOverflow. He has core expertise in various technologies such as Microsoft .NET Core, Python, Node.JS, JavaScript, Cloud (Azure), RDBMS (MSSQL), React, Powershell, etc.

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

By checking this box, you confirm that you have read and are agreeing to our terms of use regarding the storage of the data submitted through this form.

About a week back, I was reinstalling Keras, TensorFlow and all the other libraries after a reformat of my PC. When I started verifying the library installations, I came across a strange error. When I tried to run a simple deep learning model, Python runtime crashed. As soon as I execute the script I was getting the «python.exe has stopped working» error message (I’m using Windows 10).

Matplotlib is an important part of my deep learning workflows
Matplotlib is an important part of my deep learning workflows

A little bit of debugging narrowed down the error to the following line in my script.

import matplotlib.pyplot as plt

The error did not occur if I simply import matplotlib. It only occurred when specifically importing the pyplot module.

import matplotlib
# no errors

import matplotlib.pyplot as plt
# crash!!!

This is a known issue due to some library conflicts in the installation, which should hopefully be fixed in a future release. Until then, if you’re getting this error, you can fix it by following the steps below.

Try them in order and see which step works for you.

1. Check if multiple versions of numpy is installed, and remove any unnecessary versions if possible.

2. Try updating matplotlib

3. Force reinstall matplotlib using conda

conda install -f matplotlib

4. Clean out any version of matplotlib which might have been installed using pip

conda remove matplotlib
pip uninstall matplotlib
conda install matplotlib

5. If all other steps fail, try removing the conda version of matplotlib, and installing using pip instead

conda remove matplotlib
pip install matplotlib

Hopefully, these steps would help you get your matplotlib import working correctly again.

Update: As of January 2020, the error has been fixed in the latest version of Matplotlib (3.1.1).

Related links,

  • https://stackoverflow.com/questions/28848270/import-matplotlib-pyplot-gives-importerror-dlopen-library-not-loaded-libpng1
  • https://github.com/matplotlib/matplotlib/issues/10252/
  • https://github.com/ContinuumIO/anaconda-issues/issues/1672
  • https://stackoverflow.com/questions/24302091/error-when-import-matplotlib-pyplot-as-plt
  • https://github.com/matplotlib/matplotlib/issues/10277
  • https://github.com/ContinuumIO/anaconda-issues/issues/6689
  • https://github.com/matplotlib/matplotlib/issues/4598/

Build Deeper: Deep Learning Beginners' Guide

Build Deeper: The Path to Deep Learning

Learn the bleeding edge of AI in the most practical way: By getting hands-on with Python, TensorFlow, Keras, and OpenCV. Go a little deeper…

Get your copy now!

Понравилась статья? Поделить с друзьями:
  • Imax b6 ошибка input vol err
  • Imax b6 ошибка cell error voltage invalid
  • Imax b6 ошибка balance connect error
  • Imax b6 mini balance connect error как исправить
  • Imaplib imap4 error command search illegal in state auth only allowed in states selected