Introduction
Histograms help us see how data spreads out. They show how often values appear in groups.
Histograms help us see how data spreads out. They show how often values appear in groups.
matplotlib.pyplot.hist(data, bins=number_of_bins, color='color_name', edgecolor='color_name')
data is the list or array of numbers you want to plot.
bins controls how many groups the data is split into.
import matplotlib.pyplot as plt values = [1, 2, 2, 3, 3, 3, 4, 4, 5] plt.hist(values, bins=5) plt.show()
plt.hist(values, bins=3, color='skyblue', edgecolor='black') plt.show()
This program shows how ages are spread across groups. The histogram bars show how many people fall into each age range.
import matplotlib.pyplot as plt # Sample data: ages of 20 people ages = [22, 25, 27, 30, 22, 24, 29, 31, 35, 40, 42, 45, 50, 55, 60, 65, 70, 75, 80, 85] # Create histogram with 8 bins plt.hist(ages, bins=8, color='green', edgecolor='black') plt.title('Age Distribution') plt.xlabel('Age groups') plt.ylabel('Number of people') plt.show()
More bins give detailed view but can be noisy.
Fewer bins give a simpler overview but may hide details.
Histograms show counts, not exact values.
Histograms group data into bins to show frequency.
They help us understand data spread and common values.
Adjusting bins changes the detail level of the distribution.
data?plt.hist().plt.hist(data), others create different plots.import matplotlib.pyplot as plt import numpy as np data = np.array([1, 2, 2, 3, 3, 3, 4, 4, 4, 4]) plt.hist(data, bins=4) plt.show()
import matplotlib.pyplot as plt data = [1, 2, 2, 3, 3, 3] plt.hist(data, bins='five') plt.show()
plt.hist() best balances detail and clarity?Pass the test and this pattern moves unknown