Matplotlib Anatomy
Data Visualization with Matplotlib
What is Matplotlib?
Matplotlib is Python's foundational 2D plotting library, originally modeled after MATLAB's plotting API. It gives you complete programmatic control over every visual element of a chart β from axis tick marks and font sizes to line styles and color maps β making it the standard tool for producing publication-quality figures in academic papers, research reports, and data science projects.
Matplotlib renders graphics as a hierarchy of Python objects. Once you understand this hierarchy, you can customize any part of any chart precisely.
The Anatomy of a Matplotlib Figure
Understanding the hierarchy is key to full control:
Figure β the entire canvas
βββ Axes β a single plot area (subplots)
βββ Title
βββ X-axis (label, ticks, limits)
βββ Y-axis (label, ticks, limits)
βββ Lines / Bars / Scatter points
βββ Legend
Object-Oriented Interface (Recommended)
Always prefer the OO interface over plt.plot() for professional code:
import matplotlib
matplotlib.use('Agg') # Required in Pyodide
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(10, 5))
ax.plot(x, y, label="My Line")
ax.set_title("Chart Title", fontsize=16)
ax.set_xlabel("X Label")
ax.set_ylabel("Y Label")
ax.legend()
plt.tight_layout()
Note: In QuraLabz, all charts render as embedded images directly in the output console β no pop-up windows needed!