📖 Scenario: You have collected the ages of a group of people at a community event. You want to see how these ages are spread out to understand the age distribution.
🎯 Goal: Build a simple histogram using plt.hist to visualize the distribution of ages.
📋 What You'll Learn
Create a list called ages with the exact values: 22, 25, 29, 22, 24, 30, 27, 25, 29, 31
Create a variable called bins and set it to 5
Use plt.hist with ages and bins to create the histogram
Use plt.show() to display the histogram
💡 Why This Matters
🌍 Real World
Histograms help us understand how data like ages, heights, or test scores are spread out in a group.
💼 Career
Data scientists use histograms to quickly see patterns and distributions in data, which helps in making decisions or building models.
Progress0 / 4 steps
1
Create the data list
Create a list called ages with these exact values: 22, 25, 29, 22, 24, 30, 27, 25, 29, 31
Matplotlib
Hint
Use square brackets [] to create a list and separate numbers with commas.
2
Set the number of bins
Create a variable called bins and set it to 5
Matplotlib
Hint
Just assign the number 5 to the variable bins.
3
Create the histogram
Import matplotlib.pyplot as plt and use plt.hist with ages and bins to create the histogram
Matplotlib
Hint
Use import matplotlib.pyplot as plt to import. Then call plt.hist(ages, bins=bins).
4
Display the histogram
Use plt.show() to display the histogram
Matplotlib
Hint
Call plt.show() after creating the histogram to see the plot.
Practice
(1/5)
1. What does the plt.hist() function do in matplotlib?
easy
A. It creates a scatter plot of two variables.
B. It plots a line graph connecting data points.
C. It creates a histogram to show data frequency distribution.
D. It generates a pie chart to show proportions.
Solution
Step 1: Understand the purpose of plt.hist()
The function plt.hist() groups data into bins and shows how often values fall into each bin.
Step 2: Compare with other plot types
Line graphs connect points, scatter plots show points, and pie charts show parts of a whole, which are different from histograms.
Final Answer:
It creates a histogram to show data frequency distribution. -> Option C
Quick Check:
Histogram = frequency bars [OK]
Hint: Histograms show frequency counts in bars [OK]
Common Mistakes:
Confusing histogram with line plot
Thinking plt.hist() creates scatter plots
Mixing histogram with pie chart
2. Which of the following is the correct way to create a histogram of data stored in a list named data using matplotlib?
easy
A. plt.histogram(data); plt.show()
B. plt.plot.hist(data); plt.show()
C. plt.bar(data); plt.show()
D. plt.hist(data); plt.show()
Solution
Step 1: Recall the correct function name
The correct function to create a histogram is plt.hist(), not plt.plot.hist() or plt.histogram().
Step 2: Confirm usage with plt.show()
After plotting, plt.show() displays the figure. Using plt.bar() creates bar charts, not histograms.
Final Answer:
plt.hist(data); plt.show() -> Option D
Quick Check:
Use plt.hist() for histograms [OK]
Hint: Use plt.hist() to plot histograms, then plt.show() [OK]
A. plt.show() is missing and needed to display the plot.
B. The bins parameter should be an integer, not a string.
C. plt.hist() requires a color argument.
D. The values list contains invalid data types.
Solution
Step 1: Check bins parameter type
The bins argument must be an integer or sequence of numbers, not a string like 'five'.
Step 2: Verify other parts
The data list is valid, color is optional, and plt.show() is present, so no other errors.
Final Answer:
The bins parameter should be an integer, not a string. -> Option B
Quick Check:
Bins must be int or list, not string [OK]
Hint: Bins must be number, not text [OK]
Common Mistakes:
Passing bins as a string instead of integer
Thinking color is required
Forgetting plt.show() (not the case here)
5. You have a dataset of exam scores ranging from 0 to 100. You want to create a histogram with 5 equal-width bins and color the bars blue. Which code snippet correctly achieves this?
hard
A. plt.hist(scores, bins=5, color='blue'); plt.show()
B. plt.hist(scores, bins=[0,20,50,70,90,100], color='blue'); plt.show()
C. plt.hist(scores, bins=5, colors='blue'); plt.show()
D. plt.hist(scores, bins=5, color=blue); plt.show()
Solution
Step 1: Use bins=integer for equal-width
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].
Step 2: Color the bars blue
The color='blue' parameter sets the bar color correctly.
Step 3: Eliminate distractors
B specifies explicit bins [0,20,50,70,90,100] with unequal widths (20,30,20,20,10); C uses invalid 'colors'; D has unquoted 'blue' causing NameError.
Final Answer:
plt.hist(scores, bins=5, color='blue'); plt.show() -> Option A
Quick Check:
bins=5 (equal width) + color='blue' [OK]
Hint: bins=5 for equal-width over data range, color='blue' [OK]
Common Mistakes:
Using explicit bins with unequal widths
Using 'colors' instead of 'color'
Using color=blue without quotes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.