Introduction
A histogram helps us see how data is spread out by showing how many values fall into different groups.
A histogram helps us see how data is spread out by showing how many values fall into different groups.
plt.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.
plt.hist([1, 2, 2, 3, 3, 3, 4])
plt.hist(data, bins=5)plt.hist(data, bins=4, color='skyblue', edgecolor='black')
This code shows how ages are spread in 5 groups. The bars show how many people fall in each age group.
import matplotlib.pyplot as plt # Sample data: ages of 15 people ages = [22, 25, 27, 30, 22, 24, 29, 31, 35, 30, 28, 26, 27, 29, 30] # Create histogram with 5 bins plt.hist(ages, bins=5, color='lightgreen', edgecolor='black') # Add title and labels plt.title('Age Distribution') plt.xlabel('Age groups') plt.ylabel('Number of people') # Show the plot plt.show()
If you don't set bins, matplotlib chooses a default number.
Adding edgecolor helps see the bars clearly.
Histograms work best with numeric data.
A histogram groups data into bins and shows how many values fall in each.
Use plt.hist() with your data and choose bins to control grouping.
Colors and labels make the chart easier to understand.
plt.hist() function do in matplotlib?plt.hist() groups data into bins and shows how often values fall into each bin.data using matplotlib?plt.hist(), not plt.plot.hist() or plt.histogram().plt.show() displays the figure. Using plt.bar() creates bar charts, not histograms.import matplotlib.pyplot as plt values = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] plt.hist(values, bins=4) plt.show()
bins=4 divides the data range into 4 intervals (bins).import matplotlib.pyplot as plt values = [5, 7, 8, 5, 6] plt.hist(values, bins='five') plt.show()
bins argument must be an integer or sequence of numbers, not a string like 'five'.plt.show() is present, so no other errors.bins=5 automatically divides the range (0-100) into 5 equal-width bins of 20 units each: [0-20, 20-40, 40-60, 60-80, 80-100].color='blue' parameter sets the bar color correctly.Pass the test and this pattern moves unknown