Examples#
For an overview of the plotting methods we provide, see Plot types
This page contains example plots. Click on any image to see the full image and source code.
For longer tutorials, see our tutorials page. You can also find external resources and a FAQ in our user guide.
Tagging!
You can also browse the example gallery by tags.
Lines, bars and markers#
Bar chart with individual bar colors
Plotting categorical variables
Plotting the coherence of two signals
fill_between with transparency
Fill the area between two lines
Fill the area between two vertical lines
Discrete distribution as horizontal bar chart
Dashed line style configuration
Lines with a ticked patheffect
Plotting masked and NaN values
Mapping marker properties to multivariate data
Scatter plot with masked values
Shade regions defined by a logical mask using fill_between
Timeline with lines, dates, and text
Images, contours and fields#
Interactive adjustment of colormap range
Colormap normalizations SymLogNorm
Contouring the solution space of optimizations
Blend transparency with color in 2D images
Modifying the coordinate formatter
Contour plot of irregularly spaced data
Layer images with alpha blending
Visualize matrices with matshow
Multiple images with one colorbar
Advanced quiver and quiverkey functions
Subplots, axes and figures#
Programmatically control subplot adjustment
Controlling view limits using margins and sticky_edges
Draw regions that span an Axes
Resize Axes with constrained layout
Different scales on the same Axes
Figure size in different units
Figure labels: suptitle, supxlabel, supylabel
Combine two subplots using subplots and GridSpec
Gridspec for multi-column/row subplot layouts
Manage multiple figures in pyplot
Create multiple subplots using plt.subplots
Statistics#
Artist customization in box plots
Box plots with custom fill colors
Box plot vs. violin plot comparison
Separate calculation and plotting of boxplots
Plot a confidence ellipse of a two-dimensional dataset
Different ways of specifying error bars
Including upper and lower limits in error bars
Create boxes from error bars using PatchCollection
Demo of the histogram function's different histtype settings
The histogram (hist) function with multiple data sets
Histogram bins, density, and weight
Multiple histograms side by side
Pie and polar charts#
Error bar rendering on polar axis
Text, labels and annotations#
Angle annotations on bracket arrows
The difference between \dfrac and \frac
Format ticks using engineering notation
Annotation arrow style reference
Fonts demo (object-oriented style)
Fonts demo (keyword arguments)
Legend using pre-defined labels
Concatenate text objects with different properties
Render math equations using TeX
Controlling style of text and labels using a dictionary
Text rotation angle in data coordinates
Color#
For a description of the colormaps available in Matplotlib, see the colormaps tutorial.
Colors in the default property cycle
Create a colormap from a list of colors
Selecting individual colors from a colormap
Ways to set a color's alpha value
Shapes and collections#
Reference for Matplotlib artists
Line, Poly and RegularPoly Collection with autoscaling
Ellipse with orientation arrow demo
Plot multiple lines using a LineCollection
Style sheets#
Bayesian Methods for Hackers style sheet
Module - pyplot#
Text and mathtext using pyplot
Module - axes_grid1#
HBoxDivider and VBoxDivider demo
Show RGB channels using RGBAxes
Control the position and size of a colorbar with Inset Axes
Per-row or per-column colorbars
Axes with a fixed physical size
ImageGrid cells with a fixed aspect ratio
Make room for ylabel using axes_grid
Align histogram to scatter plot using locatable Axes
Module - axisartist#
Simple axis tick label and tick directions
Showcase#
Integral as the area under a curve
Shaded & power normalized rendering
Pan/zoom events of overlapping axes
Animation#
Animated image using a precomputed list of images
Event handling#
Matplotlib supports event handling with a GUI neutral event model, so you can connect to Matplotlib events without knowledge of what user interface Matplotlib will ultimately be plugged in to. This has two advantages: the code you write will be more portable, and Matplotlib events are aware of things like data coordinate space and which axes the event occurs in so you don't have to mess with low level transformation details to go from canvas space to data space. Object picking examples are also included.
Figure/Axes enter and leave events
Miscellaneous#
Identify whether artists intersect
Add lines directly to a figure
Building histograms using Rectangles and PolyCollections
Rasterization for vector graphics
3D plotting#
Clip the data to the axes view limits
Create 2D bar graphs in different planes
Plot contour (level) curves in 3D
Plot contour (level) curves in 3D using the extend3d option
Project contour profiles onto a graph
Project filled contour onto a graph
Custom hillshading in a 3D surface plot
Create 3D histogram of 2D data
3D surface with polar coordinates
Triangular 3D filled contour plot
3D voxel plot of the NumPy logo
3D voxel / volumetric plot with RGB colors
3D voxel / volumetric plot with cylindrical coordinates
3D wireframe plots in one direction
Scales#
These examples cover how different scales are handled in Matplotlib.
Specialty plots#
Radar chart (aka spider or star chart)
Long chain of connections using Sankey
SkewT-logP diagram: using transforms and custom projections
Spines#
Ticks#
Automatically setting tick positions
Format date ticks using ConciseDateFormatter
Placing date ticks using recurrence rules
Date tick locators and formatters
Custom tick formatter for time series
SI prefixed offsets and natural order of magnitudes
Set default y-axis tick labels on the right
Setting tick labels from a list of values
Move x-axis tick labels to the top
Units#
These examples cover the many representations of units in Matplotlib.
Embedding Matplotlib in graphical user interfaces#
You can embed Matplotlib directly into a user interface application by following the embedding_in_SOMEGUI.py examples here. Currently Matplotlib supports PyQt/PySide, PyGObject, Tkinter, and wxPython.
When embedding Matplotlib in a GUI, you must use the Matplotlib API directly rather than the pylab/pyplot procedural interface, so take a look at the examples/api directory for some example code working with the API.
Embed in GTK3 with a navigation toolbar
Embed in GTK4 with a navigation toolbar
mplcvd -- an example of figure hook
Embed in a web application server (Flask)
Widgets#
Examples of how to write primitive, but GUI agnostic, widgets in matplotlib
Select indices from a collection using polygon selector
Image scaling using a RangeSlider
Rectangle and ellipse selectors
Snap sliders to discrete values