Skip to main content

UK parliament demographics chart

I was wondering recently how the ages of MPs in the House of Commons compared to the UK population as a whole, so I spent a while making these charts with D3.js.

Inevitable scope creep means that you can see the gender balance and number of people per age band for not only all MPs, and the UK adult population, but all members of the House of Lords, and also broken down for political parties within both houses.

The charts themselves are better, but here are some screenshots. There’s more about how I made the charts below.

Screen shot of charts
All MPs compared to all members of the Lords
Screen shot of charts
Adult UK population compared to all members of the Lords
Screen shot of charts
Labour MPs compared to Conservative MPs


The code for all this is on GitHub.

I started by finding sources for the data. Via the mnis python library I found, and made sense of, Parliament’s Members Names Information Service. I then wrote some python to grab details of all MPs and Lords members, along with their genders and birth dates, saving that (individual JSON files for the Commons and the Lords).

I then looked for breakdowns of the UK population’s age and gender, settling on this report from the Office for National Statistics, based on data from 2016. That provided the number of male and female people for every age (I wonder if/when they’ll start using at least an “other” gender?). Putting that into a spreadsheet I totalled up the numbers for the age bands I’d settled on (ten-year bands felt about right), and the total for each gender. I then manually created a JSON file containing that data.

The same python code then aggregates the data about individual parliamentarians, into the age bands and genders, and for the entire Houses and the parties within them, and combines that with the UK population data into a single JSON file that’s useful for the JavaScript to handle.

For generating the charts I used D3.js. I figured that, for the age comparisons, using a population pyramid chart would work well. Although they’re usually used for comparing the ages between genders, I hoped it would still make sense if I used each side to compare things other than gender (e.g. Labour MPs compared to Conservative MPs).

And then for the gender chart itself… I think the same format works OK, certainly in combination with the ages pyramid. If it was a standalone chart maybe it wouldn’t be ideal, but it seems fine in this context.

With previous D3.js work I’d settled on a pattern that enabled me to:

  1. Re-use the same charting code for multiple charts on the same page, using a pattern like this re-usable chart example.

  2. Make the charts responsive. By default D3.js charts stay the same size once generated so, to make them responsive, their dimensions, scales, axes, bars, etc. all need to be adjusted whenever the browser window changes.

This time I also needed to make the charts updatable, i.e. the data they display can be changed after they’ve rendered. Finding this post by Rob Moore saved me a bit of time by showing how to slightly adjust the re-usable chart pattern to allow data to be passed in after creation. Here’s the chart JavaScript.

Once that was working, some separate JavaScript was needed to generate the select input fields from the categories available in the JSON data, and to pass the relevant data to the charting code whenever new categories were selected.


As ever, I’ve spent much longer doing this than is really warranted to scratch the itch of “I wonder how the ages of MPs compares to the rest of the population?” Around 58% of MPs are 50 or older compared to around 47% of the UK adult population. The contrast is greater when comparing the Lords.

I don’t know if this is a useful comparison. For example, I can imagine an argument saying parliamentarians’ ages be compared with people who have some level of managerial responsibility, rather than all adults. On the other hand, if Parliament represents the people, you could say they need more people under 30 and over 70.

I did also try using the entire UK population in these charts, instead of only those aged 18-and-over, but that didn’t seem a useful comparison, seeing that one must be at least 18 to be an MP.

It’s interesting that all of the Lords’ members are over 40. On the one hand that seems terrible, if they’re supposed to be representing everyone, and should have a clue about modern life. On the other, given all of the Lords’ other terrible un-democratic-ness (Bishops! Hereditary peers! etc!), this seems a minor problem, especially when their role is often to act as a moderating force. I doubt many younger people would agree with that idea though, probably rightly.

I don’t have much to say about gender balance other than, obviously, some parties are doing better than others (only 21% of Conservative MPs are women).

That’s about it. Have a fiddle with the charts.

Mention this post

If you’ve mentioned this post somewhere, enter the URL here to let me know: