Matplotlib is a comprehensive, low-level plotting library for Python that creates static, animated, and interactive visualizations across 2D and 3D spaces. Originally designed to mimic MATLAB's plotting interface, it has become the foundational visualization library in the Python ecosystem, serving as the backend for many higher-level tools like Seaborn and Pandas plotting. Understanding both its pyplot interface (stateful, MATLAB-like) and its object-oriented API (explicit control via Figure and Axes) is essential β the latter offers better scalability for complex multi-subplot layouts and production code, while pyplot excels at quick exploratory analysis.
What This Cheat Sheet Covers
This topic spans 22 focused tables and 138 indexed concepts. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
Table 1: Core Plotting Functions
| Function | Example | Description |
|---|---|---|
plt.plot(x, y, 'r--') | β’ Line plot connecting data points β’ accepts format strings for color, marker, and linestyle in compact notation. | |
plt.scatter(x, y, s=50, c='blue') | β’ Scatterplot for visualizing relationships β’ supports variable marker sizes and colors via arrays. | |
plt.bar(categories, values) | β’ Vertical bar chart β’ use barh() for horizontal orientation. | |
plt.hist(data, bins=20) | Histogram showing distribution of a single variable across binned ranges. | |
plt.boxplot([data1, data2]) | Box-and-whisker plot displaying quartiles and outliers for statistical distributions. | |
plt.fill_between(x, y1, y2) | β’ Shaded region between two curves β’ commonly used for confidence intervals. | |
plt.errorbar(x, y, yerr=err) | β’ Plot with error bars showing uncertainty β’ accepts symmetric or asymmetric errors. |