PLOTTING WITH PYPLOT II – HISTOGRAM,
FREQUENCY DISTRIBUTION, BOXPLOTS
Chapter – 4 (FIRST PART)
Class – XII
In this chapter, we will learn to create histogram,
frequency polygons and boxplots using pyplot module of matplotlib library.
Creating Histogram with Pyplot: A
histogram is a summarization tool for discrete or continuous data. A histogram
provides a visual interpretation of numerical data by showing the number of
data points that fall within a specified range of values (called bins). It is
similar to a vertical bar graph.
Difference between a Histogram and Bar Chart:
A Bar Chart majorly represents categorical data, they
are usually represented using rectangular bars with lengths proportional to the
values that they represent.
While histogram on the other hand, is used to describe
distribution. Given the set of data, what are their distributions
To draw Histogram in Python following concepts must be
clear:
Title: To display
heading of the histogram.
Color: To show the
color of the bar.
Axis: x-axis and
y-axis.
Data: The data can
be represented as an array.
Height and width of bars:
This is determined based on the analysis. The width of the bar is called bin or
intervals.
Border color: To display
border color of the bar.
There are various ways to create histogram in python
pandas. One of them is using matplotlib python library. Using this library, we
can easily create histogram.
So, install matplotlib library using following
statement at command prompt:
>pip install matplotlib
Example: Program in
python. Develop a python program with below code and execute it.
import
numpy as np import
matplotlib.pyplot as plt
data
= [50,11,21,31,41] plt.hist([5,15,25,35,45,
55], bins=[0,10,20,30,40,50, 60], weights=[20,10,45,33,6,8],
edgecolor="red")
plt.show() |
|
In the above example,
First argument of hist() method is position (x, y
coordinate) of weight, where weight is to be displayed. No of coordinates must
match with no of weight otherwise error will generate.
Second argument is interval.
And third argument is weight for bars.
import
numpy as np import
matplotlib.pyplot as plt
data
= [50,11,21,31,41] plt.hist([5,15,25,35,15,
55], bins=[0,10,20,30,40,50, 60], weights=[20,10,45,33,6,8],
edgecolor="red") plt.show()
|
|
Note: At interval (bin) 40 to 50 no bar because we
have not mentioned from 40 to 50 in first argument of hist method. Where as in
interval 10 to 20 width is being displayed as 16 (10+6 both weight are added)
because 15 is twice in first argument.
No comments:
Post a Comment
Please do not any spam in the comment box.