How to make Bubble Charts with matplotlib

Source: http://glowingpython.blogspot.de/2011/11/how-to-make-bubble-charts-with.html

from pylab import *
from scipy import *

# reading the data from a csv file
durl = 'http://datasets.flowingdata.com/crimeRatesByState2005.csv'
rdata = genfromtxt(durl,dtype='S8,f,f,f,f,f,f,f,i',delimiter=',')

rdata[0] = zeros(8) # cutting the label's titles
rdata[1] = zeros(8) # cutting the global statistics

x = []
y = []
color = []
area = []

for data in rdata:
 x.append(data[1]) # murder
 y.append(data[5]) # burglary
 color.append(data[6]) # larceny_theft 
 area.append(sqrt(data[8])) # population
 # plotting the first eigth letters of the state's name
 text(data[1], data[5], 
      data[0],size=11,horizontalalignment='center')

# making the scatter plot
sct = scatter(x, y, c=color, s=area, linewidths=2, edgecolor='w')
sct.set_alpha(0.75)

axis([0,11,200,1280])
xlabel('Murders per 100,000 population')
ylabel('Burglaries per 100,000 population')
show()

The following figure is the resulting bubble chart

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.

#####

dataset

state,murder,forcible_rape,robbery,aggravated_assault,burglary,larceny_theft,motor_vehicle_theft,population
United States,5.6,31.7,140.7,291.1,726.7,2286.3,416.7,295753151
Alabama,8.2,34.3,141.4,247.8,953.8,2650,288.3,4545049
Alaska,4.8,81.1,80.9,465.1,622.5,2599.1,391,669488
Arizona,7.5,33.8,144.4,327.4,948.4,2965.2,924.4,5974834
Arkansas,6.7,42.9,91.1,386.8,1084.6,2711.2,262.1,2776221
California,6.9,26,176.1,317.3,693.3,1916.5,712.8,35795255
Colorado,3.7,43.4,84.6,264.7,744.8,2735.2,559.5,4660780
Connecticut,2.9,20,113,138.6,437.1,1824.1,296.8,3477416
Delaware,4.4,44.7,154.8,428.2,688.9,2144,278.5,839906
District of Columbia,35.4,30.2,672.1,721.3,649.7,2694.9,1402.3,582049
Florida,5,37.1,169.4,496.6,926.3,2658.3,423.3,17783868
Georgia,6.2,23.6,154.8,264.3,931,2751.1,490.2,9097428
Hawaii,1.9,26.9,78.5,147.8,767.9,3308.4,716.4,1266117
Idaho,2.4,40.4,18.6,195.4,564.4,1931.7,201.8,1425862
Illinois,6,33.7,181.7,330.2,606.9,2164.8,308.6,12674452
Indiana,5.7,29.6,108.6,179.9,697.6,2412,346.7,6253120
Iowa,1.3,27.9,38.9,223.3,606.4,2042.7,184.6,2949450
Kansas,3.7,38.4,65.3,280,689.2,2758.1,339.6,2741771
Kentucky,4.6,34,88.4,139.8,634,1685.8,210.8,4182293
Louisiana,9.9,31.4,118,435.1,870.6,2494.5,318.1,4497691
Maine,1.4,24.7,24.4,61.7,478.5,1832.6,102,1311631
Maryland,9.9,22.6,256.7,413.8,641.4,2294.3,608.4,5582520
Massachusetts,2.7,27.1,119,308.1,541.1,1527.4,295.1,6453031
Michigan,6.1,51.3,131.8,362.9,696.8,1917.8,476.5,10090554
Minnesota,2.2,44,92,158.7,578.9,2226.9,278.2,5106560
Mississippi,7.3,39.3,82.3,149.4,919.7,2083.9,256.5,2900116
Missouri,6.9,28,124.1,366.4,738.3,2746.2,443.1,5806639
Montana,1.9,32.2,18.9,228.5,389.2,2543,210.7,934801
Nebraska,2.5,32.9,59.1,192.5,532.4,2574.3,316.5,1751721
Nevada,8.5,42.1,194.7,361.5,972.4,2153.9,1115.2,2408804
New Hampshire,1.4,30.9,27.4,72.3,317,1377.3,102.1,1301415
New Jersey,4.8,13.9,151.6,184.4,447.1,1568.4,317.5,8621837
New Mexico,7.4,54.1,98.7,541.9,1093.9,2639.9,414.5,1916538
New York,4.5,18.9,182.7,239.7,353.3,1569.6,185.6,19330891
North Carolina,6.7,26.5,145.5,289.4,1201.1,2546.2,327.8,8669452
North Dakota,1.1,24.2,7.4,65.5,311.9,1500.3,166,635365
Ohio,5.1,39.8,163.1,143.4,872.8,2429,360.9,11475262
Oklahoma,5.3,41.7,91,370.5,1006,2644.2,391.8,3532769
Oregon,2.2,34.8,68.1,181.8,758.6,3112.2,529,3617869
Pennsylvania,6.1,28.9,154.6,235,451.6,1729.1,236.5,12418161
Rhode Island,3.2,29.8,72.1,146.1,494.2,1816,408.7,1064989
South Carolina,7.4,42.5,132.1,579,1000.9,2954.1,384.4,4256199
South Dakota,2.3,46.7,18.6,108.1,324.4,1343.7,108.4,780084
Tennessee,7.2,36.4,167.3,541.9,1026.9,2828.1,420.6,5995748
Texas,6.2,37.2,156.6,329.8,961.6,2961.7,408.7,22801920
Utah,2.3,37.3,44.3,143.4,606.2,2918.8,343.9,2499637
Vermont,1.3,23.3,11.7,83.5,491.8,1686.1,102.9,618814
Virginia,6.1,22.7,99.2,154.8,392.1,2035,211.1,7563887
Washington,3.3,44.7,92.1,205.8,959.7,3149.5,783.9,6261282
West Virginia,4.4,17.7,44.6,206.1,621.2,1794,210,1803920
Wisconsin,3.5,20.6,82.2,135.2,440.8,1992.8,226.6,5541443
Wyoming,2.7,24,15.3,188.1,476.3,2533.9,145.1,506242

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s