About Chandra Archive Proposer Instruments & Calibration Newsletters Data Analysis HelpDesk Calibration Database NASA Archives & Centers Chandra Science Links

Skip the navigation links
Last modified: 9 July 2009

URL: http://cxc.harvard.edu/chips4.1/gallery/histograms.py.html

Gallery: Histograms (Python)

Examples


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.

[ChIPS output]
Postscript version
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> print 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.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> print get_histogram()
depth = 100
dropline = False
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

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.

[ChIPS output]
Postscript version
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.

Changes in CIAO 4.1


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).

[ChIPS output]
Postscript version
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",1])

Changes in CIAO 4.1


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.

[ChIPS output]
Postscript version
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 = 1
hist.line.color = "red"
hist.symbol.style = "diamond"
hist.symbol.size = 4.0
hist.symbol.fill = 1
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:

  1. Opacity values are only used in the on-screen version; for the off-screen versions - i.e. those created by print_window - the opacity of a histogram is set to 1.

  2. The off-screen output often shows a pattern of lines within the filled region. This is seen with the PNG and JPEG formats (for example in the output shown above).

Changes in CIAO 4.1

  1. The new copy_colvals routine is used instead of get_colvals to read the column data from the file.

  2. The histogram fill is now controlled by the fill.style attribute rather than fill.visible, since it is now possible to fill histograms with patterns rather than just a solid fill. In CIAO 4.0 the line was

    hist.fill.visible = 1
    

    and it is now

    hist.fill.visible = "solid"
    

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. This is a new capability, added in CIAO 4.1.

[ChIPS output]
Postscript version
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.

Last modified: 9 July 2009


The Chandra X-Ray Center (CXC) is operated for NASA by the Smithsonian Astrophysical Observatory.
60 Garden Street, Cambridge, MA 02138 USA.    Email: cxcweb@head.cfa.harvard.edu
Smithsonian Institution, Copyright © 1998-2004. All rights reserved.