#! /usr/bin/gawk -f # Last edited on 2026-02-04 06:00:04 by stolfi # Reads a histogram data file as created by {make_histogram.gawk}. # Writes to {stdout} the polygonal outline of a series of rectangular bars # that can be used with the "filledcurve" style of gnuplot to plot the histogram. # # Each line of the input must have four fields "{XLO} {XMD} {XHI} # {YVAL}" where {VAL} is assumed to be the number of original values {X} # that fell in the range {[XLO _ XHI]}; and {XMD} is the position where # the bar of that intervalshould be plotted in the histogram. Usually # {XMD} is the the midpoint of the interval, but may not be at the ends. # # Input lines with {VAL} equal to zero are ignored. Otherwise the {VAL} # may be positive or negative. # # This program assumes that a certain number {num} of histograms are to # be plotted together by gnuplot, and this is the histogram with index # {which} in {0..num-1}. # # User must define with "-v" the variables {num} and {which}. BEGIN { nbar = 0; # Count of bars generated. ndat = 0; # Count of input data records read. if (num == "") { arg_error("must define {num}") } if (which == "") { arg_error("must define {which}") } num += 0; which += 0; printf "converting histogram %d of %d to polyline...\n", which, num > "/dev/stderr"; } /^ *[-+0-9.]/ { if (NF != 4) { data_error("bad NF") } ndat++; xlo = $1+0; xmd = $2+0; xhi = $3+0; val = $4+0 # Determine the bin width: hblo = xmd - xlo; hbhi = xhi = xmd; bwd = 2 * (hblo < hbhi ? hblo : hbhi) # Determine the {X} range of all the bars: pwd = bwd*num/(num + 1) plo = xmd - pwd/2 phi = xmd + pwd/2 # Determine the low and high {X} for this bar: rwd = pwd/num eps = 0.1*rwd rlo = plo + (which+0)*rwd + eps; rhi = plo + (which+1)*rwd - eps; # Output the vertices of the polygonal: if (val > 0) { print ""; print rlo, 0; print rlo, val; print rhi, val; print rhi, 0; nbar++; } next; } // { data_error("bad format"); } END { printf "%5d histogram entries\n", ndat > "/dev/stderr"; printf "%5d non-zero histogram bars\n", nbar > "/dev/stderr"; if (nbar == 0) { prog_error("no bins"); } print ""; }