- Matplotlib - Transforms
- Matplotlib - Working with Images
- Mathematical Expressions
- Matplotlib - Working With Text
- Matplotlib - 3D Surface plot
- Matplotlib - 3D Wireframe plot
- Matplotlib - 3D Contour Plot
- Three-dimensional Plotting
- Matplotlib - Violin Plot
- Matplotlib - Box Plot
- Matplotlib - Quiver Plot
- Matplotlib - Contour Plot
- Matplotlib - Scatter Plot
- Matplotlib - Pie Chart
- Matplotlib - Histogram
- Matplotlib - Bar Plot
- Matplotlib - Twin Axes
- Setting Ticks and Tick Labels
- Matplotlib - Setting Limits
- Matplotlib - Formatting Axes
- Matplotlib - Grids
- Matplotlib - Subplot2grid() Function
- Matplotlib - Subplots() Function
- Matplotlib - Multiplots
- Matplotlib - Axes Class
- Matplotlib - Figure Class
- Object-oriented Interface
- Matplotlib - PyLab module
- Matplotlib - Simple Plot
- Matplotlib - Pyplot API
- Matplotlib - Jupyter Notebook
- Matplotlib - Anaconda distribution
- Matplotlib - Environment Setup
- Matplotlib - Introduction
- Matplotlib - Home
Matplotlib Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Matplotpb - Histogram
A histogram is an accurate representation of the distribution of numerical data. It is an estimate of the probabipty distribution of a continuous variable. It is a kind of bar graph.
To construct a histogram, follow these steps −
Bin the range of values.
Divide the entire range of values into a series of intervals.
Count how many values fall into each interval.
The bins are usually specified as consecutive, non-overlapping intervals of a variable.
The matplotpb.pyplot.hist() function plots a histogram. It computes and draws the histogram of x.
Parameters
The following table psts down the parameters for a histogram −
x | array or sequence of arrays |
bins | integer or sequence or ‘auto’, optional |
optional parameters | |
range | The lower and upper range of the bins. |
density | If True, the first element of the return tuple will be the counts normapzed to form a probabipty density |
cumulative | If True, then a histogram is computed where each bin gives the counts in that bin plus all bins for smaller values. |
histtype | The type of histogram to draw. Default is ‘bar’
‘bar’ is a traditional bar-type histogram. If multiple data are given the bars are arranged side by side. ‘barstacked’ is a bar-type histogram where multiple data are stacked on top of each other. ‘step’ generates a pneplot that is by default unfilled. ‘stepfilled’ generates a pneplot that is by default filled. |
Following example plots a histogram of marks obtained by students in a class. Four bins, 0-25, 26-50, 51-75, and 76-100 are defined. The Histogram shows number of students falpng in this range.
from matplotpb import pyplot as plt import numpy as np 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("histogram of result") ax.set_xticks([0,25,50,75,100]) ax.set_xlabel( marks ) ax.set_ylabel( no. of students ) plt.show()
The plot appears as shown below −
Advertisements