Python | Dual Histogram Plot

Dual Histogram Plot in Python. Here, we are going to learn about the dual histogram plot and its Python implementation.
Submitted by Anuj Singh, on July 20, 2020

A histogram is a graphical technique or a type of data representation using bars of different heights such that each bar group's numbers into ranges (bins or buckets). Taller the bar higher the data falls in that bin. A Histogram is one of the most used techniques in data visualization and therefore, matplotlib has provided a function matplotlib.pyplot.hist() for plotting histograms. A dual histogram is a type of histogram in which we can plot a histogram of two data sets. It is often used for dual data comparison and classification.

The following example illustrates the implementation and use of the Dual Histogram Plot.

Python code for dual histogram plot

```import matplotlib.pyplot as plt
import numpy as np

# random data generation
mu, sigma = 100, 15
x = mu + sigma * np.random.randn(10000)

mu_w = 200
sigma_w = 10
w = np.random.normal(mu_w, sigma_w, size=100)

# Histogram of the Data
plt.figure()
plt.hist(x, density=True, histtype='barstacked', rwidth=1)
plt.hist(w, density=True, histtype='barstacked', rwidth=1)
plt.title('barstacked')
plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title('Dual Histogram')
plt.show()

plt.figure()
plt.hist(x, density=True, histtype='barstacked', rwidth=1)
plt.hist(w, bins=10, density=True, histtype='barstacked', rwidth=1)
plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title('Dual Histogram: Independence of each hist')
plt.show()

plt.figure()
plt.hist(x, bins=25, color='pink', density=True, histtype='barstacked', rwidth=1)
plt.hist(w, bins=10, color='purple', alpha=0.6, density=True, histtype='barstacked', rwidth=1)
plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title('Dual Histogram: Bins and Colour Change')
plt.show()
```

Output:

```Output is as figure
```

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates