Question:

Ms. Anjali, a data analyst, has been assigned the task of creating a histogram to display the age distribution of ten participants in a workshop. She has been provided with the following list of their ages:

Ages = [22, 25, 24, 28, 30, 24, 29, 27, 21, 23]

She has started writing a Python program for it. However, the code is incomplete. Help her complete the program by filling in the missing parts, so that the histogram as shown below is displayed.

import matplotlib.pyplot as plt
Ages = [22, 25, 24, 28, 30, 24, 29, 27, 21, 23]
binsize=[20,22,24,26,28,30]
plt.________(Ages, bins=binsize, edgecolor = 'black', label = 'Participants')  # Statement-1
______.xlabel('Age')  # Statement-2
plt.ylabel('Number of Participants')
plt.title("Age Distribution")
plt.________  # Statement-3
___________  # Statement-4

I. Write the suitable code for the blank space in the line marked as Statement-1 which plots the histogram.

II. Fill in the blank in Statement-2 to use the correct alias of the required module to set the label on x axis.

III. Fill in the blank in Statement-3 with the correct Python code to display the legend on the graph.

IV. Fill in the blank in Statetment-4 with the appropriate Python code to display the graph.

Show Hint

The module alias is plt. Use hist() to draw the histogram, legend() to show the label and show() to display the graph.
Updated On: Oct 20, 2026
Show Solution
collegedunia
Verified By Collegedunia

Solution and Explanation

Step 1: Read the program:
The program imports matplotlib.pyplot as plt. The data is the list Ages, and the bin edges are 20, 22, 24, 26, 28 and 30.
The four blanks are the plot call, the module alias, the legend call and the show call.

Step 2: Statement-1 - plot the histogram:
A histogram in pyplot is drawn by the function hist(). The line already passes Ages, bins, edgecolor and label.
plt.hist(Ages, bins=binsize, edgecolor='black', label='Participants')

Answer: hist

Step 3: Statement-2 - label the x axis:
The module was imported with the alias plt. So the xlabel function must be called as plt.xlabel.
The line plt.ylabel in the next statement confirms this alias.
Answer: plt

Step 4: Statement-3 - show the legend:
The hist call has label='Participants'. A label appears on the graph only when legend() is called.
plt.legend()

Answer: legend()

Step 5: Statement-4 - display the graph:
The graph window opens only when show() is called. This is always the last statement of a plot program.
plt.show()

Answer: plt.show()

Step 6: Check the picture:
With these bins the ages fall like this: 20-22 has 1 value (21), 22-24 has 2 values (22 and 23), 24-26 has 3 values (24, 24, 25), 26-28 has 1 value (27) and 28-30 has 3 values (28, 29, 30).
The last bin includes its right edge 30. So the bar heights are 1, 2, 3, 1 and 3.

Final Answer:
I. hist
II. plt
III. legend()
IV. plt.show()
Was this answer helpful?
0
0