Home »
Python »
Python Data Visualization
Python | Histogram vs Box Plot using Matplotlib
Histogram vs Box Plot in Python: In this tutorial, we will learn to compare histogram and box plot for data visualization?
By Anuj Singh Last updated : August 18, 2023
Histogram Vs Box Plot using Matplotlib
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.
Examples
Python program 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