Creating a Color Filled Contour Plot
QUESTION: My filled color contour plot seems to have a hole in it. This is especially noticable when I display the filled plot in PostScript. How can I eliminate this hole?
![]()
ANSWER: There are several oddities with filled contour plots, and most of them are tickled when you allow IDL to choose the contour intervals for you by using the NLevels keyword to the Contour command. To give you an idea of what some of the problems are, consider the IDL commands below and the output they generate. The idea is to create a filled contour plot with 12 contour levels. Notice, I use the NLevels keyword to select the levels. According to the IDL documentation, IDL should select 12 "approximately equally spaced contour intervals". (If you would like to run this program on your machine, you will need the LoadData, Scale_Vector, and Colorbar programs from my web page.)
![]()
; Create some data and set up colors for a filled contour plot.
Device, Decomposed=0
levels = 12
data = LoadData(2)
x = Scale_Vector(Findgen(41), 0, 100)
y = Scale_Vector(Findgen(41), 0, 50)
white = GetColor('White', 1)
black = GetColor('Black', 2)
LoadCT, 33, NColors=12, Bottom=3
; Create a window and create a filled contour plot.
; Allow IDL to select the contour intervals.
Window, 0, Title='IDL Selected Contour Intervals', XSize=300, YSize=400
Contour, data, x, y, /Fill, C_Colors=Indgen(levels)+3, Background=1, $
NLevels=levels, Position=[0.1, 0.1, 0.9, 0.80], Color=black
Contour, data, x, y, /Overplot, NLevels=levels, /Follow, Color=black
ColorBar, NColors=12, Bottom=3, Divisions=6, $
Range=[Min(data), Max(data)], Format='(I4)', $
Position = [0.1, 0.9, 0.9, 0.95], Color=black
![]()
The output of these commands is shown below.
What you should notice immediately is that there is a hole in the contour plot (which is easily seen here, since I made the background color white). You might also notice that only nine colors are used in the plot. In fact, IDL has calculated only nine contour intervals! This appears to be a bug to me and I am investigating it with RSI. (I'm running IDL 5.2 on a Windows NT 4 machine.) [Editor's Note: Apparently, this is not a bug. Technical support advised me that if I wanted 12 contour intervals I should “try setting NLEVELS to 14 or 15.” In any case, we are up to IDL 6.3 now, and IDL still works this way.]
The hole comes about because IDL draws fills the space between the first contour interval (about value 154 here) to the second contour interval (about value 307). What we really want is for IDL to fill the space between what we might think of as the "zeroth" contour interval to the first contour interval. (Here the "zeroth" contour interval might have the value of 0, or the minimum value of the data set.)
If this is what we want, then we have to do it ourselves. The Levels keyword allows us to specify exactly which contours levels should be drawn. Assuming we want the first contour interval to be the "zeroth" level, and that we want 12 equally-space contour intervals, we can write the appropriate code like this:
![]()
levels = 12 Window, 1, Title='User Specifed Contour Intervals', XSize=300, YSize=400 step = (Max(data) - Min(data)) / levels userLevels = IndGen(levels) * step + Min(data) Contour, data, x, y, /Fill, C_Colors=Indgen(levels)+3, Background=1, $ Levels=userLevels, Position=[0.1, 0.1, 0.9, 0.80], Color=black Contour, data, x, y, /Overplot, Levels=userLevels, /Follow, Color=black ColorBar, NColors=12, Bottom=3, Divisions=6, $ Range=[Min(data), Max(data)], Format='(I4)', $ Position = [0.1, 0.9, 0.9, 0.95], Color=black
![]()
The results of this code is shown below. Notice that the hole has disappeared and that we are now using all 12 contour colors.
The bottom line is that if you want to make correctly filled contour plots without holes and with the number of contour intervals you specify, you absolutely must create your own contour levels and use the Levels keyword. You must never let IDL select the levels for you by using the NLevels keyword.
Reimar Bauer wishes me to point out that for filled contour plots, the X and Y vectors, if supplied, must be arranged in increasing order. This ensures that the polygons generated will be in counterclockwise order, as required by the mapping graphics pipeline.
![]()
Last Updated 5 April 1999


