Drawing a colorbar aside a line plot, using Matplotlib
Asked Answered
T

1

6

I'm trying to add a color bar in a graph, but I don't understand how it works. The problem is that I make my own colorcode by:

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 (homemade) functions to select the relevant data and plot them accordingly. This would 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 the colors in a color bar, with labels I can change. How do I do this?

I have seen several examples where you plot all the data as one array, with automated color bars, but here I plot the data one by one (by using functions to select the relevant data).

EDIT:

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
Transalpine answered 24/10, 2014 at 10:10 Comment(7)
I am assuming function will eventually call a "plotting" function or similar. There you will need to pass your colour. Can you post the content of function?Went
Couldn't you remove references to ys in the question? or does ys in the first box become y in the second one?Luther
My understanding of your Q: (1) you plot repeatedly a curve whose shape and color depend on a parameter, that is monotonically varying; (2) afterwards, you want to give an hint at parameter value for the different curves using a pl.colorbar. If I'm correct, it's difficult to do, because colorbars are designed to play nice with pl.imshow and pl.countourf rather than with pl.plot. That said, you should be able to use two subplots, in the first one your plot and in the second a colorbar drawn on its own, as it is explained/exemplified in matplotlib.org/examples/api/colorbar_only.htmlLuther
Thanks for the link, only I don't understand how that example works. I have seen the example before posting this, but I couldn't get it to work for my purpose. What is 'cmap' in this example? What does it contain? And what is 'norm'?Transalpine
@KayJansen It's a complex task, you either find on SO a good-hearted matplotlib specialist (that I am not, sorry) or you're mostly on your own. Besides, I'm a bit astonished by the fact that you didn't mention your previous research in your question, but this is your 1st Q in SO... if you intend to hang about you'd better read something about asking good questions. CiaoLuther
@KayJansen ps: try to add a matplotlib tag to your question, or ask anew the question using the 'python' and 'matplotlib' tags. For your info the tag 'colorbar' has exactly 0 followers.Luther
@KayJansen Never mind, I edited your Q myself: I tried to give a more meaningful title and added the matplotlib tag. Let's see if these edits are approved.Luther
L
16

Drawing a colorbar aside a line plot

Please map my solution (I used simply 11 sines of different amplitudes) to your problem (as I told you, 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's all, folks
plt.show()

Example

Curves+colormap

Acknowledgements

A similar problem, about a scatter plot

Update — April 14, 2021

  1. With recent versions of Matplotlib, the statement s_m.set_array([]) is not required any more. On the other hand, it does no harm.
  2. When plotting, in place of color=s_m.to_rgba(parameter) one may want to use the (slightly) more obvious color=c_m(norm(parameter)).
Luther answered 25/10, 2014 at 12:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.