I have a series of lines (currently 60 in total) that I want to plot to the same figure to show the time evolution of a certain process. The lines currently are plotted so that the earliest time step is plotted in 100% red, the latest time step is plotted in 100% blue, and the time steps in the middle are some mixture of red and blue based on what time they are at (the amount of red decreases linearly with increasing time, while the amount of blue increases linearly with increasing time; a simple color gradient). I would like to make a (preferably vertical) color bar of some kind that shows this in a continuous fashion. What I mean by that is I want a color bar that is red on the bottom, blue on the top, and some mixture of red and blue in the middle of the bar that smoothly transitions from red to blue in the same manner as the lines I plot. I also want to put axes on this color bar so that I can show which color corresponds to which timestep.
I've read the documentation for matplotlib.pyplot.colorbar()
but wasn't able to figure out how to do what I wanted to do without using one of matplotlib's previously defined colormaps. My guess is that I'll need to define my own colormap that transitions from red to blue and then it will be relatively simple to feed that to matplotlib.pyplot.colorbar()
and get the color bar that I want.
Here is an example of the code I use to plot the lines:
import numpy as np
from matplotlib import pyplot as pp
x= ##x-axis values for plotting
##start_time is the time of the earliest timestep I want to plot, type int
##end_time is the time of the latest timestep I want to plot, type int
for j in range(start_time,end_time+1):
##Code to read data in from time step number j
y = ##the data I want to plot
red = 1. - (float(j)-float(start_time))/(float(end_time)-float(start_time))
blue = (float(j)-float(start_time))/(float(end_time)-float(start_time))
pp.plot(bin_center,spectrum,color=(red,0,blue))
pp.show()
EDIT:
Maybe this will make it clearer what I mean. Below is my figure:
Each line shows the relation between the x-values and the y-values at a different time step. The red lines are earlier time steps, the blue lines are later time steps, and the purple lines are in the middle, as I defined above. With this already plotted, how would I create a color bar (vertically on the right side if possible) mapping the colors of the lines (continuously) to the time value of the time step each color represents?