Skip to content

Installation and Import

Matplotlib is the foundational plotting library for Python scientific computing.


Installation

Install via pip:

pip install matplotlib

Or via conda:

conda install matplotlib

Import Convention

The standard import convention uses plt as the alias:

import matplotlib.pyplot as plt

For numerical data, NumPy is typically imported alongside:

import matplotlib.pyplot as plt
import numpy as np

Version Check

import matplotlib
print(matplotlib.__version__)

Backend Configuration

Matplotlib uses backends for rendering. Common backends include:

import matplotlib
matplotlib.use('TkAgg')  # Before importing pyplot

In Jupyter notebooks:

%matplotlib inline    # Static images
%matplotlib notebook  # Interactive

Verifying Installation

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 2 * np.pi, 100)
y = np.sin(x)

plt.plot(x, y)
plt.show()

If a plot window appears (or renders inline in Jupyter), the installation is successful.


Key Takeaways

  • Install with pip install matplotlib or conda install matplotlib
  • Import as import matplotlib.pyplot as plt
  • Configure backend before importing pyplot if needed