4079
Long error bars from ERRORPLOT are not clipped
Topic
Plotting error bars in the new graphics in IDL 8.0 and 8.1 can produce bars that extend beyond the limites of the plot axis.
The following tech tip explains a workaround for those who would like to see those error bars clipped just at the axis borderline.
Discussion
To produce error bars in a new graphics plot the following function can be used. for example:
non_cropped_plot = errorplot(y, y, yerrors, /histogram, color='green', yrange=[2,17])
But depending on the data set and the limits used to create the plot, error bars can end up crossing the limites of the plot. For example, here is a result that for some users is not acceptable:
Workaround
A simple workaround will be to limit the length of the error bars only to be such that they will fall inside the margins of the plot. The following example code shows how to do that (Note: Please see at the bottom of this article the editable version of this code):
The corresponding results of executing the above code is the following:
Solution
This is the editable version of the example code:
pro cropping_errorbars
y = findgen(20)
; An example of an array of errors.
yerrors = reverse(y)
; The array HAS to be a 2xN, where N=ydim. Even though both dimensions
; are composed by the same elements (for a symetric error bars plot,
; it has to be done so that we can use the asymetric error option in ERRORPLOT
; (see IDL Help on the errorplot() function):
yerror1 = transpose(reverse(y))
yerror2 = transpose(reverse(y))
yerror = [yerror1,yerror2]
ydim=N_ELEMENTS(y)
; The Y range has to be fixed!:
ymax=17
ymin=2
yrange=ymax-ymin
p=plot(y, y, yrange=[ymin, ymax], /nodata)
;Cropping the error bars when they are longer than the Y range:
for i=0, ydim-1 do begin
;Positive error bar
if (yerror[1,i] ge ymax-y[i]) then begin
yerror[1,i]=ymax-y[i]
endif
;Negative error bar
if (yerror[0,i] ge y[i]-ymin) then begin
yerror[0,i]=y[i]-ymin
endif
endfor
cropped_plot = errorplot(y, y, yerror,color='red', /overplot, /histogram)
non_cropped_plot = errorplot(y, y,yerrors,/histogram,color='green', yrange=[2,17])
; Set some properties
cropped_plot.THICK=1
cropped_plot.SYM_COLOR ="cornflower"
cropped_plot.ERRORBAR_COLOR="indian_red"
cropped_plot.ERRORBAR_CAPSIZE=0.5
end