matplotlib
Fork me on GitHub


Travis-CI:

Table Of Contents

This Page

Sample plots in Matplotlib

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

Line Plot

Here’s how to create a line plot with text labels using plot().

../../_images/sphx_glr_simple_plot_0011.png

Simple Plot

Subplot demo

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

../../_images/sphx_glr_subplot_0011.png

Subplot

Histograms

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

../../_images/sphx_glr_histogram_features_0011.png

Histogram Features

Path demo

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

../../_images/sphx_glr_path_patch_0011.png

Path Patch

mplot3d

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

../../_images/sphx_glr_surface3d_0011.png

Surface3d

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.

../../_images/sphx_glr_plot_streamplot_0011.png

Plot Streamplot

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.

../../_images/sphx_glr_ellipse_demo_0011.png

Ellipse Demo

Bar charts

Use the bar() command to make bar charts, which includes customizations such as error bars:

../../_images/sphx_glr_barchart_demo_0011.png

Barchart Demo

You can also create stacked bars (bar_stacked.py), or horizontal bar charts (barh.py).

Pie charts

The pie() command allows you to 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.

../../_images/sphx_glr_pie_features_0011.png

Pie Features

Table demo

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

../../_images/sphx_glr_table_demo_0011.png

Table Demo

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.

../../_images/sphx_glr_scatter_demo2_0011.png

Scatter Demo2

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.

../../_images/sphx_glr_slider_demo_0011.png

Slider Demo

Fill demo

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

../../_images/sphx_glr_fill_0011.png

Fill

Thanks to Andrew Straw for adding this function.

Date demo

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

../../_images/sphx_glr_date_0011.png

Date

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

Log plots

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

../../_images/sphx_glr_log_demo_0011.png

Log Demo

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

Polar plots

The polar() command generates polar plots.

../../_images/sphx_glr_polar_demo_0011.png

Polar Demo

Legends

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

../../_images/sphx_glr_legend_0011.png

Legend

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 FreeType and the DejaVu, BaKoMa computer modern, or STIX fonts. See the matplotlib.mathtext module for additional details.

../../_images/sphx_glr_mathtext_examples_0011.png

Mathtext Examples

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.

../../_images/sphx_glr_tex_demo_0011.png

Tex Demo

EEG demo

You can embed Matplotlib into pygtk, wx, Tk, or Qt applications. Here is a screenshot of an EEG viewer called pbrain.

../../_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.

../../_images/sphx_glr_xkcd_0011.png

Xkcd

../../_images/sphx_glr_sample_plots_001.png
# An extra set of sample plots for the thumbnail image.
import matplotlib.pyplot as plt
import numpy as np

np.random.seed(19680801)
data = np.random.randn(2, 100)

fig, axs = plt.subplots(2, 2, figsize=(5, 5))
axs[0, 0].hist(data[0])
axs[1, 0].scatter(data[0], data[1])
axs[0, 1].plot(data[0], data[1])
axs[1, 1].hist2d(data[0], data[1])

plt.show()

Total running time of the script: ( 0 minutes 0.075 seconds)

Gallery generated by Sphinx-Gallery