text_plain_output_1.png image_output_1.png
import matplotlib.pyplot as plt

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

## Rate of crimes solved
solved = pd.DataFrame(data, columns = ['Crime Solved']) 
resolution = solved.stack().value_counts()
ax = resolution.plot(kind = 'pie',
                              title = 'Crimes solved between 1980 & 2014 (in %)',
                              startangle = 10,
                              autopct='%.2f')
ax.set_ylabel('')

#Gender of victims
sex = pd.DataFrame(data, columns = ['Victim Sex']) 
count_sex = sex.stack().value_counts()
ax = count_sex.plot(kind = 'pie',
                              title = 'Sex of the victims',
                              startangle = 10,
                              autopct='%.2f')
ax.set_ylabel('')

#Race of Victims
race = pd.DataFrame(data, columns = ['Victim Race']) 
count_race = race.stack().value_counts()
ax = count_race.plot(kind = 'pie',
                              title = 'Race of the victims',
                              startangle = 10,
                              autopct='%.2f',
                              explode=(0, 0, 0.7, 1, 1.3))
ax.set_ylabel('')

data['Victim Age'] = data['Victim Age'].astype('int')
mask = data['Victim Age'] < 21
young_victims = pd.DataFrame(data.loc[mask], columns=['Year'])
count_years = young_victims.stack().value_counts()
homicides_young = count_years.sort_index(axis=0, ascending=False)
mask2 = data['Victim Age'] > 21
adult_victims = pd.DataFrame(data.loc[mask2], columns=['Year'])
count_years = adult_victims.stack().value_counts()
homicides_adult = count_years.sort_index(axis=0, ascending=False)

homicides_adult.to_frame()
homicides_young.to_frame()
homicides = pd.DataFrame({'Adult': homicides_adult, 'Young': homicides_young})
homicides.sort_index(inplace=True)
pos = list(range(len(homicides['Adult'])))
width = 0.25
fig, ax = plt.subplots(figsize=(25, 15))
plt.bar(pos, homicides['Adult'], width, alpha=0.5, color='#EE3224', label=homicides.index[0])
plt.bar([p + width for p in pos], homicides['Young'], width, alpha=0.5, color='#F78F1E', label=homicides.index[1])
ax.set_ylabel('Adult / Young')
ax.set_title('Comparation between victims by age')
ax.set_xticks([p + 1.5 * width for p in pos])
ax.set_xticklabels(homicides.index)
plt.xlim(min(pos) - width, max(pos) + width * 4)
plt.ylim([0, max(homicides['Adult'] + homicides['Young'])])
plt.legend(['Adult', 'Young'], loc='upper left')
plt.grid()
plt.show()