Skip to content

Instantly share code, notes, and snippets.

@tloredo
Created April 15, 2015 06:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tloredo/e79e381cfb8b2b4c0e60 to your computer and use it in GitHub Desktop.
Save tloredo/e79e381cfb8b2b4c0e60 to your computer and use it in GitHub Desktop.
GridSpec example for BVNDemo IPython notebook
# This won't run as a standalone Python script; put it in a cell at
# the bottom of your BVNDemo IPy notebook and run it there.
# Get BVN samples to plot.
bvn = BivariateNormal([0,0], [1,1], .7)
xyvals = bvn.sample(500)
# We'll put 3 different, related axes in a single new figure.
fig = figure(figsize=(8,6))
# Use mpl's GridSpec capability, via the subplot2grid() helper function,
# to put axes in the figure; see: http://matplotlib.org/users/gridspec.html
# Use the sharex, sharey arguments to link axes.
xy_ax = subplot2grid((3,3), (1,0), colspan=2, rowspan=2)
x_ax = subplot2grid((3,3), (0,0), colspan=2, sharex=xy_ax)
y_ax = subplot2grid((3,3), (1,2), rowspan=2, sharey=xy_ax)
setp(x_ax.get_xticklabels(), visible=False) # hide x labels on top axes
setp(y_ax.get_yticklabels(), visible=False) # hide y labels on right axes
xy_ax.scatter(xyvals[:,0], xyvals[:,1]) # scatter plot in x,y axes
x_ax.hist(xyvals[:,0], alpha=.3) # x histo on top
y_ax.hist(xyvals[:,1], orientation='horizontal', alpha=.3) # y histo on side
# Label axes!
xy_ax.set_xlabel('$x$')
xy_ax.set_ylabel('$y$')
x_ax.set_ylabel('Counts')
y_ax.set_xlabel('Counts')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment