colorbars with multiple subplots

Hi,

I am trying to plot a colorbar next to each subplot in a figure. In the following example, I create two figures. In the second figure, I try to add the colorbars. Is there a way to show the colorbar next to each subplot. The way I did it, all the colorbars appear next to the last subplot, take away space from it, and all are plotted using the "jet" colormap.
Unfortunately, I am not sure how to do this better, and would appreciate hints.

import numpy as np
import matplotlib.pylab as plt

def main():
    
    # four subplots, no colorbar, so far so good
    f, axarr = plt.subplots(2, 2)
    axarr[0, 0].imshow(np.random.rand(5,5)*10)
    axarr[0, 1].imshow(np.random.rand(5,5))
    axarr[1, 0].imshow(np.random.rand(5,5)*100)
    axarr[1, 1].imshow(np.random.rand(5,5)*1000)
    plt.show()

    # four subplots, four colorbars
    f, axarr = plt.subplots(2, 2)
    a = axarr[0, 0].imshow(np.random.rand(5,5)*10)
    cbar1 = f.colorbar(a, cmap='jet')
    b = axarr[0, 1].imshow(np.random.rand(5,5))
    cbar2 = f.colorbar(b, cmap='Reds')
    c = axarr[1, 0].imshow(np.random.rand(5,5)*100)
    cbar2 = f.colorbar(c, cmap='Blues')
    d = axarr[1, 1].imshow(np.random.rand(5,5)*1000)
    cbar2 = f.colorbar(d, cmap='Greens')
    plt.show()

if __name__ == '__main__':
    main()

I think you are looking for the AxesGrid object from the mpl_toolkits.axes_grid1 module:

http://matplotlib.org/mpl_toolkits/axes_grid/users/overview.html#axes-grid1

Ben Root

···

On Thu, Dec 13, 2012 at 10:45 AM, Claus <clausenator@…287…> wrote:

Hi,

I am trying to plot a colorbar next to each subplot in a figure. In the following example, I create two figures. In the second figure, I try to add the colorbars. Is there a way to show the colorbar next to each subplot. The way I did it, all the colorbars appear next to the last subplot, take away space from it, and all are plotted using the “jet” colormap.

Unfortunately, I am not sure how to do this better, and would appreciate hints.

import numpy as np

import matplotlib.pylab as plt

def main():

# four subplots, no colorbar, so far so good

f, axarr = plt.subplots(2, 2)

axarr[0, 0].imshow(np.random.rand(5,5)*10)

axarr[0, 1].imshow(np.random.rand(5,5))

axarr[1, 0].imshow(np.random.rand(5,5)*100)

axarr[1, 1].imshow(np.random.rand(5,5)*1000)

plt.show()



# four subplots, four colorbars

f, axarr = plt.subplots(2, 2)

a = axarr[0, 0].imshow(np.random.rand(5,5)*10)

cbar1 = f.colorbar(a, cmap='jet')

b = axarr[0, 1].imshow(np.random.rand(5,5))

cbar2 = f.colorbar(b, cmap='Reds')

c = axarr[1, 0].imshow(np.random.rand(5,5)*100)

cbar2 = f.colorbar(c, cmap='Blues')

d = axarr[1, 1].imshow(np.random.rand(5,5)*1000)

cbar2 = f.colorbar(d, cmap='Greens')

plt.show()

if name == ‘main’:

main()

Claus,

f.colorbar may be trying to place the colorbar on the 'current axes'. Does placing
plt.axes(axarr[0,0])
before each f.colorbar help? Also, the plt.colorbar function [1] (maybe f.colorbar also) can take a keyword argument for the axes in which to draw the colorbar.

-Sterling

[1] http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.colorbar

···

On Dec 13, 2012, at 7:45AM, Claus wrote:

Hi,

I am trying to plot a colorbar next to each subplot in a figure. In the following example, I create two figures. In the second figure, I try to add the colorbars. Is there a way to show the colorbar next to each subplot. The way I did it, all the colorbars appear next to the last subplot, take away space from it, and all are plotted using the "jet" colormap.
Unfortunately, I am not sure how to do this better, and would appreciate hints.

import numpy as np
import matplotlib.pylab as plt

def main():

   # four subplots, no colorbar, so far so good
   f, axarr = plt.subplots(2, 2)
   axarr[0, 0].imshow(np.random.rand(5,5)*10)
   axarr[0, 1].imshow(np.random.rand(5,5))
   axarr[1, 0].imshow(np.random.rand(5,5)*100)
   axarr[1, 1].imshow(np.random.rand(5,5)*1000)
   plt.show()

   # four subplots, four colorbars
   f, axarr = plt.subplots(2, 2)
   a = axarr[0, 0].imshow(np.random.rand(5,5)*10)
   cbar1 = f.colorbar(a, cmap='jet')
   b = axarr[0, 1].imshow(np.random.rand(5,5))
   cbar2 = f.colorbar(b, cmap='Reds')
   c = axarr[1, 0].imshow(np.random.rand(5,5)*100)
   cbar2 = f.colorbar(c, cmap='Blues')
   d = axarr[1, 1].imshow(np.random.rand(5,5)*1000)
   cbar2 = f.colorbar(d, cmap='Greens')
   plt.show()

if __name__ == '__main__':
   main()
------------------------------------------------------------------------------
LogMeIn Rescue: Anywhere, Anytime Remote support for IT. Free Trial
Remotely access PCs and mobile devices and provide instant support
Improve your efficiency, and focus on delivering more value-add services
Discover what IT Professionals Know. Rescue delivers
http://p.sf.net/sfu/logmein_12329d2d
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

I echo using the AxesGrid object from the toolkits. Protip – I have
had trouble making it work with semilog plots. So if that is what
you are doing, it might be worth trying another thing or two first.

Steven
···

On 12/13/12 9:56 AM, Benjamin Root
wrote:

On Thu, Dec 13, 2012 at 10:45 AM, Claus <clausenator@…287…>
wrote:

Hi,

      I am trying to plot a colorbar next to each subplot in a

figure. In the following example, I create two figures. In the
second figure, I try to add the colorbars. Is there a way to
show the colorbar next to each subplot. The way I did it, all
the colorbars appear next to the last subplot, take away space
from it, and all are plotted using the “jet” colormap.

      Unfortunately, I am not sure how to do this better, and would

appreciate hints.

      import numpy as np

      import matplotlib.pylab as plt



      def main():



          # four subplots, no colorbar, so far so good

          f, axarr = plt.subplots(2, 2)

          axarr[0, 0].imshow(np.random.rand(5,5)*10)

          axarr[0, 1].imshow(np.random.rand(5,5))

          axarr[1, 0].imshow(np.random.rand(5,5)*100)

          axarr[1, 1].imshow(np.random.rand(5,5)*1000)

          plt.show()



          # four subplots, four colorbars

          f, axarr = plt.subplots(2, 2)

          a = axarr[0, 0].imshow(np.random.rand(5,5)*10)

          cbar1 = f.colorbar(a, cmap='jet')

          b = axarr[0, 1].imshow(np.random.rand(5,5))

          cbar2 = f.colorbar(b, cmap='Reds')

          c = axarr[1, 0].imshow(np.random.rand(5,5)*100)

          cbar2 = f.colorbar(c, cmap='Blues')

          d = axarr[1, 1].imshow(np.random.rand(5,5)*1000)

          cbar2 = f.colorbar(d, cmap='Greens')

          plt.show()



      if __name__ == '__main__':

          main()
      I think you are looking for the AxesGrid object from the

mpl_toolkits.axes_grid1 module:

      [http://matplotlib.org/mpl_toolkits/axes_grid/users/overview.html#axes-grid1](http://matplotlib.org/mpl_toolkits/axes_grid/users/overview.html#axes-grid1)



      Ben Root
------------------------------------------------------------------------------
LogMeIn Rescue: Anywhere, Anytime Remote support for IT. Free Trial
Remotely access PCs and mobile devices and provide instant support
Improve your efficiency, and focus on delivering more value-add services
Discover what IT Professionals Know. Rescue delivers
_______________________________________________
Matplotlib-users mailing list

-- Steven Boada
Doctoral Student
Dept of Physics and Astronomy
Texas A&M University

http://p.sf.net/sfu/logmein_12329d2dMatplotlib-users@lists.sourceforge.nethttps://lists.sourceforge.net/lists/listinfo/matplotlib-usersboada@…3847…