Draw a color bar towards the line chart using Matplotlib

I am trying to add a color chart on a graph, but I do not understand how this works. The problem is that I create my own color code:

x = np.arange(11) ys = [i+x+(i*x)**2 for i in range(11)] colors = cm.rainbow(np.linspace(0, 1, len(ys))) 

and colors[i] will give me a new color. Then I use (home-made) functions to select the appropriate data and build it accordingly. It will look something like this:

 function(x,y,concentration,temperature,1,37,colors[0]) function(x,y,concentration,temperature,2,37,colors[1]) # etc 

Now I want to add colors to the color bar, with shortcuts that I can change. How to do it?

I saw several examples where you build all the data as one array with automatic color bars, but here I draw the data one by one (using functions to select the appropriate data).

EDIT:

the function (x, y, concentration, temperature, 1.37, colors [0]) looks like this (simplified):

 def function(x,y,c,T,condition1,condition2,colors): import matplotlib.pyplot as plt i=0 for element in c: if element == condition1: if T[i]==condition2: plt.plot(x,y,color=colors,linewidth=2) i=i+1 return 
+4
python matplotlib colorbar
Oct 24 '14 at 10:10
source share
1 answer

Drawing a color panel towards the line graph

Please draw my solution (I used only 8 sines of different amplitudes) to your problem (as I said, it is difficult to understand from what you wrote in your Q).

 import matplotlib import numpy as np from matplotlib import pyplot as plt # an array of parameters, each of our curves depend on a specific # value of parameters parameters = np.linspace(0,10,11) # norm is a class which, when called, can normalize data into the # [0.0, 1.0] interval. norm = matplotlib.colors.Normalize( vmin=np.min(parameters), vmax=np.max(parameters)) # choose a colormap c_m = matplotlib.cm.cool # create a ScalarMappable and initialize a data structure s_m = matplotlib.cm.ScalarMappable(cmap=c_m, norm=norm) s_m.set_array([]) # plotting 11 sines of varying amplitudes, the colors are chosen # calling the ScalarMappable that was initialised with c_m and norm x = np.linspace(0,np.pi,31) for parameter in parameters: plt.plot(x, parameter*np.sin(x), color=s_m.to_rgba(parameter)) # having plotted the 11 curves we plot the colorbar, using again our # ScalarMappable plt.colorbar(s_m) # That all, folks plt.show() 

Example

Curves + colormap

Confirmations

Similar problem regarding scatter plot

+9
Oct 25 '14 at 12:59 on
source share



All Articles