Histograms – AICorr.com



Histograms

This page covers a matplotlib histograms tutorial.

A histogram is a graphical representation of the distribution of data, which consists of a series of adjacent bars. The height of each bar represents the frequency or count of data values within a specific interval. It is known as a “bin.”

Why use histograms?

Histograms are commonly used to visualise the distribution of continuous data. Distribution such as measurements or observations from a dataset. They provide insights into the central tendency, spread, and shape of the data distribution. Exploration of a histogram can help with identification of patterns, outliers, and other characteristics of the dataset.

Furthermore, histograms are particularly useful in fields such as statistics, data analysis, and data visualisation. They offer a visual summary of the underlying data distribution. As a result, this makes it easier to interpret and draw conclusions.

Implementation

The coding building of a histogram is quite easy. The library implements it through its method hist().

For example, let’s create a basic histogram.

import matplotlib.pyplot as plt

# Sample data
data = [4, 1, 2, 3, 5, 2, 9, 3, 1, 2, 2, 8, 9, 1, 7, 8]

# Histogram
plt.hist(data)

# labels and title
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.title('Histogram of Random Data')

# Show the plot
plt.show()
matplotlib-histogram

Histograms often apply to statistical data distributions. For instance, we can create a sample random distribution of data with the help of NumPy and display it as a histogram. For more information about NumPy, please refer to these tutorials here.

Below is the coding implementation.

import matplotlib.pyplot as plt
import numpy as np

# Random data
data = np.random.randn(1000)

# Histogram
plt.hist(data)

# labels and title
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.title('Histogram of Random Data')

# Show the plot
plt.show()
matplotlib-histogram-random-data-numpy

Now, let’s customise the histogram further. In this instance, we change the colour of the bars, colour of the bar edges, as well as the number of bins. Bar colour uses the method color, for bar edge is edgecolor, and for the number of bins is bins.

The default value for bins is 10. Bins define equally the width of the bins within the range of, either the number of bins (if integer values) or the number of bin edges (if sequence values).

import matplotlib.pyplot as plt
import numpy as np

# Random data
data = np.random.randn(1000)

# Histogram
plt.hist(data, bins=50, color="green", edgecolor="r")

# labels and title
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.title('Histogram of Random Data')

# Show the plot
plt.show()
matplotlib-histogram-customise-color-edgecolor-bins

This is an original matplotlib histograms tutorial. Created by aicorr.com.

Next: Pie Charts

We will be happy to hear your thoughts

Leave a reply

0
Your Cart is empty!

It looks like you haven't added any items to your cart yet.

Browse Products
Powered by Caddy
Shopping cart