Wednesday, May 22, 2013

Adding a colorbar to a plot


# Create the maps in a subset of the figure: we leave space for the colorbar v = viz.plot_map(data, affine, axes=[0., 0., 0.9, 1.])
# Create a dedicated Axis for the colorbar cax = v.axes['z'].ax.get_figure().
add_axes([0.9, 0.1, 0.03, 0.8])
# Add the colorbar relatively to the "seconde layer" of the last map colorbar(v.axes['z'].ax.
images[1], cax=cax)



To correctly position the colorbar you can first create an axis/figure that you pass as plot_map arguments.

fig = pl.figure(figsize=(6, 3), facecolor='w', edgecolor='w')
ax = pl.axes([.02, .0, .83, .95], axisbg='w')
slicer = plot_map(tag_map, affine, title=title, cmap=pl.cm.hot,
                      vmin=-vmax, vmax=vmax, figure=fig, axes=ax)
cax = slicer.axes['z'].ax.get_figure().add_axes([.87, .1, .03, .8])
pl.colorbar(slicer.axes['x'].ax.images[1], cax=cax)