Filled Area Plot
http://www.idlcoyote.com/gallery/filled_area_plot.png
PRO Filled_Area_Plot_FG, WINDOW=awindow
compile_opt strictarr
; Set up variables for the plot. Normally, these values would be
; passed into the program as positional and keyword parameters.
x = Findgen(101)
y = 4 * Sin(x * !DtoR) / Exp( (x-15) / 25.)
; Set up the low and high x indices of the area under the curve
; you want to fill.
low = 20
high = 45
; Find the y indices associated with the low and high indices.
lowY = 4 * Sin(low * !DtoR) / Exp( (low-15) / 25.)
highY = 4 * Sin(high * !DtoR) / Exp( (high-15) / 25.)
indices = Value_Locate(x, [low, high])
lowIndex = indices[0]
highIndex = indices[1]
; Make sure the indices you find correspond the the right X indices.
IF x[lowIndex] LT low THEN lowIndex = lowIndex + 1
IF x[highIndex] GT high THEN highIndex = highIndex - 1
; Open a window and return its reference to the user.
aWindow = Window(WINDOW_TITLE="Filled Area Plot")
;
;APPROACH 1:
; Similar to the Coyote example, create a plot, then fill it with a polygon.
;
; Draw the plot axes.
fgPlot = Plot(x, y, /Current, XTitle='X Axis', YTitle='Y Axis', Color='red', $
Name='4*Sin(x) $\slash$ e^(x-15) $\slash$ 25', LAYOUT=[1,2,1], $
Title='Example Using Polygon')
; Create closed polygons to color fill.
yMin = fgPlot.yrange[0]
xpoly = [ low, low, x[lowIndex:highIndex], high, high]
ypoly = [yMin, lowY, y[lowIndex:highIndex], highY, yMin]
; Create a filled polygon and keep it in the data space.
fgPoly = Polygon(xpoly, ypoly, /Data, Target=fgPlot, /Fill_Background, $
Fill_Color='Dodger Blue', Name='Area Under Plot')
; Bring the plot to the front
fgPlot -> Order, /Bring_Forward
;
;APPROACH 2:
; Using the FILL_* keywords in the Plot function. This does not reproduce the
; Coyote example, but it illustrates another manner of filling the area under
; the plot with a polygon. Adjust Fill_Level to see what happens.
;
; It would be nice if there were Fill_XRange and Fill_YRange keywords.
;
; Draw the plot axes and fill the area below it.
fgPlot = Plot(x, y, /Current, XTitle='X Axis', YTitle='Y Axis', Color='red', $
Name='4*Sin(x) $\slash$ e^(x-15) $\slash$ 25', $
/Fill_Background, Fill_Color='Dodger Blue', Fill_Level=0.3, $
Layout=[1,2,2], Title='Example using Fill_* Keywords (Fill_Level=0.3)')
END ;*********************************************************** ******
; This main program shows how to call the program and produce
; various types of output.
; Display the plot in a resizeable graphics window.
Fill_Area_Plot_FG, Window=window
; Create a PostScript file. Linestyles are not preserved in IDL 8.2.3 due
; to a bug. Only encapsulated PostScript files can be created.
window.save, 'filled_area_plot_fg.eps'
; Create a PNG file with a width of 600 pixels. Resolution of this
; PNG file is not very good.
window.save, 'filled_area_plot_fg.png', WIDTH=600
; For better resolution PNG files, make the PNG full-size, then resize it
; with ImageMagick. Requires ImageMagick to be installed.
window.save, 'filled_area_plot_fg_fullsize.png'
Spawn, 'convert filled_area_plot_fg_fullsize.png -resize 600 filled_area_plot_fg_resized.png'
END
|