|
From: Claus <cla...@gm...> - 2012-12-13 15:46:37
|
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()
|
|
From: Benjamin R. <ben...@ou...> - 2012-12-13 15:57:21
|
On Thu, Dec 13, 2012 at 10:45 AM, Claus <cla...@gm...> 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 Ben Root |
|
From: Steven B. <bo...@ph...> - 2012-12-13 18:00:58
|
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 <cla...@gm... > <mailto:cla...@gm...>> 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 > > 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 > http://p.sf.net/sfu/logmein_12329d2d > > > _______________________________________________ > Matplotlib-users mailing list > Mat...@li... > https://lists.sourceforge.net/lists/listinfo/matplotlib-users -- Steven Boada Doctoral Student Dept of Physics and Astronomy Texas A&M University bo...@ph... |
|
From: Sterling S. <sm...@fu...> - 2012-12-13 16:11:31
|
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 > Mat...@li... > https://lists.sourceforge.net/lists/listinfo/matplotlib-users |