next up previous contents
Next: Plotting wind vectors Up: Other tips and tricks Previous: Contours on images   Contents

Colorbars

David Fanning has a handy routine, which I (and many others) use, called colorbar.pro (http://www.dfanning.com/documents/programs.html)

Code from Dominick Spracklen (below) shows a way to use discrete colours in a contoured array and create the corresponding colour bar.

ncon=20 ; number of contours
cdif=10.; contour spacing
cmin=0 ; min contour value
labels=intarr(ncon) ; array of switches to label contours (see contour.pro)
labels(*)=1 ; all set for labelling
conval=fltarr(ncon)
cols=0+12*findgen(ncon+1); Colours to use (0 to 240 in jumps of 12)
conval=(findgen(ncon)*cdif)+cmin ; values of contours 

;Create array to contour
array = (intarr(200)+1)#indgen(200) ; 200 by 200 array

;Output position ([x1,y1,x2,y2])
nopos=[0.15,0.15,0.77,0.9] 

;Contour array
set_plot,'ps'
device,file='~jmarsham/contour.eps',/encapsulated,/color ; colour eps output
loadct,39 ; use 'rainbow' colour table
contour,array,indgen(200),indgen(200)*4/200.,$
position=nopos,levels=conval,c_labels=labels,/cell_fill,/closed,$
c_colors=cols,ytitle='Year of PhD'
;;;;; Add color bar
scale=1.0
nopos = [0.88,0.15,0.93,0.93]
!p.position=nopos
conmin=min(conval)
conmax=max(conval)

;Contours a [2, 22] array i.e. the colourbar
contour,[1,1]#(findgen(ncon+2)),[0,1],$ 
(findgen(ncon+2)-1)*(conmax-conmin)/(ncon-1)+conmin,/cell_fill,/closed,$
levels=(findgen(ncon)+1),$
c_colors=cols,/noerase,yrange=[conmin,conmax],xstyle=4,$
yticks=ncon,yticklen=1,ystyle=1,title='Work Intensity', $
ytickv=findgen(ncon+1)*(conmax-conmin)/(ncon-1)+conmin,charsize=1.,charthick=2

device,/close ; close .eps file
Remember for A # B, where A and B are vectors, IDL performs A # TRANSPOSE(B).

[1,1] is a 2 element vector, (findgen(ncon+2)) is a 14 element vector. So, [1,1]#(findgen(ncon+2)) is FLOAT = Array[2, 14]

;E.G. FILLED CONTOUR ARRAY (IRREGULARLY SPACED CONTOURS) + DISCRETE COLORBAR (JOHN MARSHAM 20/4/05)
ncon=5                      ; number of contours
cmin=0                      ; min contour value
labels=intarr(ncon)         ; array of switches to label contours 
labels(*)=1                 ; all set for labelling
conval=[0,10,100,150,190]   ; vector of irregulalrly spaced contour values
cols=0+25*findgen(ncon+1)   ; Colours to use (0 to 240 in jumps of 12)

array = (intarr(200)+1)#indgen(200) ; Create 'data' array to contour

; Contour 'data' array in colour (filled) 
nopos=[0.15,0.15,0.77,0.9] ; output position ([x1,y1,x2,y2])
set_plot,'ps'
device,file='~jmarsham/contour_arb.eps',/encapsulated,/color ; colour eps
loadct,39                                    ; use 'rainbow' colour table
contour,array,indgen(200),indgen(200)*4/200.,$
position=nopos,levels=conval,c_labels=labels,/cell_fill,/closed,$
c_colors=cols,ytitle='Year of PhD'
;Add lines
contour,array,indgen(200),indgen(200)*4/200.,/noerase,levels=conval,$
c_labels=labels,position=nopos

;Add color bar
scale=1.0
nopos = [0.88,0.15,0.93,0.93] ;position of colorbar
!p.position=nopos
conmin=min(conval)            ;min contour value
conmax=max(conval)            ;max contour value

contour,[1,1]#(indgen(ncon+1)),[0,1],indgen(ncon+1),/cell_fill,$
/closed,levels=indgen(ncon+1),c_colors=cols,/noerase,$
yrange=[0,ncon],xstyle=4,yticks=ncon,ytickv=indgen(ncon+1),$
ytickname=['0','10','100','150','190',' '],$
yticklen=1,ystyle=1,title='Work Intensity',charsize=1.,charthick=2

device,/close



John Marsham 2005-04-22