matplotlib

Table Of Contents

Previous topic

Annotating Axes

Next topic

Choosing Colormaps

This Page

Screenshots

Here you’ll find a host of example plots with the code that generated them.

Simple Plot

Here’s a very basic plot() with text labels:

(Source code, png, hires.png, pdf)

../_images/simple_plot1.png

Subplot demo

Multiple axes (i.e. subplots) are created with the subplot() command:

(Source code, png, hires.png, pdf)

../_images/subplot_demo3.png

Histograms

The hist() command automatically generates histograms and returns the bin counts or probabilities:

(Source code, png, hires.png, pdf)

../_images/histogram_demo_features2.png

Path demo

You can add arbitrary paths in matplotlib using the matplotlib.path module:

(Source code, png, hires.png, pdf)

../_images/path_patch_demo1.png

mplot3d

The mplot3d toolkit (see mplot3d tutorial and mplot3d Examples) has support for simple 3d graphs including surface, wireframe, scatter, and bar charts.

(Source code, png, hires.png, pdf)

../_images/surface3d_demo4.png

Thanks to John Porter, Jonathon Taylor, Reinier Heeres, and Ben Root for the mplot3d toolkit. This toolkit is included with all standard matplotlib installs.

Streamplot

The streamplot() function plots the streamlines of a vector field. In addition to simply plotting the streamlines, it allows you to map the colors and/or line widths of streamlines to a separate parameter, such as the speed or local intensity of the vector field.

(Source code)

This feature complements the quiver() function for plotting vector fields. Thanks to Tom Flannaghan and Tony Yu for adding the streamplot function.

Ellipses

In support of the Phoenix mission to Mars (which used matplotlib to display ground tracking of spacecraft), Michael Droettboom built on work by Charlie Moad to provide an extremely accurate 8-spline approximation to elliptical arcs (see Arc), which are insensitive to zoom level.

(Source code, png, hires.png, pdf)

../_images/ellipse_demo1.png

Bar charts

Bar charts are simple to create using the bar() command, which includes customizations such as error bars:

(Source code, png, hires.png, pdf)

../_images/barchart_demo3.png

It’s also simple to create stacked bars (bar_stacked.py), candlestick bars (finance_demo.py), and horizontal bar charts (barh_demo.py).

Pie charts

The pie() command allows you to easily create pie charts. Optional features include auto-labeling the percentage of area, exploding one or more wedges from the center of the pie, and a shadow effect. Take a close look at the attached code, which generates this figure in just a few lines of code.

(Source code, png, hires.png, pdf)

../_images/pie_demo_features1.png

Table demo

The table() command adds a text table to an axes.

(Source code, png, hires.png, pdf)

../_images/table_demo1.png

Scatter demo

The scatter() command makes a scatter plot with (optional) size and color arguments. This example plots changes in Google’s stock price, with marker sizes reflecting the trading volume and colors varying with time. Here, the alpha attribute is used to make semitransparent circle markers.

(Source code, png, hires.png, pdf)

../_images/scatter_demo21.png

Slider demo

Matplotlib has basic GUI widgets that are independent of the graphical user interface you are using, allowing you to write cross GUI figures and widgets. See matplotlib.widgets and the widget examples.

(Source code, png, hires.png, pdf)

../_images/slider_demo.png

Fill demo

The fill() command lets you plot filled curves and polygons:

(Source code, png, hires.png, pdf)

../_images/fill_demo2.png

Thanks to Andrew Straw for adding this function.

Date demo

You can plot date data with major and minor ticks and custom tick formatters for both.

(Source code, png, hires.png, pdf)

../_images/date_demo3.png

See matplotlib.ticker and matplotlib.dates for details and usage.

Financial charts

You can make sophisticated financial plots by combining the various plot functions, layout commands, and labeling tools provided by matplotlib. The following example emulates one of the financial plots in ChartDirector:

(Source code, png, hires.png, pdf)

../_images/finance_work21.png

Basemap demo

Jeff Whitaker’s Basemap add-on toolkit makes it possible to plot data on many different map projections. This example shows how to plot contours, markers and text on an orthographic projection, with NASA’s “blue marble” satellite image as a background.

(Source code, png, hires.png, pdf)

../_images/plotmap.png

Log plots

The semilogx(), semilogy() and loglog() functions simplify the creation of logarithmic plots.

(Source code, png, hires.png, pdf)

../_images/log_demo2.png

Thanks to Andrew Straw, Darren Dale and Gregory Lielens for contributions log-scaling infrastructure.

Polar plots

The polar() command generates polar plots.

(Source code, png, hires.png, pdf)

../_images/polar_demo1.png

Legends

The legend() command automatically generates figure legends, with MATLAB-compatible legend placement commands.

(Source code, png, hires.png, pdf)

../_images/legend_demo5.png

Thanks to Charles Twardy for input on the legend command.

Mathtext_examples

Below is a sampling of the many TeX expressions now supported by matplotlib’s internal mathtext engine. The mathtext module provides TeX style mathematical expressions using freetype2 and the BaKoMa computer modern or STIX fonts. See the matplotlib.mathtext module for additional details.

(Source code)

(png, hires.png, pdf)

../_images/mathtext_examples_01_001.png

Matplotlib’s mathtext infrastructure is an independent implementation and does not require TeX or any external packages installed on your computer. See the tutorial at Writing mathematical expressions.

Native TeX rendering

Although matplotlib’s internal math rendering engine is quite powerful, sometimes you need TeX. Matplotlib supports external TeX rendering of strings with the usetex option.

(Source code, png, hires.png, pdf)

../_images/tex_demo1.png

EEG demo

You can embed matplotlib into pygtk, wx, Tk, FLTK, or Qt applications. Here is a screenshot of an EEG viewer called pbrain, which is part of the NeuroImaging in Python suite NIPY.

../_images/eeg_small.png

The lower axes uses specgram() to plot the spectrogram of one of the EEG channels.

For examples of how to embed matplotlib in different toolkits, see:

XKCD-style sketch plots

matplotlib supports plotting in the style of xkcd.

(Source code)