application_vnd.jupyter.stderr_output_2.png application_vnd.jupyter.stderr_output_1.png
from plotly.offline import iplot, init_notebook_mode

import numpy as np

import numpy as np # linear algebra

import pandas as pd

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

import plotly.graph_objs as go

import numpy as np
import pandas as pd
pd.options.mode.chained_assignment = None
import plotly.plotly as py
import plotly.graph_objs as go
from plotly import tools
from plotly.offline import iplot, init_notebook_mode
init_notebook_mode(connected=True)
terror_data = pd.read_csv('../input/globalterrorismdb_0616dist.csv', encoding='ISO-8859-1', usecols=[0, 1, 2, 3, 8, 11, 13, 14, 35, 84, 100, 103])
terror_data = terror_data.rename(columns={'eventid': 'id', 'iyear': 'year', 'imonth': 'month', 'iday': 'day', 'country_txt': 'country', 'provstate': 'state', 'targtype1_txt': 'target', 'weaptype1_txt': 'weapon', 'nkill': 'fatalities', 'nwound': 'injuries'})
terror_data['fatalities'] = terror_data['fatalities'].fillna(0).astype(int)
terror_data['injuries'] = terror_data['injuries'].fillna(0).astype(int)
attacks_france = terror_data[terror_data.country == 'France']

terror_peryear = np.asarray(attacks_france.groupby('year').year.count())
terror_years = np.arange(1972, 2016)
terror_years = np.delete(terror_years, [23])
trace = [go.Scatter(x=terror_years, y=terror_peryear, mode='lines', line=dict(color='rgb(240, 140, 45)', width=3))]
layout = go.Layout(title='Terrorist Attacks by Year in France (1970-2015)', xaxis=dict(rangeslider=dict(thickness=0.05), showline=True, showgrid=False), yaxis=dict(range=[0.1, 425], showline=True, showgrid=False))
figure = dict(data=trace, layout=layout)

attacks_france['text'] = attacks_france['date'].dt.strftime('%B %-d, %Y') + '<br>' + attacks_france['fatalities'].astype(str) + ' Killed, ' + attacks_france['injuries'].astype(str) + ' Injured'
fatality = dict(type='scattergeo', locationmode='USA-states', lon=attacks_france[attacks_france.fatalities > 0]['longitude'], lat=attacks_france[attacks_france.fatalities > 0]['latitude'], text=attacks_france[attacks_france.fatalities > 0]['text'], mode='markers', name='Fatalities', hoverinfo='text+name', marker=dict(size=attacks_france[attacks_france.fatalities > 0]['fatalities'] ** 0.255 * 8, opacity=0.95, color='rgb(240, 140, 45)'))
injury = dict(type='scattergeo', locationmode='USA-states', lon=attacks_france[attacks_france.fatalities == 0]['longitude'], lat=attacks_france[attacks_france.fatalities == 0]['latitude'], text=attacks_france[attacks_france.fatalities == 0]['text'], mode='markers', name='Injuries', hoverinfo='text+name', marker=dict(size=(attacks_france[attacks_france.fatalities == 0]['injuries'] + 1) ** 0.245 * 8, opacity=0.85, color='rgb(20, 150, 187)'))
layout = dict(title='Terrorist Attacks by Latitude/Longitude in France (1970-2015)', showlegend=True, legend=dict(x=0.85, y=0.4), geo=dict(scope='europe', projection=dict(type='albers usa'), showland=True, landcolor='rgb(250, 250, 250)', subunitwidth=1, subunitcolor='rgb(217, 217, 217)', countrywidth=1, countrycolor='rgb(217, 217, 217)', showlakes=True, lakecolor='rgb(255, 255, 255)'))
data = [fatality, injury]
figure = dict(data=data, layout=layout)

target_codes = []
for attack in attacks_france['target'].values:
    if attack in ['Business', 'Journalists & Media', 'NGO']:
        target_codes.append(1)
    elif attack in ['Government (General)', 'Government (Diplomatic)']:
        target_codes.append(2)
    elif attack == 'Abortion Related':
        target_codes.append(4)
    elif attack == 'Educational Institution':
        target_codes.append(5)
    elif attack == 'Police':
        target_codes.append(6)
    elif attack == 'Military':
        target_codes.append(7)
    elif attack == 'Religious Figures/Institutions':
        target_codes.append(8)
    elif attack in ['Airports & Aircraft', 'Maritime', 'Transportation']:
        target_codes.append(9)
    elif attack in ['Food or Water Supply', 'Telecommunication', 'Utilities']:
        target_codes.append(10)
    else:
        target_codes.append(3)
attacks_france['target'] = target_codes
target_categories = ['Business', 'Government', 'Individuals', 'Healthcare', 'Education', 'Police', 'Military', 'Religion', 'Transportation', 'Infrastructure']
target_count = np.asarray(attacks_france.groupby('target').target.count())
target_percent = np.round(target_count / sum(target_count) * 100, 2)
target_fatality = np.asarray(attacks_france.groupby('target')['fatalities'].sum())
target_yaxis = np.asarray([1.33, 2.36, 2.98, 0.81, 1.25, 1.71, 1.31, 1.53, 1.34, 0])
target_injury = np.asarray(attacks_france.groupby('target')['injuries'].sum())
target_xaxis = np.log10(target_injury)
target_text = []
for i in range(0, 10):
    target_text.append(target_categories[i] + ' (' + target_percent[i].astype(str) + '%)<br>' + target_fatality[i].astype(str) + ' Killed, ' + target_injury[i].astype(str) + ' Injured')
data = [go.Scatter(x=target_injury, y=target_fatality, text=target_text, mode='markers', hoverinfo='text', marker=dict(size=target_count / 6.5, opacity=0.9, color='rgb(240, 140, 45)'))]
layout = go.Layout(title='Terrorist Attacks by Target in France (1970-2015)', xaxis=dict(title='Injuries', type='log', range=[1.36, 3.25], tickmode='auto', nticks=2, showline=True, showgrid=False), yaxis=dict(title='Fatalities', type='log', range=[0.59, 3.45], tickmode='auto', nticks=4, showline=True, showgrid=False))
annotations = []
for i in range(0, 10):
    annotations.append(dict(x=target_xaxis[i], y=target_yaxis[i], xanchor='middle', yanchor='top', text=target_categories[i], showarrow=False))
layout['annotations'] = annotations
figure = dict(data=data, layout=layout)
iplot(figure)