Python: creating a color bar that works from red to blue

I have a number of lines (60 in total) that I want to build in the same figure to show the evolution of time for a particular process. The lines are currently being constructed so that the earliest time step is applied 100% red, the last time step is applied 100% blue, and the time steps in the middle are a mixture of red and blue depending on the time in which they are located (amount of red decreases linearly with increasing time, and 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 it in a continuous way. I mean, I want the bottom panel to have a red bar, blue on top, and some kind of red and blue in the middle of the panel that smoothly transitions from red to blue in the same waylike lines I will speak. I also want to put the axes on this color bar so that I can show which color corresponds to that time value.

I read the documentation for matplotlib.pyplot.colorbar()but could not figure out how to do what I wanted to do without using one of the matplotlib color maps defined earlier. I assume that I will need to define my own color palette, which will go from red to blue, and then it will be quite simple to pass this on matplotlib.pyplot.colorbar()and get the color panel that I want.

Here is an example of the code that I use to build strings:

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 it will become clearer what I mean. Below is my figure: My figure

x y . - , - , , . , ( , ), () , ?

+4
1

.

  • , matplotlib.cm.
  • , matplotlib.pyplot.plot().

red blue, plt.plot(), plt.colorbar() Matplotlib, , ScalarMappable. , plt.plot().

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mcol
import matplotlib.cm as cm

start_time = 100
end_time = 120

# Generate some dummy data.
tim = range(start_time,end_time+1)
xdat = np.arange(0,90.1)
ydat = [np.sin(0.2*(xdat-t)/np.pi) for t in tim]


# Make a user-defined colormap.
cm1 = mcol.LinearSegmentedColormap.from_list("MyCmapName",["r","b"])

# Make a normalizer that will map the time values from
# [start_time,end_time+1] -> [0,1].
cnorm = mcol.Normalize(vmin=min(tim),vmax=max(tim))

# Turn these into an object that can be used to map time values to colors and
# can be passed to plt.colorbar().
cpick = cm.ScalarMappable(norm=cnorm,cmap=cm1)
cpick.set_array([])



F = plt.figure()
A = F.add_subplot(111)
for y, t in zip(ydat,tim):
    A.plot(xdat,y,color=cpick.to_rgba(t))

plt.colorbar(cpick,label="Time (seconds)")

enter image description here

+6

All Articles