Matplotlib is the fundamental plotting library in Python for creating static, animated, and interactive visualizations. It's the foundation upon which many other Python visualization libraries (like Seaborn, Pandas plotting, and Plotly) are built.
It is used for:
- Analysing trends
- Comparing values
- Monitoring systems
- Presenting findings
- Debugging performance issues
If you have deployed the
Anaconda/Jupyterenvironment inDockeryou may skip this step, numpy, pandas, and matplotlib are installed automatically via the requirements.txt file.
Matplotlib is not included in the Standard Python Library, but it can be installed using pip.
Optional: Create a virtual environment with
python -m venv ./venvthen activate it with.venv\Scripts\activate.ps1
Install Matplotlib by running pip install Matplotlib in your terminal, then import it into your workspace just like any other module, however by convention we alias it as pd:
import Matplotlib.pyplot as plt- Versatility: Create almost any type of plot imaginable
- Fine-grained control: Customize every aspect of your plots
- Integration: Works seamlessly with NumPy, Pandas, and SciPy
- Cross-platform: Works on Windows, macOS, Linux, and in web backends
- Publication-quality: Produce high-resolution figures for papers and presentations
Matplotlib has a three-layer architecture:
- Backend Layer: Handles rendering to various outputs (screen, files, etc.)
- Artist Layer: Everything you see (lines, text, ticks, etc.) is an Artist object
- Scripting Layer (pyplot): The interface most users interact with
Matplotlib graphs your data on Figures which are made of several parts:
- Figure: Think of the Figure as a blank canvas or a piece of paper. It's the top-level container that holds everything.
- Axes: Where the actual plotting happens. A Figure can contain multiple Axes objects.
- Axis: Handles the x-axis and y-axis, including limits, ticks, and labels.
- Ticks: the marks on the axes
- Labels: axis labels
- Title: the plot title
- Legend: labels for multiple lines or categories
Think of the Figure as a blank canvas or a piece of paper. It's the top-level container that holds everything.
Line plots show trends over time.
import matplotlib.pyplot as plt
cpu_usage = [40, 55, 70, 65, 80]
plt.plot(cpu_usage)
plt.show()plot(): draws the graphshow(): renders it- The default x-axis is the
indexposition
A chart without labels is almost useless.
plt.plot(cpu_usage)
plt.title("CPU Usage Over Time")
plt.xlabel("Time")
plt.ylabel("CPU %")
plt.show()Basic styling makes plots readable, markers show data points, and line styles improve clarity. Common customisations include marker, linestyle, linewidth, and color.
plt.plot(cpu_usage, marker="o", linestyle="--", linewidth=2)You can define multiple plots as part of the same figure; ensure you include a Legend to identify datasets.
cpu = [40, 55, 70, 65]
memory = [60, 65, 75, 85]
plt.plot(cpu, label="CPU")
plt.plot(memory, label="Memory")
plt.legend()
plt.show()Bar charts compare categories.
servers = ["A", "B", "C"]
cpu = [65, 80, 55]
plt.bar(servers, cpu)
plt.show()Histogram shows frequency distribution
response_times = [0.5, 0.7, 0.6, 1.2, 1.5, 0.9]
plt.hist(response_times)
plt.show()Shows correlation between variables.
cpu = [40, 50, 60, 70]
response = [0.5, 0.6, 0.9, 1.4]
plt.scatter(cpu, response)
plt.show()| Goal | Best Plot |
|---|---|
| Show a trend | Line plot |
| Compare categories | Bar chart |
| Show distribution | Histogram |
| Show relationship | Scatter plot |
| Compare multiple trends | Multi‑line plot |
| Compare distributions | Box plot |
Simple, but important
plt.savefig("cpu_plot.png")Practice your knowledge by trying the tasks linked below.