Question:medium

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

Solution and Explanation

Step 1: Match each blank with its job:
Statement-1 must draw the histogram. Statement-2 must set the x label using the correct alias.
Statement-3 must show the legend. Statement-4 must display the figure.

Step 2: Start with the alias in Statement-2:
The first line is import matplotlib.pyplot as plt. After this line the name pyplot is not available, only plt.
So the blank in front of .xlabel is plt.

Step 3: Fill Statement-1:
Plots in pyplot have different functions. plot() draws lines, bar() draws bars and hist() draws a histogram.
The call has bins and Ages, which are the inputs of hist().
So the statement is plt.hist(Ages, bins=binsize, edgecolor='black', label='Participants').

Step 4: Fill Statement-3:
The word 'Participants' is stored as the label of the bars. It is drawn on the graph only after the legend function runs.
So Statement-3 is plt.legend().

Step 5: Fill Statement-4:
All the earlier lines only prepare the figure. plt.show() opens it on the screen, so Statement-4 is plt.show().

Step 6: The completed program:
import matplotlib.pyplot as plt
Ages = [22, 25, 24, 28, 30, 24, 29, 27, 21, 23]
binsize = [20, 22, 24, 26, 28, 30]
plt.hist(Ages, bins=binsize, edgecolor='black', label='Participants')
plt.xlabel('Age')
plt.ylabel('Number of Participants')
plt.title("Age Distribution")
plt.legend()
plt.show()

Counting the ages in each bin gives the heights 1, 2, 3, 1, 3 for the five bars.

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