Arrange the following statements to draw a Histogram. Assume 'pd' and 'plt' are alias names for pandas and matplotlib.pyplot respectively.
(A) plt.show()
(B) df=pd.read_csv('abc.csv')
(C) plt.legend()
(D) df.plot(kind='hist',label='RECORDS')
Arrange the following statements to draw a Histogram. Assume 'pd' and 'plt' are alias names for pandas and matplotlib.pyplot respectively.
(A) plt.show()
(B) df=pd.read_csv('abc.csv')
(C) plt.legend()
(D) df.plot(kind='hist',label='RECORDS')
Solution
The data must be loaded first, then plotted, then the legend added, and finally the figure displayed.
(B) df=pd.read_csv('abc.csv') loads the data into a DataFrame.
(D) df.plot(kind='hist',label='RECORDS') draws the histogram and sets the label.
(C) plt.legend() shows the label set in the plot call, so it must come after (D).
(A) plt.show() renders the figure, so it comes last.
Option 3: plt.show() before the plot call would display nothing, and the legend would then have no plot to describe.
Option 4: the legend cannot be created before the data is loaded and plotted.
Related questions:
2025: 30 May Shift 2
2026: 29 May Shift 2
2025: 13 May Shift 2
2025: 22 May Shift 1
2026: 19 May Shift 1