matplotlib: The Foundation of Python Visualization
Matplotlib is the foundational data visualization library in Python β almost every other Python visualization library is either built on top of it or provides an alternative to it. Understanding matplotlib's structure and philosophy is prerequisite to using the higher-level libraries (seaborn, pandas plotting, even plotly's static output) that abstract over it, because debugging and customizing their output almost always requires dropping to the matplotlib level.
Matplotlib has two interfaces: the pyplot interface (a stateful, function-based interface modeled on MATLAB's plotting commands, accessed as plt.plot(), plt.title(), etc.) and the object-oriented interface (explicitly managing Figure and Axes objects). For production quality work, the object-oriented interface is strongly preferred because it is explicit about which axes you are modifying when working with multiple-panel figures, it enables better integration with web frameworks and automated reporting, and it produces more maintainable code. The core workflow in the OO interface is: create a Figure object, add one or more Axes objects to it, call plotting methods on the Axes objects, and then save or display the Figure.
Customization in matplotlib is exhaustive β essentially every visual element is adjustable through keyword arguments and object properties. The most important customizations include: figure size (figsize parameter, in inches, at a specified DPI), font sizes (for titles, axis labels, tick labels β all adjustable independently), color (any color name, hex code, RGB tuple, or seaborn palette), line style and marker style (solid, dashed, dotted, plus many marker shapes), axis limits (xlim and ylim), tick locations and labels (the Locator and Formatter classes), grid lines (grid method with alpha for transparency), and annotations (annotate method for adding text and arrows to specific data points).
Saving high-quality figures is a critical skill for publication and reporting. Matplotlib's savefig method accepts a wide range of formats: PNG for web use (raster format, appropriate for images), SVG for scalable web graphics (vector format that maintains quality at any size), PDF for print publications (vector format widely accepted by academic journals), and JPEG (lossy compression, generally not recommended for scientific figures because the compression artifacts interfere with precise color comparisons). The dpi parameter (dots per inch) controls raster image resolution β 72 DPI is screen resolution, 150 DPI is acceptable for web, and 300 DPI is minimum for print publication quality.