Which of the following is correct syntax to plot Histogram? (assume 'df' to be an object of a DataFrame).
(A) df.plot(kind='hist',bins=20)
(B) df.plot(kind='hist',bins=[18,19,20,21])
(C) df.plot(kind='hist',bins=range(20,30))
(D) df.plot(kind='hist')
Which of the following is correct syntax to plot Histogram? (assume 'df' to be an object of a DataFrame).
(A) df.plot(kind='hist',bins=20)
(B) df.plot(kind='hist',bins=[18,19,20,21])
(C) df.plot(kind='hist',bins=range(20,30))
(D) df.plot(kind='hist')
Solution
All four statements are valid. In pandas, df.plot(kind='hist') draws a histogram, and the optional bins argument controls the bin layout:
(A) bins=20 is an integer, so the data is split into 20 equal-width bins.
(B) bins=[18,19,20,21] is a list of bin edges, so the bins are 18-19, 19-20 and 20-21.
(C) bins=range(20,30) is also a sequence of edges, so the bins are 20-21, 21-22 and so on up to 29.
(D) With no bins given, pandas uses the default of 10 bins.
Options 1, 2 and 4: each leaves out one valid statement. The usual mistakes are thinking bins must be a single integer (which wrongly rules out B and C) or that bins cannot be left out (which wrongly rules out D).
Related questions:
2025: 13 May Shift 2
2025: 14 May Shift 1
2025: 15 May Shift 2
2026: 18 May Shift 2