Skip to content

Instantly share code, notes, and snippets.

@windstriver
Last active August 30, 2015 00:43
Show Gist options
  • Save windstriver/eb0f4919d339116b696c to your computer and use it in GitHub Desktop.
Save windstriver/eb0f4919d339116b696c to your computer and use it in GitHub Desktop.
A simple Python script illustrating the architecture of matplotlib http://www.aosabook.org/en/matplotlib.html
# A simple Python script illustrating the architecture of matplotlib.
# It defines the backend, connects a Figure to it,
# uses the array library numpy to create 10,000 normally distributed random
# numbers, and plots a histogram of these.
# http://www.aosabook.org/en/matplotlib.html
# Import the FigureCanvas from the backend of your choice
# and attach the Figure artist to it.
import numpy as np
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
fig = Figure()
canvas = FigureCanvas(fig)
# Import the numpy library to generate the random numbers.
x = np.random.randn(10000)
# Now use a figure method to create an Axes artist; the Axes artist is
# added automatically to the figure container fig.axes.
# Here "111" is from the MATLAB convention: create a grid with 1 row and 1
# column, and use the first cell in that grid for the location of the new
# Axes.
ax = fig.add_subplot(111)
# Call the Axes method hist to generate the histogram; hist creates a
# sequence of Rectangle artists for each histogram bar and adds them
# to the Axes container. Here "100" means create 100 bins.
ax.hist(x, 100)
# Decorate the figure with a title and save it.
ax.set_title('Normal distribution with $\mu=0, \sigma=1$')
fig.savefig('matplotlib_histogram.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment