Matplotlib Apiref
Matplotlib is the most widely used data visualization library in Python, supporting the creation of static, dynamic, and interactive charts.
This document comprehensively organizes the functions and methods of all public interfaces in Matplotlib, making it convenient to quickly look up their corresponding features and usage.
* * *
## Two Major Programming Interfaces
Matplotlib provides two usage modes, suitable for different scenarios.
| Feature | Axes Interface (Explicit/Object-Oriented) | pyplot Interface (Implicit/Functional) |
| --- | --- | --- |
| Usage | Create Figure and Axes objects first, then call object methods | Directly call pyplot module functions, implicitly operating on the current chart |
| Applicable Scenarios | Complex charts, multiple subplots, requiring fine control | Quick plotting, interactive exploration, simple charts |
| Code Example | fig, ax = plt.subplots(); ax.plot(x, y) | plt.plot(x, y); plt.title("Title") |
| Recommendation Level | Recommended (clearer, more controllable) | Suitable for simple scenarios and quick prototyping |
> The Axes interface is the officially recommended programming style, with clearer code logic, and is less error-prone when handling multiple subplots and complex charts. For quick reference, both pyplot functions and their corresponding Axes methods are listed below.
| Function | Related Content |
| --- | --- |
| [Matplotlib plot() Function](#) | plot() |
| [Matplotlib scatter() Function](#) | scatter() |
| [Matplotlib bar() / barh() Function](#) | bar(), barh(), bar_label(), grouped_bar() |
| [Matplotlib hist() Function](#) | hist() |
| [Matplotlib pie() Function](#) | pie(), pie_label() |
| [Matplotlib imshow() Function](#) | imshow() |
| [Matplotlib subplots() Function](#) | subplots() |
| [Matplotlib figure() Function](#) | figure() |
| [Matplotlib savefig() Function](#) | savefig() |
| [Matplotlib errorbar() Function](#) | errorbar() |
| [Matplotlib boxplot() Function](#) | boxplot() |
| [Matplotlib contour() / contourf() Function](#) | contour(), contourf(), clabel() |
| [Matplotlib fill_between() / fill_betweenx() Function](#) | fill_between(), fill_betweenx() |
| [Matplotlib legend() Function](#) | legend() |
| [Matplotlib text() / annotate() Function](#) | text(), annotate() |
| (#) | title(), xlabel(), ylabel(), suptitle(), supxlabel(), supylabel() |
| [Matplotlib colorbar() Function](#) | colorbar() |
| (#) | axes(), cla(), clf(), close(), delaxes(), fignum_exists(), gca(), gcf(), get_figlabels(), get_fignums(), sca(), subplot(), subplot2grid(), subplot_mosaic(), twinx(), twiny() |
| (#) | step(), stem(), eventplot(), stackplot(), broken_barh(), vlines(), hlines(), fill(), loglog(), semilogx(), semilogy() |
| (#) | axhline(), axhspan(), axvline(), axvspan(), axline(), quiver(), quiverkey(), barbs(), streamplot() |
| (#) | acorr(), xcorr(), psd(), csd(), specgram(), cohere(), angle_spectrum(), magnitude_spectrum(), phase_spectrum() |
| (#) | hist2d(), hexbin(), stairs(), matshow(), pcolor(), pcolormesh(), spy(), figimage(), ecdf(), violinplot() |
| (#) | triplot(), tripcolor(), tricontour(), tricontourf(), polar(), rgrids(), thetagrids() |
| (#) | xlim(), ylim(), xscale(), yscale(), xticks(), yticks(), tick_params(), ticklabel_format(), locator_params(), minorticks_on(), minorticks_off(), grid(), axis(), box(), autoscale() |
| (#) | subplots_adjust(), tight_layout(), margins(), subplot_tool(), rc(), rc_context(), rcdefaults(), clim(), get_cmap(), set_cmap(), gci(), sci(), imread(), imsave(), draw(), ion(), ioff(), pause(), switch_backend(), show(), isinteractive(), install_repl_displayhook(), uninstall_repl_displayhook(), draw_if_interactive() |
| (#) | connect(), disconnect(), ginput(), waitforbuttonpress(), findobj(), get(), setp(), getp(), get_current_fig_manager(), new_figure_manager(), set_loglevel(), xkcd() |
| (#) | figtext(), figlegend(), table(), arrow() |
* * *
## pyplot Module - Complete Function List
pyplot is the top-level interface of Matplotlib, providing a MATLAB-like plotting experience. All functions are listed below according to official categories.
### 1. Figure and Axes Management
Functions for creating and managing Figure (canvas) and Axes (coordinate systems/subplots).
| Function | Description |
| --- | --- |
| figure() | Create a new Figure or activate an existing Figure |
| subplots() | Recommended: Create a Figure and a set of Axes subplots |
| subplot() | Add a single subplot to the current Figure (by row/column index) |
| subplot2grid() | Create a subplot at a specified position in a grid layout |
| subplot_mosaic() | Create complex non-uniform layouts using label strings |
| axes() | Add an Axes to the current Figure |
| gca() | Get the current Axes object |
| gcf() | Get the current Figure object |
| sca() | Set the current Axes |
| cla() | Clear the current Axes |
| clf() | Clear the current Figure |
| close() | Close the Figure window |
| delaxes() | Remove a specified Axes from the Figure |
| fignum_exists() | Check if a Figure with the specified number exists |
| get_figlabels() | Return a list of labels for all Figures |
| get_fignums() | Return a list of numbers for all Figures |
| twinx() | Create a twin y-axis sharing the x-axis |
| twiny() | Create a twin x-axis sharing the y-axis |
### 2. Basic Plotting
The most commonly used chart type plotting functions.
| Function | Description |
| --- | --- |
| plot() | Draw a line chart (most commonly used) |
| scatter() | Draw a scatter plot, supporting size/color/opacity mapping |
| bar() | Draw a vertical bar chart |
| barh() | Draw a horizontal bar chart |
| bar_label() | Add value labels to the bars of a bar chart |
| grouped_bar() | Draw a grouped bar chart |
| pie() | Draw a pie chart |
| pie_label() | Add labels to a pie chart |
| stem() | Draw a stem plot (matchstick plot) |
| eventplot() | Draw an event plot (multiple horizontal lines marking event positions) |
| step() | Draw a step chart |
| fill() | Draw a filled polygon |
| fill_between() | Fill the area between two horizontal curves |
| fill_betweenx() | Fill the area between two vertical curves |
| stackplot() | Draw a stacked area chart |
| broken_barh() | Draw a horizontal broken bar chart (Gantt chart style) |
| vlines() | Draw vertical reference lines |
| hlines() | Draw horizontal reference lines |
| errorbar() | Draw a line chart with
YouTip