PLOTS plot_colorbarΒΆ

# -*-Python-*-
# Created by bgrierson at 30 Aug 2017  11:38

"""
This script uses a continuous colorbar for a sequence of lines, and makes a colorbar to go with it.

"""

x = linspace(0, 2.0 * np.pi, 31)
t = linspace(0, 1, 21)
tt, xx = meshgrid(t, x)
y = np.sin(xx - tt)

# make color map for time, where None defaults to
cmap = None
nm = matplotlib.colors.Normalize(t.min(), t.max())
sm = matplotlib.cm.ScalarMappable(cmap=cmap, norm=nm)
sm.set_array(t)
colors = sm.cmap(sm.norm(t))

fig, ax = plt.subplots()
for k in range(len(t)):
    ax.plot(x, y[:, k], color=colors[k])

cb = fig.colorbar(sm, ax=ax, use_gridspec=True)
cb.set_label('Time [ms]')