I am trying to make a simple plot that shows a lot of curves that belong to a large dataset with a certain order, let's say plot 1..n. The shape of the curves changes gradually with increasing n. It is not important that readers can see exactly which plot belongs to which value of n, but they should be able to guess in which order of magnitude n is.
I therefore do something like this:
nValues = range(0,30)
xValues = np.linspace(0,10)
dataset = [(xValues-5-0.5*n)**2 for n in nValues]
colors = {n: colorsys.hsv_to_rgb(hue,0.9,0.7) for n,hue in zip(nValues,np.linspace(0,0.7,len(nValues)))}
for n in nValues:
plt.plot(dataset[n],color=colors[n])
(Just to be clear, this is just for the example, my data is actually stored in a nice pandas dataframe.)
Instead of a legend, I would like to add a colorbar next to the plot with perhaps a couple of tickmarks and labels (at least the minimum and the maximum), to indicate which color belongs to which value of n, but I don't know how to accomplish this. I figured things might be easier if I actually get my plot colors from a ColorMap, but I also don't know how to do that and I also wouldn't know how to proceed from there.
Any pointers are welcome!
Both @tom and @Joe Kington are right: this has been asked before. However, I tried to make an example with slighty less efforts as the linked answers. To use a colormap (which always maps values from [0,1] to color), you first need to normalize your data. For that you can use the
Normalize
class. TheScalarMappable
instance is only required if you intend to create a colorbar.Result: