binning histograms python tutorial

© All Rights Reserved

3 views

binning histograms python tutorial

© All Rights Reserved

- Adjustments for Caterpillar
- Fortran Run Time Error Codes
- 07a1ec04-computer-programming
- Tut 8
- VL Chap5 Arrays
- computation calculator
- New Report on Test and Item Analysis[1].Doc 2
- Javase Array
- UniVerse Basic
- FDTD
- MH1401_lab4(3)
- Region
- Arrays
- Primavera Guide
- stl.pdf
- Lecture Material Stat PPT
- mgu data structures 1.Mod 2-1 Introduction
- ABB drive
- Algorithm for Main
- mind game

You are on page 1of 3

When a measurement is made numerous times, it is often useful to bin (or group) the data

and make a histogram. For example, if the time that it takes a sphere to roll down a ramp

was measured one hundred times, then a histogram of the times would show how they are

distributed.

The hist function from the matplotlib library is useful for making histograms. The

first line of a program using it should be the following.

from pylab import *

Suppose t is an array of times. (there are 24 in the example program). The simplest way

to make a histogram is to send the hist function only the data as shown below. As for

other graphs, the figure and show functions are also needed. By default, the histogram

will have 10 bins. If no additional arguments are sent, the hist function decides where

to put the boundaries of the bins.

figure()

hist(t)

show()

The hist function returns the number of events in each bin, the edges of the bins, and

things called patches (which will not be discussed further). These values can be captured

by providing three variable names for them as follows.

events, edges, patches = hist(t)

For the example data, the events array is [0, 0, 2, 2, 4, 13, 2, 1, 0,

0]. The 10 elements are the numbers of events in the 10 bins. The array edges will

contain 11 elements. For the example, the edges array is [2.74, 3.206, 3.672,

4.138, 4.604, 5.07, 5.536, 6.002, 6.468, 6.934, 7.4]. The first

10 elements are the lower edges of the bins and the final element is the upper edge of the

final bin. The bins are the same width, but the edges may end up in unusual places.

You can control the number of bins by setting the bins argument to an integer as shown

below, but this doesnt control the locations of the edges. Choosing an appropriate

number of bins is important. If there are too few or too many bins, the histogram wont

show how the events are distributed very well. Try histogramming the example data with

3 and 30 bins.

events, edges, patches = hist(t, bins=3)

If you want to have control over the number and location of the bins, you can make the

bins argument an array. If you want N bins, the array will have (N + 1) elements. The

first N elements are the lower edges of the bins and the final element is the upper edge of

the final bin. Usually the bins have equal widths, but they can be made unequal. The

desired bin array can be made with the linspace function from the scipy library

(you need the line from scipy import * near the beginning of the program).

You must specify the first element of the array (the lower edge of the first bin), the last

element of the array (the upper edge of the final bin), and the number of elements in the

array (one more than the number of bins). The example below would produce 10 bins

(not 11) starting at 0 and ending at 10. For the example data, several of the bins will be

empty.

bins = linspace(0, 10, 11)

events, edges, patches = hist(t, bins)

If you set the bins manually, it is a good idea to check that none of the data is being left

out of the histogram. You can compare len(t), which is the length of the array t or

the number of data points, and sum(events), which is the sum of all events included

in the histogram.

It is also possible to set the upper and lower limits of the bins using the range argument.

Values outside of the specified range are ignored. The following line should do the same

as the previous example because the default number of bins is 10.

events, edges, patches = hist(t, range=(0.0,10.0) )

If you set the argument normed to True, the function will make an area-normalized

histogram. For each bin, the height on the histogram is the probability density, which is

the number of events in the bin divided by the total number of events and the width of the

bin. The area of each bin in the histogram is the probability of an event being in that bin,

so the total area is one. With this option, the probability density is returned instead of the

number of events.

probden, edges, patches = hist(t, bins, normed=True)

The color argument can be used to set the color of the bars in the histogram.

Alternatively, the edgecolor and facecolor arguments separately set the colors of

the edges and middle of the bars in the histogram, respectively. The facecolor

argument can also be set to none so that the bars only have outlines. Some of other color

options are:

r = red g = green b = blue k = black

c = cyan m = magenta y = yellow w = white

Sometimes data is binned before it is analyzed. For example, a set of decay times could

be binned before fitting the data to an exponential function. The histogram function

from the numpy library can be used to bin data without making a histogram. The first

line of a program using it should be the following.

from numpy import *

The histogram function is similar to the hist function described in the previous

section. The bins and normed arguments can be used, but it doesnt return patches.

Associating the locations of the bins and the numbers of events in them is a little tricky

because the edges array is one element longer than the events array. The example

below will make an array called tmid which is the same length as events and contains

the values of t in the middle of the bins. The resize function makes an array called

lower which has a length one less than the length of the edges array. The new array

contains the locations of the lower edges of the bins because the final element is dropped.

An array containing the difference between consecutive elements of the edges array is

2

returned by diff(edges). Adding half of the difference between the edges to the

lower edge gives the value in the middle of a bin. Note that diff(edges) is the same

length as lower.

events, edges = histogram(t)

lower = resize(edges, len(edges)-1)

tmid = lower + 0.5*diff(edges)

http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.hist

http://docs.scipy.org/doc/numpy/reference/generated/numpy.histogram.html

- Adjustments for CaterpillarUploaded bysportyjo
- Fortran Run Time Error CodesUploaded bykgrhoads
- 07a1ec04-computer-programmingUploaded bySRINIVASA RAO GANTA
- Tut 8Uploaded byapi-3817778
- VL Chap5 ArraysUploaded byHumanonKalimon
- computation calculatorUploaded byMofoleni Bolabi Coloni II
- New Report on Test and Item Analysis[1].Doc 2Uploaded byapi-3705578
- Javase ArrayUploaded byJosephus Villarey
- UniVerse BasicUploaded byKit Fung Chai
- FDTDUploaded bynevada1292
- MH1401_lab4(3)Uploaded byLivardy Wufianto
- RegionUploaded byAmir Wagdarikar
- ArraysUploaded byDevprasadMishra
- Primavera GuideUploaded bytrung2i
- stl.pdfUploaded byIgor
- Lecture Material Stat PPTUploaded bysarge1986
- mgu data structures 1.Mod 2-1 IntroductionUploaded byrobinpt
- ABB driveUploaded byRol2
- Algorithm for MainUploaded bybhargav
- mind gameUploaded byAdrian E. Lubigan
- Automation Technology AppNote Matlab Image Acquisition ToolboxUploaded byrk
- C++(2006)Uploaded byYash Gupta
- Matlab commands and functions (rand, size etc)Uploaded byImran Parvez
- Java Collection Interview QuestionsUploaded bysaumitra ghogale
- 7 QC TOOLSUploaded bybrch10
- Quality Tools2Uploaded byAdiraju Kranthi Kumar
- Types of GraphsUploaded byParamita Karan
- Computer 2Uploaded byAbhi Sharma
- intr_ds.pptUploaded byKen Ken Sanares
- A_Practical_Introduction_to_Python_Programming_Heinold.pdfUploaded byShailesh Gawade

- SeidenbergPlaut2014-2Uploaded byS
- doriangray_englishtextUploaded byS
- Lullaby+of+Birdland+-+George+Shearing+2LGCP+piano (1)Uploaded byCasimiro
- Vowel Minimal Pairs - Sheet1Uploaded byS
- Template_Vowels.txtUploaded byS
- Chopin Frederic Nocturne Major 381Uploaded byrezkanthel
- Country Road (Whisper of the Heart)Uploaded byHwang Cheeyan
- TFPUploaded byS

- BMM SyallabusUploaded bysushanta21
- Survival Guide for NCCUUploaded byFeily
- PHY-TEST-EM04(3-3)Uploaded byphysicspalanichamy
- Paket Try Out 1 Bahasa Inggris Ta 2008 Utk Gugus 01Uploaded byiin hermiyanto
- Kim Lighting Concept 5000 Series Brochure 1974Uploaded byAlan Masters
- WorldcomUploaded byVinay Jugoo
- Protists and FungiUploaded byJillian Lao
- Chapter OneUploaded bycoolchethan
- Computer.gaming.world.octUploaded byapi-3808087
- Tdsm Lecture NotesUploaded byNurul'Ain Haniyun Mohamad Fodzi
- Multiple Linear RegressionUploaded byjsanderson81
- San Diego v. Roe, 543 U.S. 77 (2004)Uploaded byScribd Government Docs
- HW5SolsUploaded byDon Nguyen
- AgUploaded byAntony Neciosup
- Bare Minimum Configuration for WM to Work Along With Inventory ManagementUploaded byolivier laroche
- External Metric Thread Table ChartUploaded byVarun Kumar
- Ratiba ya NECTA Kidato Cha Nne 2015Uploaded byDennisEudes
- AmelieUploaded byNandivelugu Lalithya Karthikeya
- Exploring Parallel ComputingUploaded byFabianFrie
- jurnal dehidrasiUploaded byIzza Ayudia Hakim
- kiokUploaded bydavidrajhivc
- Adjtimex Man InstructUploaded byBharanitharan Sundaram
- Ionic Conduction in Polymer CompositeUploaded byOmed Ghareb
- SAP Basis Sample Resume (3)Uploaded byYogesh Arasu
- 582Uploaded bywawanana
- Industrial Analog ControllersUploaded byAhmed Nahrawy
- IPS White PaperUploaded byRoxyBen
- electronic exit tickets in the classroom elyce coleUploaded byapi-247292241
- 215121515-Teething-Dolly.pdfUploaded byElena Nitulescu
- Entrepreneurial Learning---Conceptual Framework for Technology Based EntrprisUploaded byMadiha Khan

## Much more than documents.

Discover everything Scribd has to offer, including books and audiobooks from major publishers.

Cancel anytime.