Hima
Hima

Reputation: 12054

Colorbar With subplots

How do I get colorbar when there is a figure and two subplots . I want separate colorbar for all subplots . For Example

fig = plt.figure()
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
ax1.set_title('PC')
ax2.set_title('MC')
im=ax1.imshow(topo.sim.PC.activity,interpolation='nearest')
im1=ax2.imshow(topo.sim.MC.activity,interpolation='nearest')

I tried plt.colorbar() and ax1.colorbar() as well. Doesn't seem working. I have animation on both the images in later part of the code.

Upvotes: 0

Views: 196

Answers (1)

Alejandro
Alejandro

Reputation: 3412

If you rewrite your code as follows, then it will work. When using colorbar, you need to specify in which axis you want to put it. This is easy to figure out when looking at the examples in the matplotlib gallery.

fig = plt.figure()
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
ax1.set_title('PC')
ax2.set_title('MC')
im=ax1.imshow(topo.sim.PC.activity,interpolation='nearest')
im1=ax2.imshow(topo.sim.MC.activity,interpolation='nearest')

plt.colorbar(im, ax=ax1)
plt.colorbar(im1, ax=ax2)

If the colour bar is too big, you may want to use shrink kwarg.

Upvotes: 1

Related Questions