Python 3 Adding a Colorbar with Matplotlib

Viewed 4892

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 Answers
Related