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)

## Comparation between victims by age // ToDo adjust plot
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

# Plotting the bars
fig, ax = plt.subplots(figsize=(25,15))

# in position pos,
plt.bar(pos,
        #using homicides['Adult'] data,
        homicides['Adult'],
        # of width
        width,
        # with alpha 0.5
        alpha=0.5,
        # with color
        color='#EE3224',
        # with label the first value in year
        label=homicides.index[0])

# Create a bar with young data,
# in position pos + some width buffer,
plt.bar([p + width for p in pos],
        #using homicides['Young'] data,
        homicides['Young'],
        # of width
        width,
        # with alpha 0.5
        alpha=0.5,
        # with color
        color='#F78F1E',
        # with label the second value in year
        label=homicides.index[1])

# Set the y axis label
ax.set_ylabel('Adult / Young')

# Set the chart's title
ax.set_title('Comparation between victims by age')

# Set the position of the x ticks
ax.set_xticks([p + 1.5 * width for p in pos])

# Set the labels for the x ticks
ax.set_xticklabels(homicides.index)

# Setting the x-axis and y-axis limits
plt.xlim(min(pos)-width, max(pos)+width*4)
plt.ylim([0, max(homicides['Adult'] + homicides['Young'])] )

# Adding the legend and showing the plot
plt.legend(['Adult', 'Young'], loc='upper left')
plt.grid()
plt.show()

# Sex of the perpetrators
perpetrator_sex = pd.DataFrame(data, columns = ['Perpetrator Sex']) 
count_perpetrator_sex = perpetrator_sex.stack().value_counts()
ax = count_perpetrator_sex.plot(kind = 'pie',
                              title = 'Sex of the perpetrators',
                              startangle = 10,
                              autopct='%.2f')
ax.set_ylabel('')

#Crime types
crime_types = pd.DataFrame(data, columns = ['Crime Type']) 
count_types = crime_types.stack().value_counts()
count_crime_types = count_types.sort_index(axis=0, ascending=False)
#plot the total of homicides

ax = count_crime_types.plot(kind = 'pie',
                              title = 'Crime Types',
                              startangle = 25,
                              autopct='%.2f')
ax.set_ylabel('')

state = pd.DataFrame(data, columns=['State'])
count_states = state.stack().value_counts()
states = count_states.sort_index(axis=0, ascending=False)
print(states.plot(kind='barh', fontsize=10, width=0.5, figsize=(12, 10), title='Homicides in EEUU by State between 1980 and 2014'))