Discrete colorbar in (New) Graphics

The improved NG COLORBAR function in IDL 8.2 makes it much easier to display a discrete colorbar. The  example below is adapted from one of the IDLffVideoWrite examples I created for IDL 8.1; here, I use NCEP/NCAR Reanalysis 1 data to display the mean global 500 mb geopotential height surface for a single day as a filled contour plot with a discrete colorbar.

I’ve chosen to write this example as a procedure. Start by locating the data file (it’s netCDF and I’m assuming it’s in IDL’s path), then reading it with a helper routine:

pro view_nnrp500mbgph, save=save
   compile_opt idl2

   f = file_which('X174.29.255.181.65.14.23.9.nc', /include)
   x = read_nnrp500mbgph(f)

The return from READ_NNRP500MBGPH, x, is a hash containing keys ‘hgt’, ‘lon’, ‘lat’ and ‘time’. An aside: in READ_NNRP500MBGPH I had fun using IDL 8 language features in conjunction with our netCDF API. This may be worth a separate post.

Next, set up a map projection:

   m = map('Orthographic', $
      center_latitude=30, $
      center_longitude=120, $
      limit=[-90, 0, 90, 360], $ ; crosses IDL
      /current, $
      title='Daily Mean 500mb Geopotential Heights', $
      color='gray')

I chose an orthographic projection that crosses the international date line.

Now visualize the 500 mb geopotential height surface for 2010 January 1 as a filled contour plot, using a set of custom levels based on the data range:

   nlevels = 14
   levels = findgen(nlevels)*100 + 4700 ; m
   g_heights = contour(x['hgt',*,*,0], x['lon'], x['lat'], $
      overplot=m, $
      c_value=levels, $
      rgb_table=39, $
      grid_units='degrees', $
      /fill, $
      transparency=20)

In the hash x, ‘hgt’ is a 3D array of heights dimensioned by [longitude, latitude, day of the year]. In this case, the subscripts applied to ‘hgt’ give all of the longitudes and latitudes on the first day of the year.

To finish, annotate the visualization with continental outlines, a colorbar and some reference text:

   g_continents = mapcontinents(color='black')
   g_colorbar = colorbar(target=g_heights, $
      orientation=1, $
      textpos=1, $
      font_size=10, $
      transparency=g_heights.transparency, $
      border=1, $ ; boxes around cells
      position=[0.86, 0.20, 0.88, 0.80], $
      title='Height (m)')
   g_date = text(0.05, 0.20, '2010 January 01', font_size=12)
   subtitle = ['NCEP/NCAR Reanalysis Project', $
      'http://www.esrl.noaa.gov/psd/data/gridded/data.ncep.reanalysis.html']
   g_subtitle = text(0.05, 0.05, subtitle, font_size=8)

   if keyword_set(save) then $
      m.save, 'view_nnrp500mbgph.png', resolution=300
end

Note that the call to COLORBAR isn’t significantly different than in IDL 8.1. Through its TARGET keyword, it sees the levels and colors used in the contour plot and displays them appropriately as a discrete set of 14 colors (not, as in 8.1, all 256 colors from color table 39).  End caps are displayed by default for a filled contour plot.

The result:

A contour plot of 500 mb geopotential heights

Download the code and data for this example here. Please also check out the IDL Help page for COLORBAR; it has several examples that demonstrate its new behavior.

About these ads

About Mark

I solve scientific programming and visualization problems with IDL.
This entry was posted in language, visualization and tagged , , , , , , . Bookmark the permalink.

6 Responses to Discrete colorbar in (New) Graphics

  1. I guess if by “much easier” you mean “still wrong,” this is probably true. :-)

    If you simply count the colors in your color bar, you will see you have 13 colors or levels, not the 14 you think you have. Here is the plot I think you should have gotten with your discrete contour plot and color bar if you were actually using 14 levels:

    http://www.idlcoyote.com/misc/discrete_contour_plot,png

    If you like, I can send you the Coyote Graphics commands I used to produce the plot, so you can see if I am correct. Perhaps this is just a semantics problem. Maybe when you say “14 levels” you really mean “13 levels”. We have the same problem in direct graphics. When we say NLEVELS=14, we mean “somewhere in the neighborhood of 14, depending on, you know, things.” :-)

  2. Sorry, that URL had a comma in it that I didn’t notice until I had posted it. Here is the correct URL:

    http://www.idlcoyote.com/misc/discrete_contour_plot.png

  3. I don’t see any obvious way to pick my discrete colors, other than to choose a color table. Am I missing something? How would I produce something like a land-cover map?

  4. I count 14 levels in that contour (12 of which are labeled in the color bar since the max and min are not labeled). 14 levels means that there are 13 colors used in between the levels. This is how it works with direct graphics with the LEVELS keyword, who knows what happens with the NLEVELS keyword.

  5. Well, OK, you must be one of those guys who starts counting at 0, too. But, if what you say is true, and I’m sure it is, then I would argue that the pointed ends on the color bar are misleading. Normally, when we label a color bar with pointed ends we mean that the first or last color include any value less than or greater than the nearest labeled value. While that might be true for the low end of the color bar (assuming we start the levels at the minimum value of the data), it can’t be true for the high end of the color bar, since that color is “boxed in” by the last level. Any value in the data that is larger than the highest level will be shown in some unspecified color. (At least, not specified by the color bar.)

    The only way to do this properly is to equate “number of levels” with “number of colors in the color bar”, which means (in IDL 8.2 terms) that you will need one more “level” than the number
    of colors you want to use in the discrete contour plot, and the first level will have to be the minimum of the data to contour, and the last level will have to be the maximum of the data to contour. Anything other than this is likely to show the wrong colors in the contour plot.

    In direct graphics, by the way, the last level is “unbounded, and so automatically goes up to the maximum value of the data. This is NOT the case with this function graphics contour plot or color bar.

  6. Honestly, I think the situation is even worse than I think it is. Run Mark’s code to produce a plot. Then change just a portion of the data to make it at least one “level” greater than the maximum value of the previous data. I modified the code like this:

    height = x['hgt',*,*,0]
    height [30:50, 30:50] = Max(height) + 1500
    print, max(height), max(levels)
    g_heights = contour(height, x['lon'], x['lat'], $
    overplot=m, $
    c_value=levels, $
    rgb_table=39, $
    grid_units=’degrees’, $
    /fill, $
    transparency=20)

    Now, run the program again. The levels on the color bar are exactly the same, but the colors are completely different! Say what!?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s