Plotting Data using Matplotlib
Histograms and open data
Draw and customise histograms, including a frequency polygon.
A histogram shows how data are distributed. The range of values is split into intervals called bins; each column covers one bin, and its height shows how many values fall in it.
- df.plot(kind='hist') chooses the bins itself;
- bins=20 asks for 20 equal bins;
- bins=[0, 10, 20, 30] sets your own bin edges.
Histograms can be customised with edgecolor, linewidth, linestyle, fill (True or False) and hatch (a pattern inside each bar, like '/' or 'o').
Where can you find real data to practise with? Open data is data that anyone can use freely. data.gov.in is India's Open Government Data Platform. With read_csv, the usecols= parameter reads only the columns you need.
NumPy's np.histogram() gives the frequencies and bin edges as numbers. Joining the midpoints of the bins at their heights draws a frequency polygon. Bins, then count.
Practise this lesson
Questions that get harder as you improve, a Tutor that helps without giving the answer away, and progress you can see. Free for every learner.
No account, no email address. Opens the real product.