YouTip LogoYouTip

Matplotlib Func

Matplotlib Common Functions 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 functionalities 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 | First create Figure and Axes objects, 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. The code logic is clearer, and it is less prone to errors when handling multiple subplots and complex charts. For quick reference, both the pyplot functions and their corresponding Axes methods are listed below. * * * ## 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 and 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 a Figure window | | delaxes() | Remove a specified Axes from a 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, supports size/color/opacity mapping | | bar() | Draw a vertical bar chart | | barh() | Draw a horizontal bar chart | | bar_label() | Add value labels to the bars in 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 error bars | | loglog() | Line chart with log-log coordinates | | semilogx() | Line chart with logarithmic x-axis | | semilogy() | Line chart with logarithmic y-axis | | polar() | Plot in polar coordinates | ### 3. Spans Draw horizontal and vertical reference lines and shaded regions. | Function | Description | | --- | --- | | axhline() | Add a horizontal line spanning the Axes | | axhspan() | Add a horizontal shaded span spanning the Axes | | axvline() | Add a vertical line spanning the Axes | | axvspan() | Add a vertical shaded span spanning the Axes | | axline() | Add an infinitely long line passing through two points | ### 4. Spectrum Analysis Functions for signal processing and spectrum visualization. | Function | Description | | --- | --- | | acorr() | Plot the autocorrelation | | xcorr() | Plot the cross-correlation | | angle_spectrum() | Plot the angle spectrum | | magnitude_spectrum() | Plot the magnitude spectrum | | phase_spectrum() | Plot the phase spectrum | | psd() | Plot the power spectral density | | csd() | Plot the cross spectral density | | cohere() | Plot the coherence | | specgram() | Plot a spectrogram (time-frequency plot) | ### 5. Statistical Charts Plot charts related to statistical distributions. | Function | Description | | --- | --- | | boxplot() | Draw a boxplot | | violinplot() | Draw a violin plot | | ecdf() | Draw the empirical cumulative distribution function (ECDF) | ### 6. Binning and Histograms | Function | Description | | --- | --- | | hist() | Draw a 1D histogram | | hist2d() | Draw a 2D histogram | | hexbin() | Draw a hexagonal binning plot | | stairs() | Draw a stair-step histogram (new version, replaces the step mode of hist) | ### 7. Contours | Function | Description | | --- | --- | | contour() | Draw contour lines | | contourf() | Draw filled contours | | clabel() | Add labels to contour lines | ### 8. 2D Arrays and Images Visualization functions for displaying 2D data, matrices, and images. | Function | Description | | --- | --- | | imshow() | Display an image or 2D array (heatmap) | | matshow() | Display a matrix as an image in a new Figure | | pcolor() | Create a pseudocolor plot with a rectangular grid (creates a PolyCollection) | | pcolormesh() | Create a pseudocolor plot with a non-regular rectangular grid (creates a QuadMesh, better performance) | | spy() | Plot the sparsity pattern of a 2D array (non-zero elements) | | figimage() | Place an image at the Figure level (not in Axes) | ### 9. Unstructured Triangular Grids | Function | Description | | --- | --- | | triplot() | Draw an unstructured triangular grid | | tripcolor() | Create a pseudocolor plot of an unstructured triangular grid | | tricontour() | Draw contour lines on an unstructured triangular grid | | tricontourf() | Draw filled contours on an unstructured triangular grid | ### 10. Text and Annotations | Function | Description | | --- | --- | | text() | Add text at specified coordinates in the Axes | | figtext() | Add text at a specified position in the Figure | | annotate() | Add an annotation with an arrow | | arrow() | Add an arrow | | legend() | Add a legend to the Axes | | figlegend() | Add a legend at the Figure level | | table() | Add a table to the Axes | ### 11. Vector Fields | Function | Description | | --- | --- | | quiver() | Plot a 2D field of arrows | | quiverkey() | Add a key (legend scale) to a quiver plot | | barbs() | Plot a 2D field of barbs (used in meteorology for wind speed and direction) | | streamplot() | Draw streamlines of a vector flow | ### 12. Axis Configuration Functions for setting axis ranges, ticks, labels, and scales. | Function | Description | | --- | --- | | title() | Set the Axes title | | suptitle() | Set the Figure suptitle (main title) | | xlabel() | Set the x-axis label | | ylabel() | Set the y-axis label | | xlim() | Get or set the x-axis limits | | ylim() | Get or set the y-axis limits | | xscale() | Set the x-axis scale (linear/log/symlog/logit...) | | yscale() | Set the y-axis scale (linear/log/symlog/logit...) | | xticks() | Get or set the x-axis tick locations and labels | | yticks() | Get or set the y-axis tick locations and labels | | tick_params() | Adjust tick appearance (direction, color, size, label rotation, etc.) | | ticklabel_format() | Set the format of tick labels (e.g., scientific notation) | | locator_params() | Control parameters of the tick locator | | minorticks_on() | Display minor ticks | | minorticks_off() | Hide minor ticks | | rgrids() | Get or set the radial gridlines on a polar plot | | thetagrids() | Get or set the theta gridlines on a polar plot | | grid() | Turn the grid lines on or off | | axis() | Convenience function to
← Matplotlib Ref Advanced PlotsCodex App Interface β†’