Hello helpful IDL experts!
I have been struggling with this for the past couple of days so hope someone on here might be able to put me straight!
I am trying to plot monthly temperature and rainfall in a Walter & Lieth style climate diagram. I am struggling to work out how to accurately define the polygons of the intersecting lines, as seen in this example R plot: http://www.sisef.it/forest@/papers//no19/Bagnato_679@image00 2.jpg
It is made problematic by the scale change part way up the graph. The code I have so far is copied below - many thanks in advance for any suggestions!
Jess
set_plot, 'x'
!p.multi=[0,2,1]
mean_temp=[26.8,26.8,27.0,27.0,26.1,25.4,25.6,26.2,27.4,27.8 ,27.1,26.8]
mean_precip=[277.9,260.4,257.6,169.0,89.3,31.7,18.9,23.2,79. 6,163.4,207.6,255.5]
;define monthly series
months=TIMEGEN(12, START=JULDAY(1,1,2003,0,0,0),UNITS="Month")
xr1=months[0]
xr2=months[-1]
;top section of plot
cgplot,months,findgen(12),xrange=[xr1,xr2],yrange=[100,300], xstyle=5,ystyle=5,position=[0.05,0.80,0.95,0.95],/nodata
cgaxis, yaxis=1, yrange=[100,300],yminor=1,yticks=1,YTICKFORMAT="(A1)",color='grey',/save
cgtext,xr2+3,450,'300', /data, charsize=1.5,color='dodger blue'
cgoplot,months,mean_precip,color='dodger blue'
;------------------ fill in polygon below precipitation line in top section
aboveColor='blu5'
FOR j=0,N_Elements(months)-2 DO BEGIN
data=mean_precip
yfit=intarr(12)+100
; Set initial coordinates.
x0 = months[j]
x1 = months[j+1]
y0 = data[j]
y1 = data[j+1]
f0 = yfit[j]
f1 = yfit[j+1]
xcoords = [x0, x0, x1, x1, x0]
ycoords = [f0, y0, y1, f1, f0]
CASE 1 OF
(y0 GE f0) && (y1 GE f1): cgColorfill, xcoords, ycoords, Color=aboveColor
(y0 LT f0) && (y1 GT f1): BEGIN
theta = ATan( (y1-y0)/(x1-x0) )
x2 = (f0-y0) / Tan(theta) + x0
xcoords = [x2, x1, x1, x2]
ycoords = [f0, y1, f1, f0]
cgColorfill, xcoords, ycoords, Color=aboveColor
END
(y0 GT f0) && (y1 LT f1): BEGIN
theta = ATan( (y1-y0)/(x1-x0) )
x2 = (f0-y0) / Tan(theta) + x0
xcoords = [x0, x0, x2, x0]
ycoords = [f0, y0, f1, f0]
cgColorfill, xcoords, ycoords, Color=aboveColor
END
ELSE:
ENDCASE
ENDFOR
cgaxis,xaxis=0,xrange=[xr1,xr2],XTICKFORMAT="(A1)",color='grey',xstyle=1,xticks=1,xminor=1
cgaxis, yaxis=0, yrange=[50,65],YTICKFORMAT="(A1)",yminor=1,yticks=1,color='grey',/save
;lower section of plot
cgplot,months,mean_temp,xrange=[xr1,xr2],$
xstyle=5,ystyle=5,position=[0.05,0.05,0.95,0.80],/nodata
cgaxis,xaxis=0,xrange=[xr1,xr2],XTICKUNITS=['Months'],xticki nterval=1,xminor=1,xstyle=1,color='grey'
cgaxis, yaxis=0, yrange=[0,50],yticks=5,yminor=1,ystyle=1,color='red'
cgaxis, yaxis=0, yrange=[0,50],yticks=5,yminor=1,ystyle=1,color='grey',YTICKF ORMAT= "(A1)",/save
cgoplot,months,mean_temp,color='red'
cgaxis, yaxis=1,yrange=[0,100],yticks=5,yminor=1,ystyle=1,color='dod ger blue',/save
cgoplot,months,mean_precip,color='dodger blue'
cgaxis, yaxis=1,yrange=[0,100],yticks=5,yminor=1,ystyle=1,color='gre y',YTICKFORMAT= "(A1)"
end
;;Up to this point the plot looks ok!! Then I try to fill in above and below the line with the following code and it fails miserably...
FOR j=0,N_Elements(months)-2 DO BEGIN
data=mean_precip
yfit=mean_temp*2
; Set initial coordinates.
x0 = months[j]
x1 = months[j+1]
y0 = data[j]
y1 = data[j+1]
f0 = yfit[j]
f1 = yfit[j+1]
xcoords = [x0, x0, x1, x1, x0]
ycoords = [f0, y0, y1, f1, f0]
;if (data[j] gt 100.) then y0=100
;if (data[j+1] gt 100.) then y1=100
f0 = yfit[j]
f1 = yfit[j+1]
xcoords = [x0, x0, x1, x1, x0]
ycoords = [f0, y0, y1, f1, f0]
; Is this a below the line polygon?
CASE 1 OF
(y0 LE f0) && (y1 LE f1): cgColorfill, xcoords, ycoords, Color=belowColor
(y0 LE f0) && (y1 GT f1): BEGIN
theta = ATan( (y1-y0)/(x1-x0) )
x2 = (f0-y0) / Tan(theta) + x0
xcoords = [x0, x0, x2, x0]
ycoords = [y0, f0, f0, y0]
cgColorfill, xcoords, ycoords, Color=belowColor
END
(y0 GT f0) && (y1 LT f1): BEGIN
theta = ATan( (y1-y0)/(x1-x0) )
x2 = (f0-y0) / Tan(theta) + x0
xcoords = [x2, x1, x1, x2]
ycoords = [f0, f1, y1, f0]
cgColorfill, xcoords, ycoords, Color=belowColor
END
ELSE:
ENDCASE
; Is this an above the line polygon?
CASE 1 OF
(y0 GE f0) && (y1 GE f1): cgColorfill, xcoords, ycoords, Color=aboveColor
(y0 LT f0) && (y1 GT f1): BEGIN
theta = ATan( (y1-y0)/(x1-x0) )
x2 = (f0-y0) / Tan(theta) + x0
xcoords = [x2, x1, x1, x2]
ycoords = [f0, y1, f1, f0]
cgColorfill, xcoords, ycoords, Color=aboveColor
END
(y0 GT f0) && (y1 LT f1): BEGIN
theta = ATan( (y1-y0)/(x1-x0) )
x2 = (f0-y0) / Tan(theta) + x0
xcoords = [x0, x0, x2, x0]
ycoords = [f0, y0, f1, f0]
cgColorfill, xcoords, ycoords, Color=aboveColor
END
ELSE:
ENDCASE
ENDFOR
|