Learn Matplotlib
Matplotlib is Python's foundational plotting library, used to turn raw numbers into line charts, bar charts, scatter plots, and many other visual forms.
What is Matplotlib?
Matplotlib is a data visualization library for Python that lets you turn arrays of numbers into charts you can look at, save as image files, or embed in a report or web page. It gives you fine control: you decide the size of the figure, the colors, the fonts, the axis ranges, and virtually every pixel of the final image, while also offering simple one-line functions for people who just want a quick plot.
The library was started by neurobiologist John Hunter in 2003, who wanted a free, open-source alternative to the plotting tools found in MATLAB so he could visualize brain signal data directly from Python. Two decades later, Matplotlib remains the most widely used plotting library in the Python data ecosystem and is the engine behind many higher-level tools such as pandas' built-in .plot() method and seaborn.
What can you build with it?
- Line charts to show a trend over time, such as a stock price or temperature reading
- Bar charts and histograms to compare categories or show how values are distributed
- Scatter plots to reveal the relationship between two numeric variables
- Pie charts and stacked area charts for showing proportions
- Heatmaps and images built from two-dimensional arrays
- Publication-quality figures exported as PNG, SVG, PDF, or other formats
Example
import matplotlib.pyplot as plt
years = [2019, 2020, 2021, 2022, 2023]
revenue = [12, 15, 9, 22, 30]
plt.plot(years, revenue)
plt.title("Yearly Revenue")
plt.show()Where Matplotlib fits in
Matplotlib is built directly on top of NumPy, so it understands NumPy arrays natively and works well with any library that produces array-like data, including pandas DataFrames. Many other visualization tools, such as seaborn, are actually thin layers that generate nicer-looking charts by calling Matplotlib functions behind the scenes, which is why learning Matplotlib gives you a foundation that transfers to the rest of the Python visualization world.
Why learn Matplotlib?
- It is free, open source, and works the same way on Windows, macOS, and Linux
- It integrates with Jupyter notebooks, scripts, and web frameworks alike
- It is the most common plotting library taught in data science courses, so charts you see in tutorials usually use it
- Once you understand its core ideas, other libraries such as seaborn and pandas plotting feel familiar
The rest of this tutorial walks through installing Matplotlib, understanding the pyplot interface, drawing your first plots, and then customizing markers, lines, labels, and grids so your charts communicate exactly what you want them to.
Exercise: Matplotlib Introduction
What is Matplotlib primarily used for in Python?
Frequently Asked Questions
- Should I learn Matplotlib or Seaborn?
- Matplotlib first. Seaborn is built on top of it and produces attractive statistical charts with less code, but the moment you need to adjust an axis, label or legend you are back in Matplotlib. Knowing the base makes the wrapper straightforward.
- Why does my Matplotlib chart not appear?
- Usually a missing show call, or a script running where no window can open. In notebooks the figure renders inline automatically; in a plain script you need show, or save the figure to a file instead.
- What is the difference between plt and the axes API?
- The plt interface tracks a current figure implicitly, which is convenient for a quick chart. The axes API gives you explicit figure and axis objects, which is what you want for subplots or any chart built across several functions.