# Plotting Histogram in Python using Matplotlib

## Plotting Histogram in Python using Matplotlib

A histogram is basically used to represent data provided in a form of some groups.It is accurate method for the graphical representation of numerical data distribution.It is a type of bar plot where X-axis represents the bin ranges while Y-axis gives information about frequency.

## Creating a Histogram

To create a histogram the first step is to create bin of the ranges, then distribute the whole range of the values into a series of intervals, and count the values which fall into each of the intervals.Bins are clearly identified as consecutive, non-overlapping intervals of variables.The matplotlib.pyplot.hist() function is used to compute and create histogram of x.

The following table shows the parameters accepted by matplotlib.pyplot.hist() function :

Let’s create a basic histogram of some random values. Below code creates a simple histogram of some random values:

 `from` `matplotlib ``import` `pyplot as plt` `import` `numpy as np` `# Creating dataset` `a ``=` `np.array([``22``, ``87``, ``5``, ``43``, ``56``,` `              ``73``, ``55``, ``54``, ``11``,` `              ``20``, ``51``, ``5``, ``79``, ``31``,` `              ``27``])` `# Creating histogram` `fig, ax ``=` `plt.subplots(figsize ``=``(``10``, ``7``))` `ax.hist(a, bins ``=` `[``0``, ``25``, ``50``, ``75``, ``100``])` `# Show plot` `plt.show()`

Output :

## Customization of Histogram

Matplotlib provides a range of different methods to customize histogram.
matplotlib.pyplot.hist() function itself provides many attributes with the help of which we can modify a histogram.The hist() function provide a patches object which gives access to the properties of the created objects, using this we can modify the plot according to our will.

Example 1:

 `import` `matplotlib.pyplot as plt` `import` `numpy as np` `from` `matplotlib ``import` `colors` `from` `matplotlib.ticker ``import` `PercentFormatter` `# Creating dataset` `np.random.seed(``23685752``)` `N_points ``=` `10000` `n_bins ``=` `20` `# Creating distribution` `x ``=` `np.random.randn(N_points)` `y ``=` `.``8` `*``*` `x ``+` `np.random.randn(``10000``) ``+` `25` `# Creating histogram` `fig, axs ``=` `plt.subplots(``1``, ``1``,` `                        ``figsize ``=``(``10``, ``7``),` `                        ``tight_layout ``=` `True``)` `axs.hist(x, bins ``=` `n_bins)` `# Show plot` `plt.show()`

Output :

Example 2: The code below modifies the above histogram for a better view and accurate readings.

 `import` `matplotlib.pyplot as plt` `import` `numpy as np` `from` `matplotlib ``import` `colors` `from` `matplotlib.ticker ``import` `PercentFormatter` `# Creating dataset` `np.random.seed(``23685752``)` `N_points ``=` `10000` `n_bins ``=` `20` `# Creating distribution` `x ``=` `np.random.randn(N_points)` `y ``=` `.``8` `*``*` `x ``+` `np.random.randn(``10000``) ``+` `25` `legend ``=` `[``'distribution'``]` `# Creating histogram` `fig, axs ``=` `plt.subplots(``1``, ``1``,` `                        ``figsize ``=``(``10``, ``7``),` `                        ``tight_layout ``=` `True``)` `# Remove axes splines` `for` `s ``in` `[``'top'``, ``'bottom'``, ``'left'``, ``'right'``]:` `    ``axs.spines[s].set_visible(``False``)` `# Remove x, y ticks` `axs.xaxis.set_ticks_position(``'none'``)` `axs.yaxis.set_ticks_position(``'none'``)` `  ` `# Add padding between axes and labels` `axs.xaxis.set_tick_params(pad ``=` `5``)` `axs.yaxis.set_tick_params(pad ``=` `10``)` `# Add x, y gridlines` `axs.grid(b ``=` `True``, color ``=``'grey'``,` `        ``linestyle ``=``'-.'``, linewidth ``=` `0.5``,` `        ``alpha ``=` `0.6``)` `# Add Text watermark` `fig.text(``0.9``, ``0.15``, ``'Jeeteshgavande30'``,` `         ``fontsize ``=` `12``,` `         ``color ``=``'red'``,` `         ``ha ``=``'right'``,` `         ``va ``=``'bottom'``,` `         ``alpha ``=` `0.7``)` `# Creating histogram` `N, bins, patches ``=` `axs.hist(x, bins ``=` `n_bins)` `# Setting color` `fracs ``=` `((N``*``*``(``1` `/` `5``)) ``/` `N.``max``())` `norm ``=` `colors.Normalize(fracs.``min``(), fracs.``max``())` `for` `thisfrac, thispatch ``in` `zip``(fracs, patches):` `    ``color ``=` `plt.cm.viridis(norm(thisfrac))` `    ``thispatch.set_facecolor(color)` `# Adding extra features   ` `plt.xlabel(``"X-axis"``)` `plt.ylabel(``"y-axis"``)` `plt.legend(legend)` `plt.title(``'Customized histogram'``)` `# Show plot` `plt.show()`

Output :

Last Updated on October 28, 2021 by admin

## Python DocstringsPython Docstrings

Python Docstrings Python documentation strings (or docstrings) provide a convenient way of associating documentation with

## Ways to increment Iterator from inside the For loop in PythonWays to increment Iterator from inside the For loop in Python

Ways to  Iterator from inside the For loop in Python For loops, in general, are

## Creating a Pandas Series from ListsCreating a Pandas Series from Lists

Creating a Pandas Series from Lists   A Series is a one-dimensional labeled array capable

## Iterate over a list in PythonIterate over a list in Python

List is equivalent to arrays in other languages, with the extra benefit of being dynamic

## Python – Converting all strings in list to integersPython – Converting all strings in list to integers

Python – Converting all strings in list to integers Interconversion between data types is facilitated

## Python – Display text to PyGame windowPython – Display text to PyGame window

Python | Display text to PyGame window Pygame is a cross-platform set of Python modules designed

## sort() in Pythonsort() in Python

Python sort() method Python sort function can be used to sort the list in both

## How to Install Python Pandas on Windows and Linux?How to Install Python Pandas on Windows and Linux?

Install Python Pandas on Windows and Linux   Pandas in Python is a package that