; docformat = 'rst' ;+ ; This is an example program to demontrate how to create a linear trend plot ; with Coyote Graphics routines. ; ; :Categories: ; Graphics ; ; :Examples: ; Save the program as "linear_trend_plot.pro" and run it like this:: ; IDL> .RUN linear_trend_plot ; ; :Author: ; FANNING SOFTWARE CONSULTING:: ; David W. Fanning ; 1645 Sheely Drive ; Fort Collins, CO 80526 USA ; Phone: 970-221-0438 ; E-mail: david@idlcoyote.com ; Coyote's Guide to IDL Programming: http://www.idlcoyote.com ; ; :History: ; Change History:: ; Written, 28 February 2014 by David W. Fanning. ; Modified polygon filling algorithm at suggestion of Kang Wang to avoid problems at crossings. 8 Mar 2014. DWF. ; ; :Copyright: ; Copyright (c) 2014, Fanning Software Consulting, Inc. ;- PRO Linear_Trend_Plot ; Create some trend data with variation. variation = Scale_Vector(cgDemoData(1), 5, 20) trend = 0.075 * Findgen(101) data = trend + variation ; Create a time vector. time = cgScaleVector(Findgen(101), 0, 10) ; Other properties (usually passed via keywords). aboveColor = 'BLU5' belowColor = 'RED5' xtitle = 'Years' ytitle = 'Population' ; Plot it. cgPlot, time, data, XTitle=xtitle, YTitle=ytitle ; Fit a linear curve to the data to show the trend. params = LinFit(time, data, /Double, YFIT=yfit) cgPlot, time, yfit, COLOR='black', /Overplot ; Step though the plot, one section at a time, filling each section ; with the appropriate polygon color. The crossing points are calculated ; correctly for each polygon. FOR j=0,N_Elements(time)-2 DO BEGIN ; Set initial coordinates. x0 = time[j] x1 = time[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] ; 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 ; Repair the plot after polygon filling. cgPlot, time, data, /Overplot cgPlot, time, yfit, /Overplot END ;-------------------------------------------------------------------- ; This main program shows how to call the program and produce ; various types of output. ; Display the plot in a graphics window. Linear_Trend_Plot ; Display the plot in a resizeable graphics window. cgWindow, 'Linear_Trend_Plot', WBackground='White', $ WTitle='Linear Trend Plot in a Resizeable Graphics Window' ; Create a PostScript file. cgPS_Open, 'linear_trend_plot.ps' Linear_Trend_Plot cgPS_Close ; Create a PNG file with a width of 600 pixels. cgPS2Raster, 'linear_trend_plot.ps', /PNG, Width=600 END