0% found this document useful (0 votes)
5 views16 pages

Histogram

A histogram is a visual tool for summarizing discrete or continuous data, represented as a column chart where the height indicates the number of data points within specified ranges (bins). The document explains how to create histograms using the hist() function from the PyPlot module in Matplotlib, detailing parameters such as bins, cumulative counts, histtype, and orientation. Examples illustrate the creation of histograms with different configurations, including stacked and grouped data.

Uploaded by

reshmaras
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views16 pages

Histogram

A histogram is a visual tool for summarizing discrete or continuous data, represented as a column chart where the height indicates the number of data points within specified ranges (bins). The document explains how to create histograms using the hist() function from the PyPlot module in Matplotlib, detailing parameters such as bins, cumulative counts, histtype, and orientation. Examples illustrate the creation of histograms with different configurations, including stacked and grouped data.

Uploaded by

reshmaras
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Histogram

• Histogram is a summarization tool for discrete or continuous


data.
• Histograms are column-charts, where each column represents
a range of values, and the height of a column corresponds to
how many values are in that range.
• Histogram provides visual interpretation of numerical data by
showing number of data points that fall within specified range
of values called bins.
• It is similar to a vertical bargraph but shows no gap between
the bars.
hist() of PyPlot module allows you to create and plot histogram

The syntax of hist() function

matplotlib.pyplot.hist(x, bins=value,cumulative=False,
histtype=type, align=alignment,
orientation=orientation)
where,
x: It is list to be plotted on histogram

bins: bins can be integer number computed with + 1 or generated by default.

cumulative: It is a boolean value i.e. either True or False. If provided True then
bins are calculated where each bin gives the counts in that bin plus all bins for
smaller values. The last bin gives total number of data points. The default value is
false.

hisstype: It is an option parameter. It can be any one of these:

bar: Bar type histogram, it arranges data side by side if given data is multiple. It is
by default histtype.
barstacked: When multiple data are stacked on top of each other
step: Generates a lineplot that is by default unfilled
stepfilled: Generates a lineplot that is by default filled
orientation:horizontal or vertical.
import matplotlib.pyplot as plt
marks= [100,45,69,43,85,79,45,69,45,90,100]
plt.hist(marks)
plt.show()
import matplotlib.pyplot as plt
marks= [100,45,69,43,85,79,45,69,45,90,100]
plt.hist(marks,bins=[0,45,50,70,100])
plt.show()

Range:
0-45
45-50
50-70
70-100
import matplotlib.pyplot as plt
marks= [100,45,69,43,85,79,45,69,45,90,100]
plt.hist(marks,bins=[0,45,50,70,100],rwidth=0.9)
plt.show()

Range:
0-45
45-50
50-70
70-100
bins : can be int or sequence or str, optional (default :10)

bins=[0,45,50,70,100]
Will have 4 bins i.e [0 to 44,45 to 49,50to 69,70 to 100]

import matplotlib.pyplot as plt


marks= [100,45,69,43,85,79,45,69,45,90,100]
plt.hist(marks,bins=3,rwidth=0.9)
plt.show()
• A bar graph displays discrete or categorical data, showing the
comparison between different groups, with spaces between the
bars.
• A histogram displays the distribution of continuous data, showing
how frequently values fall within certain ranges (bins) with the
bars touching each other
cumulative: Default is False
import matplotlib.pyplot as plt
marks= [100,45,69,43,85,79,45,69,45,90,100]
plt.hist(marks,bins=[0,45,50,70,100],rwidth=0.9, cumulative=True)
plt.show()
6+4=10

4+2=6

1+3=4

1
histtype-
import matplotlib.pyplot as plt
marks= [100,45,69,43,85,79,45,69,45,90,100]
plt.hist(marks,bins=[0,45,50,70,100],rwidth=0.9,histtype='step')
plt.show()
import matplotlib.pyplot as plt
marks= [100,45,69,43,85,79,45,69,45,90,100]
plt.hist(marks,bins=[0,45,50,70,100],rwidth=0.9, histtype='stepfilled')
plt.show()

❑ No gap between ranges


❑ bar and barstacked is same in one set
of value.
orientation • Default is vertical

import matplotlib.pyplot as plt


marks= [100,45,69,43,85,79,45,69,45,90,100]
plt.hist(marks,bins=[0,45,50,70,100],rwidth=0.9,orientation='horizontal')
plt.show()
import matplotlib.pyplot as plt
Boys= [10,20,11,21,9]
Girls=[11,23,10,9,6]
plt.hist([Boys,Girls],bins=[0,10,20,30],rwidth=0.9)
plt.show()
import matplotlib.pyplot as plt
Boys= [10,20,11,21,9]
Girls=[11,23,10,9,6]
plt.hist([Boys,Girls],bins=[0,10,20,30],rwidth=0.9,color=["r","k"])
plt.show()
import matplotlib.pyplot as plt
Boys= [10,20,11,21,9]
Girls=[11,23,10,9,6]
plt.hist([Boys,Girls],bins=[0,10,20,30],rwidth=0.9,color=["r","k"],histtype='barstacked')
plt.show()

OUTPUT
import matplotlib.pyplot as plt
Boys= [10,20,11,21,9]
Girls=[11,23,10,9,6]
plt.hist([Boys,Girls],bins=[0,10,20,30],rwidth=0.9,color=["r","k"],label=["Boys","Girls"])
plt.legend()
plt.show()

You might also like