How to Install and Remove Matplotlib in Python: A Step-by-Step Guide with Examples

Matplotlib is a powerful and widely-used Python library for creating static, interactive, and animated visualizations in Python. Whether you’re a data scientist, engineer, or hobbyist programmer, Matplotlib is an essential tool in your toolkit. In this article, we’ll explore the step-by-step process of installing and removing Matplotlib in Python, accompanied by illustrative examples.

1. Installing Matplotlib.

  1. Installing Matplotlib is a straightforward process, and there are multiple ways to achieve it.
  2. Here, we’ll cover the most common method using the pip package manager.

1.1 Install Using Pip.

  1. Open a Terminal or Command Prompt.
  2. On Windows, you can open the Command Prompt from the Start menu.
  3. On macOS or Linux, use the Terminal.
  4. Check if Pip is Installed

    pip --version
  5. If Pip is not installed, you can follow the official documentation to install it.
  6. Install Matplotlib.

    pip install matplotlib
  7. This command will download and install the latest version of Matplotlib and its dependencies.

1.2 Verify Installation.

  1. There are 2 methods to ensure Matplotlib is installed correctly.
  2. You can run the command pip show matplotlib in a command line, if you see the below result, that means matplotlib is installed successfully.
    PS C:\Users\Jerry> pip show matplotlib
    Name: matplotlib
    Version: 3.8.2
    Summary: Python plotting package
    Home-page: https://matplotlib.org
    Author: John D. Hunter, Michael Droettboom
    Author-email: [email protected]
    License: PSF
    Location: C:\Users\Jerry\AppData\Local\Programs\Python\Python312\Lib\site-packages
    Requires: contourpy, cycler, fonttools, kiwisolver, numpy, packaging, pillow, pyparsing, python-dateutil
    Required-by:
  3. Another method to ensure Matplotlib is installed correctly is that, you can run a simple test Python script in Python interactive console.
    import matplotlib.pyplot as plt
    
    plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
    plt.show()
    
  4. If no errors occur and a plot is displayed, Matplotlib is successfully installed.
    matplotlib-verify-installed-successfully-plot-image

2. Removing Matplotlib.

  1. If, for any reason, you need to remove Matplotlib from your Python environment, you can do so using pip as well.

2.1 Remove Using Pip.

  1. Open a Terminal or Command Prompt.
  2. Uninstall Matplotlib.

    PS C:\Users\Jerry> pip uninstall matplotlib
    Found existing installation: matplotlib 3.8.2
    Uninstalling matplotlib-3.8.2:
      Would remove:
        c:\users\jerry\appdata\local\programs\python\python312\lib\site-packages\matplotlib-3.8.2.dist-info\*
        c:\users\jerry\appdata\local\programs\python\python312\lib\site-packages\matplotlib.libs\msvcp140-8fe972c21762050da1adf4b84a93423e.dll
        c:\users\jerry\appdata\local\programs\python\python312\lib\site-packages\matplotlib\*
        c:\users\jerry\appdata\local\programs\python\python312\lib\site-packages\mpl_toolkits\*
        c:\users\jerry\appdata\local\programs\python\python312\lib\site-packages\pylab.py
    Proceed (Y/n)? Y
      Successfully uninstalled matplotlib-3.8.2

     

  3. Confirm the uninstallation when prompted.

2.2 Verify Removal.

  1. Run the command pip show matplotlib in a terminal to confirm.
    PS C:\Users\Jerry> pip show matplotlib
    WARNING: Package(s) not found: matplotlib
  2. Attempt to import Matplotlib in a Python script or interpreter. If Matplotlib is successfully removed, attempting to import it should result in an ModuleNotFoundError.
    >>> import matplotlib.pyplot as plt
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ModuleNotFoundError: No module named 'matplotlib'

3. Alternative Installation Methods.

  1. While pip is the most common method, there are alternative ways to install Matplotlib depending on your Python environment.

3.1 Install Using Conda.

  1. If you’re using the Anaconda distribution, you can install Matplotlib using the conda package manager:
    conda activate your_environment_name
    
    conda install matplotlib
    
    conda deactivate

3.2 Remove Using Conda.

  1. If you are working within a specific Conda environment, activate it using the following command. Replace your_environment_name with the actual name of your Conda environment.
    conda activate your_environment_name
  2. Use the following command to uninstall Matplotlib.
    conda remove matplotlib
  3. Don’t forget to deactivate it if you no longer need it.
    conda deactivate

4. Conclusion.

  1. In this article, we’ve covered the essential steps to install and remove Matplotlib in Python. Whether you’re creating intricate data visualizations or plotting simple graphs, Matplotlib is a versatile library that is integral to the Python data science ecosystem.
  2. Remember to choose the installation method that best fits your Python environment, be it using pip, conda, or any other suitable method.
  3. By following the outlined steps and examples, you should now have a solid understanding of how to seamlessly integrate Matplotlib into your Python projects or gracefully remove it when necessary. Happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.