Python can import matplotlib

anaconda cannot import matplotlib.pyplot

I am getting this error when I am trying to import «matplotlib.pyplot». I cant even install matplotlib.pyplot through conda install. It shows this:

import matplotlib.pyplot Traceback (most recent call last): File «», line 1, in ModuleNotFoundError: No module named ‘matplotlib.pyplot’

7 Answers 7

As reported here, when you use Anaconda, install the packet using conda. In this case, the right instruction to use (on Ubuntu 18.04) is:

conda install -c conda-forge matplotlib 

This will solve the problem.

If you use pip (you can), you will mess up all the dependencies (and for instance, the probability that other scripts/programs do not work anymore is not null: if you use Spyder, you will have big dependencies problem to face).

In order to always avoid problem like this, I recommend you to use Virtual Enviroment:

Whats is it?

A step-by-step guide is always useful.

It could be that it’s running your default Python installation instead of the one installed with Anaconda. Try prepending this to the top of your script:

Читайте также:  Php log user agent

If that does not work, try installing matplotlib with pip , then try again:

Let me know if that works for you.

I had the same issue for days, just solved it by adding «%matplotlib inline» on top of «import matplotlib.pyplot as plt»

So enter this to import mathplotlib.pylot:

%matplotlib inline import matplotlib.pyplot as plt 

Just open anaconda prompt and use either of the below command to install the package. This solved my issue.

Make sure Matplotlib is accessed in the Same Conda Environment as it was installed .
In the case below, Matplotlib was installed in pytorch environment and not the base environment .
Hence when run in the pytorch environment it gets imported, but doesn’t get imported in base environment .

#Installation (pytorch) F:\Script\Ai\Pytorch>conda install -c conda-forge matplotlib #Check installation in pytorch Environment (pytorch) F:\Script\Ai\Pytorch>python >>> import matplotlib >>> print('matplotlib: <>'.format(matplotlib.__version__)) matplotlib: 3.3.4 #Import Error in base Environment (base) F:\Script\Ai\Pytorch>python >>> import matplotlib Traceback (most recent call last): File "", line 1, in ModuleNotFoundError: No module named 'matplotlib' 

Источник

How to Install Matplotlib in Python?

Matplotlib is an open-source python library that can be installed using python packages like pip and conda on different operating systems like Windows, Linux, and macOS.

  • It displays numeric data in the form of plots, graphs, and charts in Python.
  • It provides a wide variety of functionality for plotting.
  • It is a very useful library that helps to plot different figures on the graph, like line plot, bar graphs, contour plot, histograms, scatter plots, etc.

Prerequisites

  • It is mandatory to install python from www.python.org before installing matplotlib.
  • During the installation of matplotlib, some files are automatically installed. The following list is mainly for reference:
    • Python (>= 3.7)
    • NumPy (>= 1.17)
    • setup tools
    • cycler (>= 0.10.0)
    • dateutil (>= 2.7)
    • kiwisolver (>= 1.0.1)
    • Pillow (>= 6.2)
    • pyparsing (>=2.2.1)
    • fontTools (>=4.22.0)

    Installing Matplotlib

    Let’s discuss installation of matplotlib on different operating systems, below.

    Installing Matplotlib on MacOS

    1. Check if pip3 and python3 are installed correctly by running the following command:

    Installing matplotlib

    Installing Matplotlib in Windows

    Steps for installing Matplotlib on Windows:

    1. Download python from www.python.org according to your operating system and install it.
    2. Next, install Matplotlib using the following PIP command in the command prompt terminal:

    Installing matplotlib in Windows

    The installation will start automatically, and Matplotlib will be successfully installed with its latest version.

    pip install matplotlib for installation

    You must have noticed that some files are automatically installed with matplotlib, like python-dateutil, packaging, pyparsing, numpy, kiwisolver, etc. These files are the mandatory dependencies of the matplotlib module.

    Matplotlib Installation On Ubuntu

    Python is pre-installed on Ubuntu computers. Pip, on the other hand, is not installed. There are two ways to install pip:

    1. Install the complete package by downloading python from python.org, then use the apt install command for installing on the ubuntu system.
    2. Another way is to directly install pip first on Ubuntu and then install matplotlib by performing the following steps:

    installing pip on ubuntu

    for installing matplotlib in python3, use

    using pip install matplotib

    For installing matplotlib in python2. Matplotlib is successfully installed on Ubuntu using the above commands.

    Installing From the Source

    First, we must install the dependencies (as discussed in the ‘prerequisites’ section). A C compiler is required for this.

    To run the tests, you will need to install some additional dependencies:

    Advanced Usage

    Installing Matplotlib Using Conda

    Conda is an open-source environment and package system.

    • It offers prebuilt packages and can install and update.
    • We do not need to install the system and python packages separately since everything is already included in the .yml (a text file that contains a list of dependencies, etc., in a single file). Anaconda must be commanded below in cmd /conda prompt/ terminal:

    Above command install matplotlib from anaconda main channel. Matplotlib can also be installed from the anaconda community channel.

    advanced use of matplotlib

    Matplotlib Installation Using PIP

    Comparing Python2 and Python3

    For Python2 Users:

    Python2 users must use pip for the installation of matplotlib.

    For macOS, windows, and ubuntu users:

    For Python3 Users:

    Python3 users must use pip3 for the installation of matplotlib.

    For macOS, windows, and ubuntu users:

    How to Install Matplotlib Python venv?

    How to Install Matplotlib Python venv in MacOS?

    How to Install Matplotlib Python venv in Windows

    install matplotlib python venv in windows

    Activating environment to install

    1. Retrieve the latest version of matplotlib from the git source, and install matplotlib in editable mode.

    How to Install Matplotlib Python venv in Ubuntu?

    1. Retrieve the latest version of matplotlib from the git source, and install matplotlib in editable mode.

    Verify Matplotlib Installation

    Verifying Installation of Matplotlib on MacOS

    Verifying the installation of matplotlib on macOS can be done using the command:

    1. We set up the python environment by typing python3 in the terminal.
    2. Successful import of matplotlib verifies that matplotlib is installed on the system. The command for importing matplotlib is import matplotlib . Set of commands to verify installation

    Verify Matplotlib Installation

    Verifying Installation of Matplotlib on Windows

    Verifying matplotlib installation can be done in two different ways.

    Verifying Installation of matplotlib on Windows

    In the command prompt.

    1. Another verification method is setting up a python environment, importing matplotlib, and then checking its version.

    Verification of installation

    Verifying Installation of Matplotlib on Ubuntu

    Verifying the Installation of matplotlib on Ubuntu can be done using the command:

    1. We set the python environment by typing python3 in the terminal.
    2. Successful import of matplotlib verifies that matplotlib is installed on the system. The command for importing matplotlib is import matplotlib .
    3. Checking the version of the matplotlib using the command matplotlib.__version__

    Set of commands to verify installation

    Verifying Installation of matplotlib on Ubuntu

    What Happens If Matplotlib is not Installed Correctly?

    The Python «ModuleNotFoundError: No module named ‘matplotlib'» occurs when we forget to install the matplotlib module before importing it or installing it in an incorrect environment. Check if pip is installed by simply writing pip in the python console. If you do not have pip, get a python script call g e t − p i p . p y get-pip.py g e t − p i p . p y from the internet and save it to your local system.

    Run the g e t − p i p . p y get-pip.py g e t − p i p . p y script to install pip by writing the following code in cmd (command prompt) to install pip:

    Now in cmd, type the following code to install matplotlib with its dependencies:

    Conclusion

    • Python and compilers are prerequisites for installing matplotlib.
    • Matplotlib can be installed using conda and pip python packages. Pip contains python libraries. On the other hand, conda has python libraries, c libraries, and executables like c compilers, etc.
    • Matplotlib can be installed from the GitHub repository of matplotlib.
    • Matplotlib installation verification can be done in two ways.
      • Importing matplotlib
      • Checking matplotlib version

      Learn More:

      Источник

      Getting started#

      Further details are available in the Installation Guide .

      Draw a first plot#

      Here is a minimal example plot:

      import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 2 * np.pi, 200) y = np.sin(x) fig, ax = plt.subplots() ax.plot(x, y) plt.show() 

      ../../_images/index-1.png

      If a plot does not show up please check Troubleshooting .

      Where to go next#

      • Check out Plot types to get an overview of the types of plots you can create with Matplotlib.
      • Learn Matplotlib from the ground up in the Quick-start guide .

      © Copyright 2002–2012 John Hunter, Darren Dale, Eric Firing, Michael Droettboom and the Matplotlib development team; 2012–2023 The Matplotlib development team.

      Built from v3.7.2-2-g4915f574b8.

      Источник

Оцените статью