Gallery: Histograms
Examples
- Displaying (x,y) data points as a histogram
- Displaying (xlow,xhigh,y) data points as a histogram
- A histogram which shows the bin edges
- A histogram showing the full range of options
- Filling a histogram with a pattern
1) Displaying (x,y) data points as a histogram
Histograms are used to plot binned one-dimensional data - of the form (xmid, y) or (xlow, xhigh, y) - with optional error bars on the Y values. A later example shows how to display error bars on the points.
add_histogram("spectrum.fits[fit][cols x,y]")
In this example the data is stored as a set of (x, y) points, where the x values give the center of each bin. Unlike curves, histograms default to only being drawn by a solid line; the symbol.style attribute is set to none.
The preferences for curves can be found by using the get_preference call:
chips> get_preference("histogram")
histogram.stem : hist
histogram.depth : default
histogram.line.color : default
histogram.line.thickness: 1
histogram.line.style : solid
histogram.symbol.color : default
histogram.symbol.style : none
histogram.symbol.size : 5
histogram.symbol.angle : 0
histogram.symbol.fill : false
histogram.err.color : default
histogram.err.thickness: 1
histogram.err.style : line
histogram.err.up : on
histogram.err.down : on
histogram.err.caplength: 10
histogram.dropline : off
histogram.fill.color : default
histogram.fill.opacity : 1
histogram.fill.style : nofill
The settings for the current histogram can be found by using the get_histogram routine:
chips> get_histogram() depth = 100 dropline = False err.caplength = 10 err.color = default err.down = False err.style = line err.thickness = 1.0 err.up = False fill.color = default fill.opacity = 1.0 fill.style = 0 id = None line.color = default line.style = 1 line.thickness = 1.0 stem = None symbol.angle = 0.0 symbol.color = default symbol.fill = False symbol.size = 5 symbol.style = 0
2) Displaying (xlow,xhigh,y) data points as a histogram
In the first example, the binned data was given using the mid-point of each bin. In this example we show how histograms can be plotted by giving the low and high edges of each bin.
tbl = read_file("spectrum.fits[fit]")
xlo = copy_colvals(tbl,"xlo")
xhi = copy_colvals(tbl,"xhi")
y = copy_colvals(tbl,"y")
add_histogram(xlo,xhi,y,["line.color","red"])
log_scale(X_AXIS)
The bin edges can not be specified by passing in a file name to add_histogram, so we have to read in the arrays using crates routines and then plot them. We use the read_file to read in the file, copy_colvals to get the column values, and then add_histogram to plot them.
3) A histogram which shows the bin edges
When using xmid values, the bins are assumed to be contiguous. This is not the case when the bin edges - namely xlow and xhigh - are given. Here we plot data from a histogram with non-contiguous bins, setting the dropline attribute so that all the bin edges are drawn (this attribute can also be used with histograms like that used in the first example).
tbl = read_file("histogram.fits")
xlo = copy_colvals(tbl,"xlo")
xhi = copy_colvals(tbl,"xhi")
y = copy_colvals(tbl,"y")
add_histogram(xlo,xhi,y,["line.style","longdash","dropline",True])
4) A histogram showing the full range of options
In this example we change most of the attributes of a histogram. The Filling a histogram with a pattern example shows how you can change the fill style of a histogram from solid to a pattern.
tbl = read_file("histogram.fits")
xlo = copy_colvals(tbl,"xlo")
xhi = copy_colvals(tbl,"xhi")
y = copy_colvals(tbl,"y")
dylo = copy_colvals(tbl,"dylo")
dyhi = copy_colvals(tbl,"dyhi")
hist = ChipsHistogram()
hist.dropline = True
hist.line.color = "red"
hist.symbol.style = "diamond"
hist.symbol.size = 4
hist.symbol.fill = True
hist.symbol.color = "orange"
hist.err.color = "green"
hist.fill.style = "solid"
hist.fill.opacity = 0.2
hist.fill.color = "blue"
add_histogram(xlo,xhi,y,dylo,dyhi,hist)
# Move the histogram behind the axes so that the tick marks are not hidden
shuffle_back(chips_histogram)
A note on filling histograms
There are two issues when using the solid fill pattern:
-
Opacity values are not used in the postscript output, in that they are always taken to be 1.
-
The off-screen output may show a pattern of lines within the filled region for postscript and PDF outputs, depending on what display program you are using. They should not appear when printed out.
5) Filling a histogram with a pattern
In this example we fill the histogram using a pattern, rather than a solid fill as used in the A histogram showing the full range of options example.
add_histogram("spectrum.fits[fit][cols x,y]")
set_histogram(["fill.style","crisscross"])
set_histogram(["fill.color","green","line.color","red"])
The fill.style attribute of histograms is used to determine how the region is filled. Here we use the value "crisscross", rather than "solid", to fill the histogram with crossed lines. These lines can be colored independently of the histogram boundary.

![[ChIPS Logo]](../imgs/chips_logo_navbar.gif)