English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Histogram is an accurate representation of the distribution of numerical data. It is an estimate of the probability distribution of continuous variables and is a type of bar chart. To build a histogram, please follow the following steps -
Bin value range. Divide the entire value range into a series of intervals. Calculate how many values are in each interval.
Bins are usually specified as continuous, non-overlapping intervals of variables. The matplotlib.pyplot.hist() function draws histograms. It calculates and plots the histogram of x.
Parameters
The table below lists the parameters of the histogram -
x - Array or array sequence. bins - Integer or sequence or auto, optional. range - The lower and upper range of the bins. density - If True, then the first element of the returned tuple will be the count normalized to form a probability density. cumulative - If True, then the histogram, where each bin gives the count in the bin plus all bins with smaller values, is calculated. histtype - The type of histogram to be drawn, default is bar.
The following example depicts a histogram of the marks obtained by students in a class. Four bins are defined, 0-25,26-50,51-75and76-100. The histogram shows the number of students who fall within this range.
Example code:
# Filename : example.py # Copyright : 2020 By w3codebox # Author by : www.oldtoolbag.com # Date : 2020-08-08 #! /usr/bin/env python #coding=utf-8 import matplotlib.pyplot as plt import numpy as np import math plt.rcParams['font.sans']-serif'] = ['SimHei'] # Step 1 (Replacement of sans)-serif font) plt.rcParams['axes.unicode_minus'] = False # Original source from [LidiHuo], commercial redistribution please contact the author for authorization, non-commercial please keep the original link: fig, ax = plt.subplots(1,1]) a = np.array([22,87,5,43,56,73,55,54,11,20,51,5,79,31,27]) ax.hist(a, bins = [0,25,50,75,100]) ax.set_title("Result Histogram") ax.set_xticks([0,25,50,75,100]) ax.set_xlabel('Score') ax.set_ylabel('Number of Students') plt.show()
Execute the above example code to get the following results -