> ## Content Index
> Fetch the complete content index at: https://datascientyst.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# ImportError: matplotlib is required for plotting when the default backend "matplotlib" is selected
- URL: https://datascientyst.com/pandas-importerror-matplotlib-is-required-for-plotting/
- Published: 2024-01-20T23:24:52.000Z
- Updated: 2024-01-20T23:24:52.000Z
- Author: John D K
- Tags: Pandas Error

Pandas, a powerful data manipulation library in Python, offers a convenient way to analyze and visualize data through its integration with the Matplotlib plotting library. However, users may encounter an ImportError when attempting to use Pandas for plotting, specifically indicating that Matplotlib is required. In this article, we will explore the issue and provide solutions to address the Pandas ImportError related to Matplotlib.

## Understanding the ImportError: matplotlib is required for plotting

The error message typically looks like this:

```
ImportError: matplotlib is required for plotting when the default backend "matplotlib" is selected

```

This error arises when Pandas attempts to use Matplotlib for plotting, but Matplotlib is either not installed or is not accessible.

```python
import pandas as pd
ser = pd.Series([1, 2, 3, 3])
plot = ser.plot(kind='hist', title="My plot")

```

## Solution: Install Matplotlib

The most straightforward solution is to ensure that Matplotlib is installed.

Use the following command to install Matplotlib via pip:

```python
pip install matplotlib

```

This command installs the Matplotlib library along with its dependencies, allowing Pandas to use it for plotting functionalities. The package information is available on: [https://pypi.org/project/matplotlib/](https://pypi.org/project/matplotlib/?ref=datascientyst.com)

P.S. Note that matplotlib is part of the optional dependencies for Pandas. You can find all of them here: [Pandas Visualization Dependencies](https://pandas.pydata.org/docs/getting%5Fstarted/install.html?ref=datascientyst.com#visualization)

## Check Matplotlib Version

It's essential to have a compatible version of Matplotlib installed. Pandas may have specific version requirements.

To install a specific version of Matplotlib, use:

```bash
pip install matplotlib==3.8.0

```

Replace `3.8.0` with the version you want to install.

## Import Matplotlib in Your Script

In some cases, the error may persist if Matplotlib is not imported explicitly in your script.

Ensure that you have the following import statement at the beginning of your Python script or Jupyter Notebook - `import matplotlib.pyplot as plt`:

```python
import matplotlib.pyplot as plt
import pandas as pd
ser = pd.Series([1, 2, 3, 3])
plot = ser.plot(kind='hist', title="My plot")

```

This statement ensures that Matplotlib is accessible for Pandas when performing plotting operations.

## Check Virtual Environment

If you are working within a virtual environment, make sure it is activated before installing Matplotlib.

If the virtual environment is not active, the installation might be done in the global Python environment instead.

```python
pip freeze

```

will list the installed python packages.

## Upgrade Pandas

Ensure that you have the latest version of Pandas installed. Use the following command to upgrade Pandas:

```python
pip install --upgrade pandas

```

Upgrading to the latest version may resolve compatibility issues with Matplotlib.

## Conclusion

The Pandas Error: **"ImportError: matplotlib is required for plotting when the default backend "matplotlib"** is selected" is commonly encountered when Matplotlib is not properly installed or is incompatible with the version of Pandas being used.

By following the steps outlined above, you can resolve this error and unlock Pandas plotting capabilities with Matplotlib.