Folks, I was trying to use an object oriented approach to creating
colorbars, but I could manage to make it work. Currently I have close
to what I am trying to achieve, but the last bits are missing. The
outstanding issues are:
1) I only need one colorbar, how would I create a single colorbar on
the right that spanned across all axes? (ie. same height as the stack)
2) is there a way to place the colorbar in the bottom middle of my
panels (if I end up with more than one)?
3) How can I customize the tick labels of the colorbar?
4) Is this a 'pythonic' approach to begin with?
Here's my code, and a fake class that should provide the data needed
to use the example:
#!/usr/bin/env python
"""
Demo to help understand plotting tools
"""
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
class Fun(object):
""" Dummy class for testing
"""
class body:
pass
def __init__(self):
body = Fun.body
body.time = np.arange(1000)
body.alt = np.sin(body.time)
body.A = 360*np.random.random_sample(1000)-180
body.B = 180*np.random.random_sample(1000)-90
body.C = 90*np.random.random_sample(1000)-45
def plot_angles(F):
""" Plots the angles of body """
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
# Set up plotting environment colors
Nc = np.arange(-180,181,1)
norm = mpl.colors.normalize(Nc.min(),Nc.max())
fig, (ax1, ax2, ax3) = plt.subplots(3, sharex=True, sharey=True)
ax1.scatter(F.body.time,F.body.alt,c=F.body.A,norm=norm,label='A',edgecolor='none')
ax2.scatter(F.body.time,F.body.alt,c=F.body.B,norm=norm,label='B',edgecolor='none')
ax3.scatter(F.body.time,F.body.alt,c=F.body.C,norm=norm,label='C',edgecolor='none')
# Fine-tune figure; make subplots close to each other and hide x ticks for
# all but bottom plot.
fig.subplots_adjust(hspace=0)
plt.setp([a.get_xticklabels() for a in fig.axes[:-1]], visible=False)
for a in fig.axes:
ia = inset_axes(a,width="20%", height="5%", loc=4)
a.set_xlim(F.body.time[0],F.body.time[-1])
fig.add_subplot(a)
plt.colorbar(a.collections[0],cax=ia,orientation='horizontal')
ia.xaxis.set_ticks_position("top")
plt.draw()
return fig
def demo():
F = Fun()
plot_angles(F)
if __name__ == "__main__":
fig = demo()
plt.show()