Thanks Jeff,

I'll study your code and will see if I can adapt it for my use.

Great tool also,

Roger
--

On Fri, Dec 12, 2008 at 3:20 PM, Jeff Whitaker <jsw...@fastmail.fm> wrote:

> Roger André wrote:
>
>> Hi All,
>>
>> I'm very new to Matplotlib and am having some trouble getting a colorbar
>> to be positioned and sized I want it to.  A big part of the problem is that
>> I have adapted several examples from the Cookbook and Gallery, just to see
>> if I could roughly approximate what I want to see, and now am having trouble
>> integrating the different pieces.  Specifically, I can't seem to resolve
>> when to use add_subplot vs. add_axes.  Below are 2 examples of code.  The
>> first one shows correct layout of a data figure and a separate colorbar
>> below it.  The colorbar is the correct size, and is located in the right
>> spot.  The second example has the correct data mapped in it using the
>> basemap module, but I cannot get the colorbar to move up closer to the
>> figure, or to shrink it.  Could someone advise me on this?  I've looked at
>> the "Artist tutorial", and although it is very well written, I'm still not
>> sure how to get this done .
>>
>> Thanks in advance,
>>
>> Roger
>>
>
> Roger:  I usually explicity two separate axes instances, one for the plot
> and one for the colorbar. The location of the axes always takes a bit of
> tweaking to get right. Here's an example:
>
> from mpl_toolkits.basemap import Basemap
> import matplotlib.pyplot as plt
> import numpy as np
> # create figure instance.
> fig = plt.figure(figsize=(8,8))
> # create an axes instance, leaving room for colorbars on right and bottom.
> ax = fig.add_axes([0.05,0.15,0.8,0.8]) # color on bottom
> # set up orthographic map projection with
> # perspective of satellite looking down at 50N, 100W.
> # use low resolution coastlines.
> map = Basemap(projection='ortho',lat_0=50,lon_0=-100,resolution='c')
> # draw coastlines, country boundaries, fill continents.
> map.drawcoastlines()
> # draw the edge of the map projection region (the projection limb)
> map.drawmapboundary()
> # draw lat/lon grid lines every 30 degrees.
> map.drawmeridians(np.arange(0,360,30))
> map.drawparallels(np.arange(-90,90,30))
> # make up some data on a regular lat/lon grid.
> nlats = 73; nlons = 145; delta = 2.*np.pi/(nlons-1)
> lats = (0.5*np.pi-delta*np.indices((nlats,nlons))[0,:,:])
> lons = (delta*np.indices((nlats,nlons))[1,:,:])
> wave = 0.75*(np.sin(2.*lats)**8*np.cos(4.*lons))
> mean = 0.5*np.cos(2.*lats)*((np.sin(2.*lats))**2 + 2.)
> # compute native map projection coordinates of lat/lon grid.
> x, y = map(lons*180./np.pi, lats*180./np.pi)
> # contour data over the map.
> cs = map.contourf(x,y,wave+mean,15)
> # get axes bounds.
> pos = ax.get_position()
> l, b, w, h = pos.bounds
> # create axes instance for colorbar on right.
> cax = plt.axes([l+w+0.03, b, 0.04, h])
> # draw colorbar on right.
> plt.colorbar(cax=cax,orientation='vertical')
> # create axes instance for colorbar on bottom.
> cax = plt.axes([l, b-0.07, w, 0.04])
> # draw colorbar on bottom.
> plt.colorbar(cax=cax,orientation='horizontal')
> plt.show()
>
> HTH,
>
> -Jeff
>
>> -------------
>>
>> Example 1:
>>
>> #! /usr/bin/python
>>
>> import matplotlib.pyplot as plt
>> import numpy as np
>> from numpy.random import randn
>> from matplotlib import mpl
>>
>> # Make plot with horizontal colorbar
>>
>> fig = plt.figure(figsize=(7,8))
>> ax = fig.add_subplot(111)
>>
>> # 'add_axes'  for color bar
>> ax1 = fig.add_axes([0.25, .07, 0.5, 0.03]) # [x_loc, y_loc, x_size,
>> y_size]
>>
>> data = np.clip(randn(250, 250), -1, 1)     # DATA FOR SQUARE FIG
>> ax.imshow(data, interpolation='nearest') # DRAW DATA IN SQUARE FIG
>>
>> ax.set_title('Monthly PCP percentiles for 9-2008')
>>
>> ###########  Colorbar Settings ########
>> cmap = mpl.cm.cool
>> norm = mpl.colors.Normalize(vmin=0.0, vmax=1.0)
>> cb1 = mpl.colorbar.ColorbarBase(ax1, cmap=cmap, norm=norm,
>> orientation='horizontal')
>> cb1.set_label('percentile')
>> ################################
>>
>> plt.show()
>> fig.savefig('test.png')
>>
>>
>> --------------------------------------------------------------------------------------------
>> Example 2:
>>
>> #! /usr/bin/python
>>
>> """taken from geos_demo_2.py"""
>>
>> from PIL import Image
>> from mpl_toolkits.basemap import Basemap
>> import numpy as np
>> import matplotlib
>> from matplotlib import mpl
>> import matplotlib.pyplot as plt
>> from matplotlib.image import pil_to_array
>>
>> plot_name = 'hydro_demo.png'
>> overlay_color = 'black'
>>
>> # read in jpeg image to rgb array
>> pilImage = Image.open('wms_mapser.png')
>>
>> #data = asarray(pilImage)
>> data = pil_to_array(pilImage)
>> data = data[:, :, :] # get data from RGB channels of image
>>
>> # define data region and projection parameters
>> ll_lon = -125
>> ll_lat = 39
>> ur_lon = -108
>> ur_lat = 54
>> lon_0 = 0
>>
>>
>> fig = plt.figure(figsize=(7,8))
>> #ax = fig.add_axes((0.1,0.1,0.8,0.8))
>> ax = fig.add_axes((.1,0.1,0.8,0.8))
>>
>> # create Basemap instance for cylindrical equidistant projection, htdro
>> image domain
>> m = Basemap(projection='cyl', lon_0=lon_0, llcrnrlon=ll_lon,
>> llcrnrlat=ll_lat, urcrnrlon=ur_lon,
>>            urcrnrlat=ur_lat, suppress_ticks=False)
>>
>> # add data
>> cmap = mpl.cm.cool
>> m.imshow(data, cmap, interpolation=None)
>> plt.clim(0, 1)
>>
>> # add a colobar
>> plt.colorbar(orientation='horizontal')
>>
>> # add timestamp and save
>> fig = plt.gcf()
>>
>> # ADD FIGURE TEXT
>> fig.text(x=0.5, y=0.1,
>>         s='percentile',
>>         fontsize=10,
>>        )
>>
>> fig.set_size_inches((7,7))
>>
>> # ADD A FIGURE TITLE
>> plt.title('Monthly PCP percentiles for 9-2008',y=1.05,fontsize=12)
>>
>> plt.show()
>> #fig.savefig(plot_name)
>> #print 'Plot saved to %s' % (plot_name)
>> ------------------------------------------------------------------------
>>
>>
>> ------------------------------------------------------------------------------
>> SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas,
>> Nevada.
>> The future of the web can't happen without you.  Join us at MIX09 to help
>> pave the way to the Next Web now. Learn more and register at
>>
>> http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/
>> ------------------------------------------------------------------------
>>
>> _______________________________________________
>> Matplotlib-users mailing list
>> Matplotlib-users@lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/matplotlib-users
>>
>>
>
>
> --
> Jeffrey S. Whitaker         Phone  : (303)497-6313
> Meteorologist               FAX    : (303)497-6449
> NOAA/OAR/PSD  R/PSD1        Email  : jeffrey.s.whita...@noaa.gov
> 325 Broadway                Office : Skaggs Research Cntr 1D-113
> Boulder, CO, USA 80303-3328 Web    : http://tinyurl.com/5telg
>
>
------------------------------------------------------------------------------
SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada.
The future of the web can't happen without you.  Join us at MIX09 to help
pave the way to the Next Web now. Learn more and register at
http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to