"Body mass index (BMI) is a simple index of weight-for-height that is commonly used to classify overweight and obesity in adults. It is defined as a person's weight in kilograms divided by the square of his height in meters (kg/m2)." "A BMI greater than or equal to 25 is overweight", and "a BMI greater than or equal to 30 is obesity" (WHO 2015)
%load_ext watermark
%watermark -a 'Hideki Tanaka (@atelierhide)' -v -p numpy,pandas,matplotlib,mpld3,swapi -d
Hideki Tanaka (@atelierhide) 25/02/2015 CPython 2.7.9 IPython 2.4.1 numpy 1.9.1 pandas 0.15.2 matplotlib 1.4.3 mpld3 0.2 swapi 0.1.3
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import mpld3
import swapi
plt.style.use('ggplot')
# Get data with swapi-python
people = swapi.get_all('people')
data = {'name': [], 'mass': [], 'height': [], 'bmi': []}
for person in people.iter():
if person.mass != 'unknown' and person.height != 'unknown':
m = float(person.mass.replace(',', ''))
h = float(person.height.replace(',', ''))
data['name'].append(person.name)
data['mass'].append(m)
data['height'].append(h)
data['bmi'].append(int(m/(h*0.01)**2))
pd.DataFrame(data).head(10)
bmi | height | mass | name | |
---|---|---|---|---|
0 | 26 | 172 | 77 | Luke Skywalker |
1 | 26 | 167 | 75 | C-3PO |
2 | 34 | 96 | 32 | R2-D2 |
3 | 33 | 202 | 136 | Darth Vader |
4 | 21 | 150 | 49 | Leia Organa |
5 | 37 | 178 | 120 | Owen Lars |
6 | 27 | 165 | 75 | Beru Whitesun lars |
7 | 34 | 97 | 32 | R5-D4 |
8 | 25 | 183 | 84 | Biggs Darklighter |
9 | 23 | 182 | 77 | Obi-Wan Kenobi |
mpld3.enable_notebook()
fig, ax = plt.subplots(figsize=(9, 6))
cmap = plt.cm.jet
bounds = np.linspace(0, 50, 11)
norm = colors.BoundaryNorm(bounds, cmap.N)
sc = ax.scatter(data['mass'], data['height'], s=np.array(data['bmi'])**2, c=data['bmi'],
cmap=cmap, norm=norm, linewidth=1, alpha=0.8)
ax.set_xlim(0, 180)
ax.set_ylim(50, 250)
ax.set_title('BMI (kg/m2) for Star Wars Characters')
ax.set_xlabel('Mass (kg)', fontsize=15)
ax.set_ylabel('Height (cm)', fontsize=15)
ax.xaxis.set_label_coords(0.5, -0.08)
ax.yaxis.set_label_coords(-0.08, 0.5)
labels = [data['name'][i] + ': BMI=' + str(data['bmi'][i]) for i in range(len(data['name']))]
tooltip = mpld3.plugins.PointLabelTooltip(sc, labels=labels)
mpld3.plugins.connect(fig, tooltip)