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, 102 flashcards. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
A jump-to index of every table row in this cheat sheet.
An interactive map of every table and concept in this topic.
Table 1: Core Plotting Functions
The starting point for almost any visualization is picking the right chart type for your data. Line, scatter, and bar cover the everyday cases, while histograms, box plots, and violin plots reveal distributions, and specialized forms like stem, step, and broken_barh handle discrete signals and timeline data β each one a single call that turns arrays into a picture.
| 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. | ||
plt.stackplot(x, y1, y2, y3) | Stacked area chart showing cumulative totals of multiple series over time. | ||
plt.violinplot(data) | β’ Combination of boxplot and kernel density estimate β’ shows distribution shape and density. | ||
plt.pie(sizes, labels=labels) | β’ Pie chart for proportional data β’ supports explode parameter to separate slices. | ||
plt.stem(x, y) | β’ Vertical lines from baseline to data points β’ useful for discrete signal data. | ||
plt.step(x, y, where='mid') | β’ Step plot connecting points with horizontal and vertical segments β’ where controls step placement. | ||
plt.hlines(y=[2, 4], xmin=0, xmax=5) | β’ Horizontal/vertical lines at specific data-coordinate positions with defined extents β’ unlike axhline, lines don't span the full axis. | ||
ax.broken_barh([(10,50),(100,20)], (5,9)) | Horizontal sequence of rectangles β useful for Gantt charts and timeline/categorical interval plots. |