placing colorbar when using subplot command

I’m relatively new to matplotlib. Trying to place a colorbar in a figure. The code below, placed in a file and executed with python, draws 4 maps using basemap. I’ve been unable to get a colorbar to show up anywhere on the figure. Ideally I would like the option of placing a colorbar across the bottom, spanning across both bottom map panels. Also would need the option of placing a colorbar either to the right of or below each map. Uncommenting the two lines under “Here make a colorbar” cause an error. I’ve used those commands when creating just one map using the figure command.

TIA,

Mike

verbose=0 #verbose=2 says a bit more
import sys,getopt
from mpl_toolkits.basemap import Basemap, shiftgrid, cm
#from
netCDF3 import Dataset as NetCDFFile
from mpl_toolkits.basemap import NetCDFFile
from pylab import *

alloptions, otherargs= getopt.getopt(sys.argv[1:],‘ro:p:X:Y:v:t:l:u:n:’) # note the : after o and p
proj=‘lam’

cmap = cm.get_cmap(‘jet’, 10) # 10 discrete colors

print “\nPlotting, please wait…maybe more than 10 seconds”
if proj==‘lam’: #Lambert Conformal
m = Basemap(llcrnrlon=-80.6,llcrnrlat=38.4,urcrnrlon=-66.0,urcrnrlat=47.7,
resolution=‘l’,area_thresh=1000.,projection=‘lcc’,
lat_1=65.,lon_0=-73.3)
xtxt=200000. #offset for text
ytxt=200000.
parallels = arange(38.,48.,2.)
meridians = arange(-80.,-64.,2.)

xsize = rcParams[‘figure.figsize’][0]
fig=figure(figsize=(xsize,m.aspect*xsize))
cax = axes([0.88, 0.1,
0.06, 0.81]) # colorbar axes for map w/ graticule

···

############################################################################################
subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=0.15, hspace=0.11)

Make the first map at upper left

sp = plt.subplot(2,2,1)

Here make a colorbar.

#cax = axes([0.88, 0.1, 0.06, 0.81]) # colorbar axes for map w/ graticule
#colorbar(format=’%3.1f’, ticks=[-1.5, -1.2, -0.9, -0.6, -0.3, 0.0, 0.3, 0.6, 0.9, 1.2, 1.5], cax=cax)

draw coastlines and political boundaries.

m.drawcoastlines()
m.drawcountries()
m.drawstates()

Make the second map

sp = plt.subplot(2,2,2)

draw coastlines and political boundaries.

m.drawcoastlines()
m.drawcountries()
m.drawstates()

Make the third map

#########################################################################
sp = plt.subplot(2,2,3)

draw coastlines and political boundaries.

m.drawcoastlines()
m.drawcountries()
m.drawstates()

Make the fourth map

sp = plt.subplot(2,2,4)

draw coastlines and political boundaries.

m.drawcoastlines()
m.drawcountries()
m.drawstates()

plt.show()
plt.savefig(“map.eps”)
plt.clf() # Clears the figure object

Mike,

Try using the axes_grid1 toolkit to produce your axes objects and to allocate enough room for colorbars.

http://matplotlib.sourceforge.net/mpl_toolkits/axes_grid/index.html

Cheers!
Ben Root

P.S. - a little history, there used to be an axes_grid toolkit, but has since been superseded by axes_grid1.

···

On Tue, Jan 17, 2012 at 9:30 AM, Michael Rawlins <rawlins02@…120…9…> wrote:

I’m relatively new to matplotlib. Trying to place a colorbar in a figure. The code below, placed in a file and executed with python, draws 4 maps using basemap. I’ve been unable to get a colorbar to show up anywhere on the figure. Ideally I would like the option of placing a colorbar across the bottom, spanning across both bottom map panels. Also would need the option of placing a colorbar either to the right of or below each map. Uncommenting the two lines under “Here make a colorbar” cause an error. I’ve used those commands when creating just one map using the figure command.

TIA,

Mike

···

From: Benjamin Root <ben.root@…1304…>
To: Michael Rawlins <rawlins02@…120…9…>
Cc: "matplotlib-users@lists.sourceforge.net" <matplotlib-users@…1064…t>
Sent: Tuesday, January 17, 2012 10:36 AM
Subject: Re: [Matplotlib-users] placing colorbar when using subplot command

On Tue, Jan 17, 2012 at 9:30 AM, Michael Rawlins <rawlins02@…9…> wrote:

I’m relatively new to matplotlib. Trying to place a colorbar in a figure. The code below, placed in a file and executed with python, draws 4 maps using basemap. I’ve been unable to get a colorbar to show up anywhere on the figure. Ideally I would like the option of placing a colorbar across the bottom, spanning across both bottom map panels. Also would need the option of placing a colorbar either to the right of or below each map. Uncommenting the two lines under “Here make a colorbar” cause an error. I’ve used those commands when creating just one map using the figure command.

TIA,

Mike

Mike,

Try using the axes_grid1 toolkit to produce your axes objects and to allocate enough room for colorbars.

http://matplotlib.sourceforge.net/mpl_toolkits/axes_grid/index.html

Cheers!
Ben Root

P.S. - a little history, there used to be an axes_grid toolkit, but has since been superseded by axes_grid1.

Clicking on source code on that page produces an error. For several of the graphic on that page, dropping the code into a file and running also produces various errors. Being new to this software, having a specific example that I can run and then incorporate into my code would be a big help.

Mike

Mike,

I do apologize for that. We will have to get that fixed on the website (not sure why it is happening). I have attached an example file for you to try. Also, which version of matplotlib are you running? Without the error message you are getting, it would be hard to tell you what is wrong (most likely it is a version issue).

Ben Root

demo_axes_grid.py (2.8 KB)

···

On Tue, Jan 17, 2012 at 12:37 PM, Michael Rawlins <rawlins02@…9…> wrote:


From: Benjamin Root <ben.root@…1304…>
To: Michael Rawlins <rawlins02@…9…>
Cc: "matplotlib-users@lists.sourceforge.net" matplotlib-users@lists.sourceforge.net
Sent: Tuesday, January 17, 2012 10:36 AM
Subject: Re: [Matplotlib-users] placing colorbar when using subplot command

On Tue, Jan 17, 2012 at 9:30 AM, Michael Rawlins <rawlins02@…9…> wrote:

I’m relatively new to matplotlib. Trying to place a colorbar in a figure. The code below, placed in a file and executed with python, draws 4 maps using basemap. I’ve been unable to get a colorbar to show up anywhere on the figure. Ideally I would like the option of placing a colorbar across the bottom, spanning across both bottom map panels. Also would need the option of placing a colorbar either to the right of or below each map. Uncommenting the two lines under “Here make a colorbar” cause an error. I’ve used those commands when creating just one map using the figure command.

TIA,

Mike

Mike,

Try using the axes_grid1 toolkit to produce your axes objects and to allocate enough room for colorbars.

http://matplotlib.sourceforge.net/mpl_toolkits/axes_grid/index.html

Cheers!
Ben Root

P.S. - a little history, there used to be an axes_grid toolkit, but has since been superseded by axes_grid1.

Clicking on source code on that page produces an error. For several of the graphic on that page, dropping the code into a file and running also produces various errors. Being new to this software, having a specific example that I can run and then incorporate into my code would be a big help.

Mike

···

From: Benjamin Root <ben.root@…1304…>
To: Michael Rawlins <rawlins02@…120…9…>
Cc: "matplotlib-users@lists.sourceforge.net" <matplotlib-users@…1064…t>
Sent: Tuesday, January 17, 2012 2:02 PM
Subject: Re: [Matplotlib-users] placing colorbar when using subplot command

On Tue, Jan 17, 2012 at 12:37 PM, Michael Rawlins <rawlins02@…9…> wrote:


From: Benjamin Root <ben.root@…1304…>
To: Michael Rawlins <rawlins02@…9…>
Cc: “matplotlib-users@…1867…s.sourceforge.net” matplotlib-users@lists.sourceforge.net
Sent: Tuesday, January 17, 2012 10:36 AM
Subject: Re: [Matplotlib-users] placing colorbar when using subplot command

On Tue, Jan 17, 2012 at 9:30 AM, Michael Rawlins <rawlins02@…9…> wrote:

I’m relatively new to matplotlib. Trying to place a colorbar in a figure. The code below, placed in a file and executed with python, draws 4 maps using basemap. I’ve been unable to get a colorbar to show up anywhere on the figure. Ideally I would like the option of placing a colorbar across the bottom, spanning across both bottom map panels. Also would need the option of placing a colorbar either to the right of or below each map. Uncommenting the two lines under “Here make a colorbar” cause an error. I’ve used those commands when creating just one map using the figure command.

TIA,

Mike

Mike,

Try using the axes_grid1 toolkit to produce your axes objects and to allocate enough room for colorbars.

http://matplotlib.sourceforge.net/mpl_toolkits/axes_grid/index.html

Cheers!
Ben Root

P.S. - a little history, there used to be an axes_grid toolkit, but has since been superseded by axes_grid1.

Clicking on source code on that page produces an error. For several of the graphic on that page, dropping the code into a file and running also produces various errors. Being new to this software, having a specific example that I can run and then incorporate into my code would be a big help.

Mike

Mike,

I do apologize for that. We will have to get that fixed on the website (not sure why it is happening). I have attached an example file for you to try. Also, which version of matplotlib are you running? Without the error message you are getting, it would be hard to tell you what is wrong (most likely it is a version issue).

Ben Root

Ben et al.

This line causes the error:

from mpl_toolkits.axes_grid1 import AxesGrid

Traceback (most recent call last):
File “demo_axes_grid.py”, line 2, in
from mpl_toolkits.axes_grid1 import AxesGrid
File “/usr/local/lib/python2.6/dist-packages/mpl_toolkits/axes_grid1/init.py”, line 4, in
from axes_grid import Grid, ImageGrid, AxesGrid
File “/usr/local/lib/python2.6/dist-packages/mpl_toolkits/axes_grid1/axes_grid.py”, line 6, in
import colorbar as mcolorbar
File “/usr/local/lib/python2.6/dist-packages/mpl_toolkits/axes_grid1/colorbar.py”, line 26, in
from matplotlib import docstring
ImportError: cannot import name docstring

I see no axes_grid1.py where I guess it belongs. Here’s what’s in
/usr/share/pyshared/mpl_toolkits/axes_grid

parasite_axes.py axislines.py anchored_artists.py inset_locator.py
grid_helper_curvelinear.py axes_size.py init.py
grid_finder.py axes_rgb.py axes_divider.py
clip_path.py axes_grid.py angle_helper.py

Do I need to upgrade python? Matplotlib? Add just a file or two?

Mike

···

From: Benjamin Root <ben.root@…1304…>
To: Michael Rawlins <rawlins02@…120…9…>
Cc: "matplotlib-users@lists.sourceforge.net" <matplotlib-users@…1064…t>
Sent: Tuesday, January 17, 2012 2:02 PM
Subject: Re: [Matplotlib-users] placing colorbar when using subplot command

On Tue, Jan 17, 2012 at 12:37 PM, Michael Rawlins <rawlins02@…9…> wrote:


From: Benjamin Root <ben.root@…1304…>
To: Michael Rawlins <rawlins02@…9…>
Cc: “matplotlib-users@…1867…s.sourceforge.net” matplotlib-users@lists.sourceforge.net
Sent: Tuesday, January 17, 2012 10:36 AM
Subject: Re: [Matplotlib-users] placing colorbar when using subplot command

On Tue, Jan 17, 2012 at 9:30 AM, Michael Rawlins <rawlins02@…9…> wrote:

I’m relatively new to matplotlib. Trying to place a colorbar in a figure. The code below, placed in a file and executed with python, draws 4 maps using basemap. I’ve been unable to get a colorbar to show up anywhere on the figure. Ideally I would like the option of placing a colorbar across the bottom, spanning across both bottom map panels. Also would need the option of placing a colorbar either to the right of or below each map. Uncommenting the two lines under “Here make a colorbar” cause an error. I’ve used those commands when creating just one map using the figure command.

TIA,

Mike

Mike,

Try using the axes_grid1 toolkit to produce your axes objects and to allocate enough room for colorbars.

http://matplotlib.sourceforge.net/mpl_toolkits/axes_grid/index.html

Cheers!
Ben Root

P.S. - a little history, there used to be an axes_grid toolkit, but has since been superseded by axes_grid1.

Clicking on source code on that page produces an error. For several of the graphic on that page, dropping the code into a file and running also produces various errors. Being new to this software, having a specific example that I can run and then incorporate into my code would be a big help.

Mike

Mike,

I do apologize for that. We will have to get that fixed on the website (not sure why it is happening). I have attached an example file for you to try. Also, which version of matplotlib are you running? Without the error message you are getting, it would be hard to tell you what is wrong (most likely it is a version issue).

Ben Root

Version 0.99.1.1 installed through Synaptic package manager on system running Ubuntu 10.04. I’d rather not attempt a fresh install from sources just yet. Adding a colorbar alonside each map panel is possible. Anyone able to modify that code to make that work? I’m assuming it very simple for an experienced user. Also, is it possible to add axes_grid1.py to my directory? If I want to get fancy in the future I will install version 1 and use axes_grid1.py.

Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.

import matplotlib
matplotlib.version
‘0.99.1.1’

···

From: Michael Rawlins <rawlins02@…9…>
To: Benjamin Root <ben.root@…1836…304…>
Cc: "matplotlib-users@lists.sourceforge.net" <matplotlib-users@…1064…t>
Sent: Wednesday, January 18, 2012 10:13 AM
Subject: Re: [Matplotlib-users] placing colorbar when using subplot command


From: Benjamin Root <ben.root@…1304…>
To: Michael Rawlins <rawlins02@…9…>
Cc: "matplotlib-users@lists.sourceforge.net" <matplotlib-users@…1544…ceforge.net>
Sent: Tuesday, January 17, 2012 2:02 PM
Subject: Re: [Matplotlib-users] placing colorbar when using subplot command

On Tue, Jan 17, 2012 at 12:37 PM, Michael Rawlins <rawlins02@…9…> wrote:


From: Benjamin Root <ben.root@…1304…>
To: Michael Rawlins <rawlins02@…9…>
Cc: “matplotlib-users@…1867…s.sourceforge.net” matplotlib-users@lists.sourceforge.net
Sent: Tuesday, January 17, 2012 10:36 AM
Subject: Re: [Matplotlib-users] placing colorbar when using subplot command

On Tue, Jan 17, 2012 at 9:30 AM, Michael Rawlins <rawlins02@…9…> wrote:

I’m relatively new to matplotlib. Trying to place a colorbar in a figure. The code below, placed in a file and executed with python, draws 4 maps using basemap. I’ve been unable to get a colorbar to show up anywhere on the figure. Ideally I would like the option of placing a colorbar across the bottom, spanning across both bottom map panels. Also would need the option of placing a colorbar either to the right of or below each map. Uncommenting the two lines under “Here make a colorbar” cause an error. I’ve used those commands when creating just one map using the figure command.

TIA,

Mike

Mike,

Try using the axes_grid1 toolkit to produce your axes objects and to allocate enough room for colorbars.

http://matplotlib.sourceforge.net/mpl_toolkits/axes_grid/index.html

Cheers!
Ben Root

P.S. - a little history, there used to be an axes_grid toolkit, but has since been superseded by axes_grid1.

Clicking on source code on that page produces an error. For several of the graphic on that page, dropping the code into a file and running also produces various errors. Being new to this software, having a specific example that I can run and then incorporate into my code would be a big help.

Mike

Mike,

I do apologize for that. We will have to get that fixed on the website (not sure why it is happening). I have attached an example file for you to try. Also, which version of matplotlib are you running? Without the error message you are getting, it would be hard to tell you what is wrong (most likely it is a version issue).

Ben Root

Version 0.99.1.1 installed through Synaptic package manager on system running Ubuntu 10.04. I’d rather not attempt a fresh install from sources just yet. Adding a colorbar alonside each map panel is possible. Anyone able to modify that code to make that work? I’m assuming it very simple for an experienced user. Also, is it possible to add axes_grid1.py to my directory? If I want to get fancy in the future I will install version 1 and use axes_grid1.py.

Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.

import matplotlib
matplotlib.version
‘0.99.1.1’

Forum:

Still unable to get a colorbar alongside the panels in this simple example. One last request for assistance.
Thanks.

Mike

import sys,getopt
from mpl_toolkits.basemap import Basemap, shiftgrid, cm
#from netCDF3 import Dataset as NetCDFFile
from mpl_toolkits.basemap import NetCDFFile
from pylab import *
import matplotlib.pyplot as plt

cmap = cm.get_cmap(‘jet’, 10) # 10 discrete colors

m = Basemap(llcrnrlon=-80.6,llcrnrlat=38.4,urcrnrlon=-66.0,urcrnrlat=47.7,
resolution=‘l’,area_thresh=1000.,projection=‘lcc’,
lat_1=65.,lon_0=-73.3)
xtxt=200000. #offset for text
ytxt=200000.
parallels = arange(38.,48.,2.)
meridians = arange(-80.,-64.,2.)

xsize =
rcParams[‘figure.figsize’][0]
fig=figure(figsize=(xsize,m.aspect*xsize))

############################################################################################
subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=0.15, hspace=0.11)

Make the first map

plt.subplot(1,2,1)

draw coastlines and political boundaries.

m.drawcoastlines()
m.drawcountries()
m.drawstates()

Here make a colorbar.

cax = axes([0.88, 0.1, 0.06, 0.81]) # colorbar axes for map w/ graticule
#colorbar(format=’%3.1f’, ticks=[-1.5, -1.2, -0.9, -0.6, -0.3, 0.0, 0.3, 0.6, 0.9, 1.2, 1.5], cax=cax)
#colorbar(cax=cax)

Make the second map

plt.subplot(1,2,2)

draw coastlines and political

boundaries.
m.drawcoastlines()
m.drawcountries()
m.drawstates()

plt.show()
plt.savefig(“map.eps”)
plt.clf() # Clears the figure object


Keep Your Developer Skills Current with LearnDevNow!
The most comprehensive online learning library for Microsoft developers
is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
Metro Style Apps, more. Free future releases when you subscribe now!
http://p.sf.net/sfu/learndevnow-d2d


Matplotlib-users mailing list
Matplotlib-users@…563…eforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

I recommend upgrading to v1.1.0. There are some PPAs out there that has a more recent version of mpl available as a package. Or you can install from source. Instructions for doing that is in the FAQ.

Ben Root

···

On Thursday, January 19, 2012, Michael Rawlins <rawlins02@…9…> wrote:


From: Michael Rawlins <rawlins02@…9…>

To: Benjamin Root <ben.root@…1304…>
Cc: "matplotlib-users@lists.sourceforge.net" <matplotlib-users@…1220…sts.sourceforge.net>

Sent: Wednesday, January 18, 2012 10:13 AM
Subject: Re: [Matplotlib-users] placing colorbar when using subplot command


From: Benjamin Root <ben.root@…1304…>

To: Michael Rawlins <rawlins02@…9…>
Cc: "matplotlib-users@lists.sourceforge.net" <matplotlib-users@…1867…s.sourceforge.net>

Sent: Tuesday, January 17, 2012 2:02 PM
Subject: Re: [Matplotlib-users] placing colorbar when using subplot command

On Tue, Jan 17, 2012 at 12:37 PM, Michael Rawlins <rawlins02@…9…> wrote:


From: Benjamin Root <ben.root@…1304…>
To: Michael Rawlins <rawlins02@…9…>

Cc: "matplotlib-users@lists.sourceforge.net" matplotlib-users@lists.sourceforge.net

Sent: Tuesday, January 17, 2012 10:36 AM
Subject: Re: [Matplotlib-users] placing colorbar when using subplot command

On Tue, Jan 17, 2012 at 9:30 AM, Michael Rawlins <rawlins02@…9…> wrote:

I’m relatively new to matplotlib. Trying to place a colorbar in a figure. The code below, placed in a file and executed with python, draws 4 maps using basemap. I’ve been unable to get a colorbar to show up anywhere on the figure. Ideally I would like the option of placing a colorbar across the bottom, spanning across both bottom map panels. Also would need the option of placing a colorbar either to the right of or below each map. Uncommenting the two lines under “Here make a colorbar” cause an error. I’ve used those commands when creating just one map using the figure command.

TIA,
Mike

Mike,

Try using the axes_grid1 toolkit to produce your axes objects and to allocate enough room for colorbars.

http://matplotlib.sourceforge.net/mpl_toolkits/axes_grid/index.html

Cheers!
Ben Root

P.S. - a little history, there used to be an axes_grid toolkit, but has since been superseded by axes_grid1.

Clicking on source code on that page produces an error. For several of the graphic on that page, dropping the code into a file and running also produces various errors. Being new to this software, having a specific example that I can run and then incorporate into my code would be a big help.

Mike

Mike,

I do apologize for that. We will have to get that fixed on the website (not sure why it is happening). I have attached an example file for you to try. Also, which version of matplotlib are you running? Without the error message you are getting, it would be hard to tell you what is wrong (most likely it is a version issue).

Ben Root

Version 0.99.1.1 installed through Synaptic package manager on system running Ubuntu 10.04. I’d rather not attempt a fresh install from sources just yet. Adding a colorbar alonside each map panel is possible. Anyone able to modify that code to make that work? I’m assuming it very simple for an experienced user. Also, is it possible to add axes_grid1.py to my directory? If I want to get fancy in the future I will install version 1 and use axes_grid1.py.

Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.

import matplotlib

matplotlib.version
‘0.99.1.1’

Forum:

Still unable to get a colorbar alongside the panels in this simple example. One last request for assistance. Thanks.

Mike

import sys,getopt
from mpl_toolkits.basemap import Basemap, shiftgrid, cm
#from netCDF3 import Dataset as NetCDFFile
from mpl_toolkits.basemap import NetCDFFile

from pylab import *
import matplotlib.pyplot as plt

cmap = cm.get_cmap(‘jet’, 10) # 10 discrete colors

m = Basemap(llcrnrlon=-80.6,llcrnrlat=38.4,urcrnrlon=-66.0,urcrnrlat=47.7,\

        resolution='l',area_thresh=1000.,projection='lcc',\
        lat_1=65.,lon_0=-73.3)

xtxt=200000. #offset for text
ytxt=200000.
parallels = arange(38.,48.,2.)

meridians = arange(-80.,-64.,2.)

xsize = rcParams[‘figure.figsize’][0]
fig=figure(figsize=(xsize,m.aspect*xsize))

############################################################################################

subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=0.15, hspace=0.11)

Make the first map

plt.subplot(1,2,1)

draw coastlines and political boundaries.

m.drawcoastlines()

m.drawcountries()
m.drawstates()

Here make a colorbar.

cax = axes([0.88, 0.1, 0.06, 0.81]) # colorbar axes for map w/ graticule
#colorbar(format=’%3.1f’, ticks=[-1.5, -1.2, -0.9, -0.6, -0.3, 0.0, 0.3, 0.6, 0.9, 1.2, 1.5], cax=cax)

#colorbar(cax=cax)

Make the second map

plt.subplot(1,2,2)

draw coastlines and political boundaries.

m.drawcoastlines()

m.drawcountries()
m.drawstates()

plt.show()
plt.savefig(“map.eps”)
plt.clf() # Clears the figure object


Keep Your Developer Skills Current with LearnDevNow!
The most comprehensive online learning library for Microsoft developers

is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
Metro Style Apps, more. Free future releases when you subscribe now!
http://p.sf.net/sfu/learndevnow-d2d


Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

···

From: Benjamin Root <ben.root@…1304…>
To: Michael Rawlins <rawlins02@…120…9…>
Cc: "matplotlib-users@lists.sourceforge.net" <matplotlib-users@…1064…t>
Sent: Thursday, January 19, 2012 10:07 AM
Subject: Re: [Matplotlib-users] placing colorbar when using subplot command

On Thursday, January 19, 2012, Michael Rawlins <rawlins02@…9…> wrote:


From: Michael Rawlins <rawlins02@…9…>

To: Benjamin Root <ben.root@…120…1304…>
Cc: "matplotlib-users@lists.sourceforge.net" matplotlib-users@lists.sourceforge.net

Sent: Wednesday, January 18, 2012 10:13 AM
Subject: Re: [Matplotlib-users] placing colorbar when using subplot command


From: Benjamin Root <ben.root@…1304…>

To: Michael Rawlins <rawlins02@…878…9…>
Cc: "matplotlib-users@lists.sourceforge.net" matplotlib-users@lists.sourceforge.net

Sent: Tuesday, January 17, 2012 2:02 PM
Subject: Re: [Matplotlib-users] placing colorbar when using subplot command

On Tue, Jan 17, 2012 at 12:37 PM, Michael Rawlins <rawlins02@…9…> wrote:


From: Benjamin Root <ben.root@…1304…>
To: Michael Rawlins <rawlins02@…9…>

Cc: "matplotlib-users@lists.sourceforge.net" matplotlib-users@lists.sourceforge.net

Sent: Tuesday, January 17, 2012 10:36 AM
Subject: Re: [Matplotlib-users] placing colorbar when using subplot command

On Tue, Jan 17, 2012 at 9:30 AM, Michael Rawlins <rawlins02@…9…> wrote:

I’m relatively new to matplotlib. Trying to place a colorbar in a figure. The code below, placed in a file and executed with python, draws 4 maps using basemap. I’ve been unable to get a colorbar to show up anywhere on the figure. Ideally I would like the option of placing a colorbar across the bottom, spanning across both bottom map panels. Also would need the option of placing a colorbar either to the right of or below each map. Uncommenting the two lines under “Here make a colorbar” cause an error. I’ve used those commands when creating just one map using the figure command.

TIA,
Mike

Mike,

Try using the axes_grid1 toolkit to produce your axes objects and to allocate enough room for colorbars.

http://matplotlib.sourceforge.net/mpl_toolkits/axes_grid/index.html

Cheers!
Ben Root

P.S. - a little history, there used to be an axes_grid toolkit, but has since been superseded by axes_grid1.

Clicking on source code on that page produces an error. For several of the graphic on that page, dropping the code into a file and running also produces various errors. Being new to this software, having a specific example that I can run and then incorporate into my code would be a big help.

Mike

Mike,

I do apologize for that. We will have to get that fixed on the website (not sure why it is happening). I have attached an example file for you to try. Also, which version of matplotlib are you running? Without the error message you are getting, it would be hard to tell you what is wrong (most likely it is a version issue).

Ben Root

Version 0.99.1.1 installed through Synaptic package manager on system running Ubuntu 10.04. I’d rather not attempt a fresh install from sources just yet. Adding a colorbar alonside each map panel is possible. Anyone able to modify that code to make that work? I’m assuming it very simple for an experienced user. Also, is it possible to add axes_grid1.py to my directory? If I want to get fancy in the future I will install version 1 and use axes_grid1.py.

Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.

import matplotlib

matplotlib.version
‘0.99.1.1’

Forum:

Still unable to get a colorbar alongside the panels in this simple example. One last request for assistance. Thanks.

Mike

import sys,getopt
from mpl_toolkits.basemap import Basemap, shiftgrid, cm
#from netCDF3 import Dataset as NetCDFFile
from mpl_toolkits.basemap import NetCDFFile

from pylab import *
import matplotlib.pyplot as plt

cmap = cm.get_cmap(‘jet’, 10) # 10 discrete colors

m = Basemap(llcrnrlon=-80.6,llcrnrlat=38.4,urcrnrlon=-66.0,urcrnrlat=47.7,\

        resolution='l',area_thresh=1000.,projection='lcc',\
        lat_1=65.,lon_0=-73.3)

xtxt=200000. #offset for text
ytxt=200000.
parallels = arange(38.,48.,2.)

meridians = arange(-80.,-64.,2.)

xsize = rcParams[‘figure.figsize’][0]
fig=figure(figsize=(xsize,m.aspect*xsize))

############################################################################################

subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=0.15, hspace=0.11)

Make the first map

plt.subplot(1,2,1)

draw coastlines and political boundaries.

m.drawcoastlines()

m.drawcountries()
m.drawstates()

Here make a colorbar.

cax = axes([0.88, 0.1, 0.06, 0.81]) # colorbar axes for map w/ graticule
#colorbar(format=’%3.1f’, ticks=[-1.5, -1.2, -0.9, -0.6, -0.3, 0.0, 0.3, 0.6, 0.9, 1.2, 1.5], cax=cax)

#colorbar(cax=cax)

Make the second map

plt.subplot(1,2,2)

draw coastlines and political boundaries.

m.drawcoastlines()

m.drawcountries()
m.drawstates()

plt.show()
plt.savefig(“map.eps”)
plt.clf() # Clears the figure object


Keep Your Developer Skills Current with LearnDevNow!
The most comprehensive online learning library for Microsoft developers

is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
Metro Style Apps, more. Free future releases when you subscribe now!
http://p.sf.net/sfu/learndevnow-d2d


Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

I recommend upgrading to v1.1.0. There are some PPAs out there that has a more recent version of mpl available as a package. Or you can install from source. Instructions for doing that is in the FAQ.

Ben Root

Have no experience with PPAs and couldn’t find a package. Installing from sources. Following instructions here:

http://matplotlib.sourceforge.net/faq/installing_faq.html#installation

I removed matplotlib 0.99.1.1 and numpy 1.3.0 using Synaptic. Issued command:

sudo apt-get build-dep python-matplotlib

to get dependencies. But the setup command return the below to standard output immediately. numpy 1.4 or later is required. How do I install that dependency?

python setup.py install
basedirlist is: [’/usr/local’, ‘/usr’]
============================================================================
BUILDING MATPLOTLIB
matplotlib: 1.2.x
python: 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC
4.4.3]
platform: linux2

REQUIRED DEPENDENCIES
* numpy 1.4 or later is required; you have 1.3.0

···

From: Benjamin Root <ben.root@…1304…>
To: Michael Rawlins <rawlins02@…120…9…>
Cc: "matplotlib-users@lists.sourceforge.net" <matplotlib-users@…1064…t>
Sent: Thursday, January 19, 2012 10:07 AM
Subject: Re: [Matplotlib-users] placing colorbar when using subplot command

On Thursday, January 19, 2012, Michael Rawlins <rawlins02@…9…> wrote:

Forum:

Still unable to get a colorbar alongside the panels in this simple example. One last request for assistance. Thanks.

Mike

import sys,getopt
from mpl_toolkits.basemap import Basemap, shiftgrid, cm
#from netCDF3 import Dataset as NetCDFFile
from mpl_toolkits.basemap import NetCDFFile

from pylab import *
import matplotlib.pyplot as plt

cmap = cm.get_cmap(‘jet’, 10) # 10 discrete colors

m = Basemap(llcrnrlon=-80.6,llcrnrlat=38.4,urcrnrlon=-66.0,urcrnrlat=47.7,\

        resolution='l',area_thresh=1000.,projection='lcc',\
        lat_1=65.,lon_0=-73.3)

xtxt=200000. #offset for text
ytxt=200000.
parallels = arange(38.,48.,2.)

meridians = arange(-80.,-64.,2.)

xsize = rcParams[‘figure.figsize’][0]
fig=figure(figsize=(xsize,m.aspect*xsize))

############################################################################################

subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=0.15, hspace=0.11)

Make the first map

plt.subplot(1,2,1)

draw coastlines and political boundaries.

m.drawcoastlines()

m.drawcountries()
m.drawstates()

Here make a colorbar.

cax = axes([0.88, 0.1, 0.06, 0.81]) # colorbar axes for map w/ graticule
#colorbar(format=’%3.1f’, ticks=[-1.5, -1.2, -0.9, -0.6, -0.3, 0.0, 0.3, 0.6, 0.9, 1.2, 1.5], cax=cax)

#colorbar(cax=cax)

Make the second map

plt.subplot(1,2,2)

draw coastlines and political boundaries.

m.drawcoastlines()

m.drawcountries()
m.drawstates()

plt.show()
plt.savefig(“map.eps”)
plt.clf() # Clears the figure object


Keep Your Developer Skills Current with LearnDevNow!
The most comprehensive online learning library for Microsoft developers

is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
Metro Style Apps, more. Free future releases when you subscribe now!
http://p.sf.net/sfu/learndevnow-d2d


Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

I recommend upgrading to v1.1.0. There are some PPAs out there that has a more recent version of mpl available as a package. Or you can install from source. Instructions for doing that is in the FAQ.

Ben Root

Have upgraded to v1.2.x. The example above runs fine. Ideally would like to place a colorbar to right of each panel or below each panel or one single colorbar spanning across both maps. Pointers to specific examples or edits to the code much appreciated. Note: basemap is required to run the example.

Mike