A collection of sloppy snippets for scientific computing and data visualization in Python.

Wednesday, November 23, 2011

How to make Bubble Charts with matplotlib

In this post we will see how to make a bubble chart using matplotlib. The snippet that we are going to see was inspired by a tutorial on flowingdata.com where R is used to make a bubble chart that represents some data extracted from a csv file about the crime rates of America by states. I used the dataset provided by flowingdata to create a similar chart with Python. Let's see the code:

It shows the number of burglaries versus the number of murders per 100,000 population. Every bubble is a state of America, the size of the bubbles represents the population of the state and the color is the number of larcenies.

if data[8] is population, don't you want area.append(data[8]) instead of the square root? It seems to me that each person in the population should represent a certain area and, therefore, the area of the circle would be proportional to the population, not sqrt(population).

Hi Shawn. Another place to start - I know I am late with this comment - is with the seaborn.scatterplot(). This is a super powerful tool that can make vary size and color by categorial or numeric value. It was developed quite recently.