|
|
|
|
Gallery: Histograms (Python)Examples
Displaying (x,y) data points as a histogramHistograms 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> 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 histogramIn 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. Changes in CIAO 4.1
A histogram which shows the bin edgesWhen 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",1])
Changes in CIAO 4.1
A histogram showing the full range of optionsIn 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 = 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 histogramsThere are two issues when using the solid fill pattern:
Changes in CIAO 4.1
Filling a histogram with a patternIn 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.
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. |
![]() |
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. |