Ch17 Figure4


In [1]:
# Let's say the study comes out that says that 20% of the texts you receive come from people who are driving.

data = []
settings = ['driving', 'working', 'before sleep', 'dining', 'studying', 'walking', 'watching TV', 'waiting for bus', 'shopping']

for i in range(5000):
    
    if rd.random() <= .2:
        setting = 'driving'
    elif rd.random() <= .5:
        setting = settings[rd.randint(1,len(settings)-4)]
    else:
        setting = settings[rd.randint(len(settings)-4, len(settings)-1)]
    data.append([i, setting])

df = pd.DataFrame(data, columns=['id', 'setting'])
# df.to_csv('csv_output/ch17_fig4.csv', index=False)
df = pd.read_csv('csv_output/ch17_fig4.csv')
df.head()


Out[1]:
id setting
0 0 studying
1 1 watching TV
2 2 shopping
3 3 waiting for bus
4 4 working

In [2]:
df = pd.read_csv('csv_output/ch17_fig4.csv')

%matplotlib inline
sns.set_style("white")
cm = sns.color_palette('Blues', 1)

f, ax = plt.subplots(1,1, figsize=(8,8))

dgb = df.groupby('setting').id.count().reset_index().sort_values('id', ascending=False)
ax.bar(left= np.arange(dgb.shape[0]), height=dgb.id/5000, color=cm[0]);

ax.set_title('Things you do when you text')
ax.set_xticks(np.arange(dgb.shape[0])+.2);
ax.set_xticklabels(dgb.setting, rotation=45);
ax.set_yticklabels(['%i%%'%(x*100) for x in np.arange(0,.25,.05)])
ax.set_ylabel('percentage');

f.savefig('svg_output/ch17_fig4.svg', format='svg')


Based on the study, almost 20% of people texting while driving, the second highest activity when texting is walking.