1

I'm having trouble creating a colorbar for my plot in Python using matplotlib. I am using a colormap, not to colour all the data that I plot but to extract a colour for a plot based on a value I'm not plotting. Hope this makes sense.. So I'm in a for loop, create a plot every time with a colour based on a certain parameter. Like this (the data is an example to create an mwe, my data is more complicated):

import matplotlib as mpl
from matplotlib import pyplot as plt
import numpy as np

xdata = np.array(range(10))
parameter = [0.5, 0.3, 0.78, 0.21, 0.45] #random parameter example

cmap = mpl.cm.get_cmap('jet')

for i in range(len(parameter)):
    clr = cmap(parameter(i))
    plt.plot(xdata,xdata**i,c=clr)

plt.show()

Now, what I would want is a colorbar on the side (or actually two, but that's another problem I think) that shows the jet colormap and according values. The values need to be scaled to a new min and max value.

So far I've found the following, but I don't understand it enough to apply it to my own problem:

Getting individual colors from a color map in matplotlib Which told me how to extract the colour and shows how to create the normalized colormap

Colorbar only Which should tell me how to add a colorbar without using the plotted data, but I don't understand enough of it. My problem is with the creation of the axes. I don't understand this part if I want to put the colorbar next to my plot. In the example they create a figure with handle fig, but in my case the figure is created when I do plt.imshow(image), since this is what I start with and then I'm plotting over the image. I cannot use the fig.add_axes here.

I hope you can help me out here. It would be great if I could also create a 'reversed' colorbar. So either the colours are in reverse direction, or the values next to the bar.

Community
  • 1
  • 1
Iris
  • 131
  • 5
  • At any point in the script you can get the figure via `fig = plt.gcf()` and an axes via `ax=plt.gca()`. So, adding an axes may be done by `plt.gcf().add_axes(...)`. There is also nothing wrong with putting `fig=plt.figure()` before plotting anything. Every standard colormap has a reversed version, which has `_r` at the end of its name, e.g. you can use `viridis_r` instead of `viridis`. – ImportanceOfBeingErnest Feb 09 '17 at 08:40
  • Thanks, this sounds like what I need! Unfortunately can't try until tomorrow, will let you know if it works out. I should have thought of the gcf and gca since I've worked with MATLAB plenty of times... -.- – Iris Feb 09 '17 at 08:43
  • Hey thanks this was exactly what I needed. I think I could've/should've known but I just couldn't put it all together anymore so you really helped me out here. If you post it as an answer I'll vote for it ;-) – Iris Feb 10 '17 at 04:29

1 Answers1

0

At any point in the script you can get the figure via fig = plt.gcf() and an axes via ax=plt.gca(). So, adding an axes may be done by plt.gcf().add_axes(...).
There is also nothing wrong with putting fig=plt.figure() before plotting anything.

Note that after creating a new axes, plt.gca() will return the new axes, so it is a good idea to create a reference to the main axes before adding a new one.
A convenient way to obtain a figure and an axes for later referencing is to create the figure via

fig, ax = plt.subplots()

Colormaps: Every standard colormap has a reversed version, which has _r at the end of its name, e.g. you can use viridis_r instead of viridis.

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712