Matplotlib is a comprehensive Python 2D plotting library that produces publication-quality figures in a variety of formats and interactive environments (scripts, Jupyter notebooks, web application servers, and graphical user interfaces).
- Figure: the overall window or page that contains one or more plots.
- Axes: a single plot area inside a Figure; contains the data, ticks, labels, and legend.
- Artist: everything you see on the figure (lines, text, patches). Most high-level plotting functions create Artists.
- pyplot: a stateful, MATLAB-like interface (
matplotlib.pyplot) for quick plotting. - Object-oriented (OO) API: recommended for complex figures — you explicitly create
FigureandAxesobjects and call methods on them.
Install via pip (recommended):
python -m pip install matplotlib
If you use conda:
conda install matplotlib
Using pyplot (quick):
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]
plt.plot(x, y, label='Line')
plt.scatter(x, y, color='red')
plt.title('Simple plot')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.grid(True)
plt.show()Using the object-oriented API (recommended):
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(6,4))
ax.plot(x, y, '-o', label='Series')
ax.set_title('OO API example')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.legend()
fig.tight_layout()
fig.savefig('figure.png', dpi=300)- Line plots:
plot() - Scatter plots:
scatter() - Bar charts:
bar()/barh() - Histograms:
hist() - Boxplots:
boxplot() - Heatmaps / images:
imshow() - Pie charts:
pie()
- Use
rcParamsormatplotlib.style.use()to change default styles (e.g.,plt.style.use('seaborn')). - Title, labels, ticks, and legends are set via Axes methods:
set_title,set_xlabel,set_xticks,legend(). - Annotate with
ax.annotate()and add text withax.text().
- Pandas: DataFrame/Series have
.plot()which uses Matplotlib under the hood. - Seaborn: higher-level statistical plotting built on Matplotlib; use for nicer defaults and statistical plots.
- Interactive environments: works well in Jupyter with
%matplotlib inlineor%matplotlib notebook.
fig.savefig('path.png', dpi=300, bbox_inches='tight')supports PNG, PDF, SVG, EPS.
- For many points, prefer
scatter()with small markers or use downsampling. - Reuse artists (update data) when animating rather than re-creating them for performance.
- Use
constrained_layoutortight_layout()to avoid overlapping labels.
- Mixing
pyplotstateful calls and OO API can lead to confusion; prefer one style per script. - Always call
plt.close(fig)in scripts that generate many figures to free memory.
- Official docs: https://matplotlib.org/
- Tutorials and examples: https://matplotlib.org/stable/tutorials/index.html
- Gallery: https://matplotlib.org/stable/gallery/index.html
This file gives a compact overview and examples to get started. For deep dives, consult the official documentation and gallery.