4

I've done a fair amount of research on adding a colorbar to a plot but I'm still really confused about how to add one. The examples I've seen use different ways of doing so, which just confuses me because I don't get what the "right" way is.

I've seen there is a colorbar method and a colorbar() function, so what should one use to simply add a colorbar?

Some examples do this:

fig,ax = plt.subplots()
s = ax.scatter(x,y,cmap = coolwarm)
matplotlib.colorbar.ColorbarBase(ax=ax, cmap=coolwarm, values=sorted(v),
                             orientation="horizontal")

While some others simply call the function:

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
image = np.random.poisson(10., (100, 80))
i = ax.imshow(image, interpolation='nearest')
fig.colorbar(i)

I'm probably missing something here, but I just don't see how these both create a colorbar (I just copied the code for the colorbar and excluded that of the data).

My question is simply: what is the simplest way to add a colorbar to a plot? Thanks!

1 Answer 1

4

The first example you quote creates an instance of ColorbarBase. This is usually not the recommended way; there might be some exceptions, but in general there is absolutely no reason to use this.

The second example you quote is one or even the way to create a colorbar inside a figure. Using this, you are on the save side. Using the colorbar method of the figure instance makes it clear in which figure to place the colorbar and supplying the respective ScalarMappable (in this case an AxesImage) ensures that the colorbar uses the correct colors from that ScalarMappable.

fig, ax = plt.subplots()
im = ax.imshow(image)
fig.colorbar(im)

or

fig, ax = plt.subplots()
sc = ax.scatter(x,y, c=something)
fig.colorbar(sc)

There is an even easier method, which would be to simply call

plt.colorbar()

Note however that this may lead to confusions as it tries to automatically determine the plot for which the colorbar should be created. Thus, there is some chance that it fails and I would not recommend using it.

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you so much for your reply! It really helped clear some things up :) I have a few questions: What exactly is a ScalarMappable? I kept getting errors about this and I couldn't find a clear explanation. If I want the color bar to go from red to green for example, or if I want all the colors, how would I make c equal to that so that python knows I want that color gradient?
A ScalarMappable is an object which combines a colormap and a normalization such that it is able to determine which values it hosts should have which color. You normally don't create this yourself but this is what a plotting command like imshow or scatter will return. The color gradient would be produced by using a colormap. Choose any of those shown here and supply it like cmap="RdYlGn" to the plotting function. c can be any list of array that has the same size as x and y, one value per point.
Is the data that goes in "c" RGB values? Or what exactly do I write in that list?
I don't know, you need to decide. Usually you have a quantity that you want to map to colors, right? This quantity goes as argument to the c. plt.scatter([1,2,3],[3,2,1],c=[6,7,8], cmap="RdYlGn").
Alright, I understand everything now. Thanks! Last question though, when using this code: fig, ax = plt.subplots() sc = ax.scatter(theta_polar_rad,r_polar) where can I specifiy that I want the graph to be a polar graph? I was using: plt.axes(polar=True) before but that doesn't seem to work with this new code. Some other people use: ax = plt.subplot(111, projection='polar') c = plt.scatter(theta, r, cmap=plt.cm.hsv) but I can't add the projection = 'polar' to my code because I'm using a different function.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.