Python | Histogram vs Box Plot using Matplotlib

In this article, we are going to compare histogram and box plot for data visualization?
Submitted by Anuj Singh, on August 08, 2020

Both box plot and histogram are used for data visualization and analyzing the central tendency in data. Therefore, we are comparing both so that we can find which is a good data visualization technique.

Example:

Python | Histogram vs Box Plot (1)

Python | Histogram vs Box Plot (2)

Python | Histogram vs Box Plot (3)

Python code for histogram vs box plot using matplotlib

import numpy as np
import matplotlib.pyplot as plt

# Generating Data
spread = np.random.rand(65) * 82
center = np.ones(36) * 50
flier_high = np.random.rand(12) * 100 + 100
flier_low = np.random.rand(10) * -100
data = np.concatenate((spread, center, flier_high, flier_low))

plt.figure()
plt.title('Horizontal Histogram')
plt.hist(data)
plt.ylabel('Variation')
plt.grid()

plt.figure()
plt.title('Horizontal Histogram')
plt.boxplot(data)
plt.ylabel('Variation')
plt.grid()

plt.figure()
plt.subplot(121)
plt.title('Horizontal Histogram')
plt.hist(data)
plt.ylabel('Variation')
plt.grid()

plt.subplot(122)
plt.title('Horizontal Histogram')
plt.boxplot(data)
plt.ylabel('Variation')
plt.grid()

Output:

Output is as Figure





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





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


© https://www.includehelp.com some rights reserved.