Do you want ALL values or only 0 to be always represented by the same
color?
If you want all, you will need to go with David's answer and always use
the same range in the bar, whatever the data range.
If you want only the 0-level with the same color, you have to be sure
that, for each of your filled contour plot, you have the same color
loaded for the level 0. That means you need to have a contour level
equal to 0, and always assign the same color from the same table to
this 0-level.
Here is a little program that provides a solution. The color in the
middle of the original colortable is assigned to the 0-level, if you
have a 0-level (you need to correctly define RANGE and NFILL -and
that's it-), set the keyword /CENTERED, and use SHIFT to pass to
colorbar. Try the examples, you may change ct to a valid colortable #
(IDL does not come with nice diverging color scheme).
;+
; This program is to get the contour LEVELS and their COLOR INDICES,
; according to the NUMBER of filled contours, and the RANGE to
; consider. The two lattest are the inputs:
;
;
; INPUTS
; nfill: number of contour-colors.
;
; range: this fltarr(2) gives the limits of the
; colorbar. Range[0] is the first contour level.
Range[1]-step
; is the last contour.
;
; INPUT KEYWORD
; center: to center the color table on level=0. This is very
; useful for diverging data. If nfill and range are such
; that there is no levels=0 , then the
; center of the table is aligned with the first value
; above 0.
;
;
; OUTPUT KEYWORDS
; ccolors: set to variable that will hold contour color indices
; levels: set to variable that will hold contour levels
; shift: set to variable that will hold index shift for colorbar
;
;
;
; EFFECT
; A limited number of colors is loaded from the color table, starting
; at index 4 by default (this can be changed with keyword BOTTOM,
; which is passed to LoadCT). You can load any other color in indices
; 0 to 3. Indice 255 (default plotting) may be untouched and free for
; modification, but only if you do not ask for too much color!!
;
; It is possible to center the color table on contour-level=0. This
; is convenient for diverging data (see examples).
; In this case, a SHIFT value is computed for input to COLORBAR
; (from David Fanning) to ensure that
; contour levels and the colorbar match each other.
;
;
; LIMITATION
; Typical usage is shown in examples below. The idea is to choose the
; range and nfill such that we can have integer contours. For example,
; nfill=4 and range=[0,4]. The number of color, nfill, can be
increased,
; but "should" be a multiple of (range[1]-range[0]). See example #3.
;
; The limit on colors number (nfill) is 252 by default [ 256-bottom
; in general case], but it can be less if keyword CENTERED is set,
; because additional colors that are not needed for contours are set.
; A warning message is displayed in case the limit is exceeded.
;
;
; EXAMPLES:
; Add POSITION, and compare the following with a set of data between
[-2.,4.]:
; [ Note that divisions (colorbar keyword, default=6) should be set to
; a small divisor of nfill for lisibility. Here default works ]
;
;
; EXAMPLE (1):
; nfill=6 & range=[-2.,4.] ; inputs
;
; contour_definition, nfill, range , 41, ccolors=cc, levels=lev
; contour, data, c_colors=cc, levels=lev
; colorbar, ncolors=nfill, range=range, bottom=4 ; (default bottom
; here)
;
;
; EXAMPLE (2):
; nfill=6 & range=[-2.,4.] ; inputs
;
; contour_definition, nfill, range, /centered, 41, ccolors=cc, $
; levels=lev, shift=shift
; contour, data, c_colors=cc, levels=lev
; colorbar, ncolors=nfill, range=range, bottom=4+shift
;
;
; EXAMPLE (3):
; nfill=120 & range=[-2.,4.] ; inputs
;
; contour_definition, nfill, range, /centered, 41, ccolors=cc, $
; levels=lev, shift=shift
; contour, data, c_colors=cc, levels=lev
; colorbar, ncolors=nfill, range=range, bottom=4+shift, divisions=3
;
;
; Written by Philippe Le Sager, NOA, 2004
;
;-
pro contour_definition, nfill, range, ct, centered=centered,
bottom=bot, $
ccolors=ccolors, levels=levels, shift=shift
on_error, 2
ncolor=nfill
shift=0
if n_elements(bot) eq 0 then bot=4
; levels
step = (range[1] - range[0]) / nfill
levels = indgen(nfill) * step + range[0]
; case of centered ct for diverging data
if keyword_set(centered) then begin
n_above = total( levels ge 0., /integer)
n_below = total( levels lt 0., /integer)
ncolor = ( n_above > n_below )*2
if n_above gt n_below then shift = (ncolor-nfill)
endif
; check ncolor
if fix(ncolor) gt (256-bot) then $
ok = dialog_message(['TOO MUCH COLORS for Contour...', 'Results
are NOT reliable'])
; load ct
LoadCT, ct, NColors=ncolor, Bottom=bot, /SILENT
ccolors = Indgen(nfill)+bot+shift
END
|