0% found this document useful (0 votes)
7 views

Notes 58 Creating Histogram

dfgfh

Uploaded by

sadafanjum042004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Notes 58 Creating Histogram

dfgfh

Uploaded by

sadafanjum042004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Creating Histogram with Pyplot

A Histogram is a graphical display of data using bars of different heights. In a


histogram, each bar groups number into ranges. The highest or tallest bars show
that more data falls in that range of values(called bins). We use hist() function for
generating histogram in Pyplot.

Difference between bar chart and Histogram:

Bar chart represents a single value whereas histogram represents range of value.
It is similar to bar graph but it doesn’t show gaps between the bars.

Syntax:

Matplotlib.pyplot.hist(x, bins=None, cumulative=False, histtype= ‘bar’, align= ‘mid’,


orientation= “vertical”)

Here,

x array or sequence to be plotted on histogram


bins it is optional. If an integer is given it automatically divides the number of
ranges (i.e. if bins=4, the whole list will be divided into for 4 ranges). We
can also define the sequence in bins also.
Cumulative it is optional. It takes Boolean value i.e. True or False. If it is True, then
a histogram, is computed where each bin gives the counts in that bin plus
all bins for smaller values. The last bin gives the total number of data
points.
histtype it is optional. It has following types: {bar, barstacked, step, stepfilled}. By
default the value is bar.
Orientation: it is optional. It has two values i.e. horizontal and vertical.

Example1:

import matplotlib.pyplot as pl
a=[2,3,7,19,6,8,11,14,15,22,33,24,22,8,2,4]
pl.hist(a)
pl.show()

import matplotlib.pyplot as pl
a=[2,3,7,6,8,12,4]
pl.hist(a)
pl.show()
Defining Range using bins:

import matplotlib.pyplot as pl
a=[2,3,7,6,8,12,4]
pl.hist(a,bins=[1,5,10,15])
pl.show()

import matplotlib.pyplot as pl
a=[2,3,7,6,8,12,4]
pl.hist(a,bins=[1,10,20])
pl.show()

You might also like