Bases: sage.plot.primitive.GraphicPrimitive
Graphics primitive that represents a histogram. This takes quite a few options as well.
EXAMPLES:
sage: from sage.plot.histogram import Histogram
sage: g = Histogram([1,3,2,0], {}); g
Histogram defined by a data list of size 4
sage: type(g)
<class 'sage.plot.histogram.Histogram'>
sage: opts = { 'bins':20, 'label':'mydata'}
sage: g = Histogram([random() for _ in xrange(500)], opts); g
Histogram defined by a data list of size 500
We can accept multiple sets of the same length:
sage: g = Histogram([[1,3,2,0], [4,4,3,3]], {}); g
Histogram defined by 2 data lists
Get minimum and maximum horizontal and vertical ranges for the Histogram object.
EXAMPLES:
sage: H = histogram([10,3,5], normed=True); h = H[0]
sage: h.get_minmax_data()
{'xmax': 10.0, 'xmin': 3.0, 'ymax': 0.4761904761904765, 'ymin': 0}
sage: G = histogram([random() for _ in xrange(500)]); g = G[0]
sage: g.get_minmax_data() # random output
{'xmax': 0.99729312925213209, 'xmin': 0.00013024562219410285, 'ymax': 61, 'ymin': 0}
sage: Y = histogram([random()*10 for _ in xrange(500)], range=[2,8]); y = Y[0]
sage: ymm = y.get_minmax_data(); ymm['xmax'], ymm['xmin']
(8.0, 2.0)
sage: Z = histogram([[1,3,2,0], [4,4,3,3]]); z = Z[0]
sage: z.get_minmax_data()
{'xmax': 4.0, 'xmin': 0, 'ymax': 2, 'ymin': 0}
Computes and draws the histogram for list(s) of numerical data. See examples for the many options; even more customization is available using matplotlib directly.
INPUT:
Note
The weights option works only with a single list. List of lists representing multiple data are not supported.
EXAMPLES:
A very basic histogram for four data points:
sage: histogram([1,2,3,4], bins=2)
Graphics object consisting of 1 graphics primitive
We can see how the histogram compares to various distributions. Note the use of the normed keyword to guarantee the plot looks like the probability density function:
sage: nv = normalvariate
sage: H = histogram([nv(0,1) for _ in xrange(1000)], bins=20, normed=True, range=[-5,5])
sage: P = plot( 1/sqrt(2*pi)*e^(-x^2/2), (x,-5,5), color='red', linestyle='--')
sage: H+P
Graphics object consisting of 2 graphics primitives
There are many options one can use with histograms. Some of these control the presentation of the data, even if it is boring:
sage: histogram(range(100), color=(1,0,0), label='mydata', rwidth=.5, align="right")
Graphics object consisting of 1 graphics primitive
This includes many usual matplotlib styling options:
sage: T = RealDistribution('lognormal', [0,1])
sage: histogram( [T.get_random_element() for _ in range(100)], alpha=0.3, edgecolor='red', fill=False, linestyle='dashed', hatch='O', linewidth=5)
Graphics object consisting of 1 graphics primitive
sage: histogram( [T.get_random_element() for _ in range(100)],linestyle='-.')
Graphics object consisting of 1 graphics primitive
We can do several data sets at once if desired:
sage: histogram([srange(0,1,.1)*10, [nv(0, 1) for _ in xrange(100)]], color=['red','green'], bins=5)
Graphics object consisting of 1 graphics primitive
We have the option of stacking the data sets too:
sage: histogram([ [1,1,1,1,2,2,2,3,3,3], [4,4,4,4,3,3,3,2,2,2] ], stacked=True, color=['blue', 'red'])
Graphics object consisting of 1 graphics primitive
It is possible to use weights with the histogram as well:
sage: histogram(range(10), bins=3, weights=[1,2,3,4,5,5,4,3,2,1])
Graphics object consisting of 1 graphics primitive