In [1]:
import numpy 
import pandas as pd
from  matplotlib import pyplot
import seaborn as sns
import requests
from matplotlib.ticker import MaxNLocator
%matplotlib inline

sns.set(style='ticks')

In [18]:
#response = requests.get('http://secret-url', stream=True)

In [3]:
df  = pd.read_json(response.content.decode('utf-8'))

In [4]:
df.shape[0]


Out[4]:
917

In [5]:
df= df[df['format'].isin(['iron'])]

In [6]:
df.shape[0]


Out[6]:
447

In [7]:
df = df[['date','place','country']]

In [8]:
df.head()


Out[8]:
date place country
0 2002-10-19 worldchampionship USA
1 2003-10-18 worldchampionship USA
2 2004-03-01 newzealand NZL
3 2004-10-16 worldchampionship USA
4 2005-02-27 malaysia MYS

In [9]:
df['Year'] = df.apply(lambda x: x.date.year, axis=1)
df['Ironman'] = df['place']
df['Country'] = df['country']

In [10]:
df = df[df['Year'] >= 2005]

In [11]:
df.sample(1)


Out[11]:
date place country Year Ironman Country
415 2016-09-24 mallorca ESP 2016 mallorca ESP

In [12]:
df = df[['Year','Ironman','Country']]

In [13]:
df.Ironman[df.Ironman=='subicbayphilippines'] = 'Philippines'
df.Ironman[df.Ironman=='mardelplata'] = 'Mar del plata'
df.Ironman[df.Ironman=='emiliaromagna'] = 'Emilia romagna'
df.Ironman[df.Ironman=='santarosa'] = 'Santa rosa'
df.Ironman[df.Ironman=='northcarolina'] = 'North carolina'
df.Ironman[df.Ironman=='laketahoe'] = 'Lake Tahoe'
df.Ironman[df.Ironman=='loscabos'] = 'Los Cabos'
df.Ironman[df.Ironman=='monttremblant'] = 'Mont tremblant'
df.Ironman[df.Ironman=='coeurdalene'] = 'Coeur d alene'
df.Ironman[df.Ironman=='westernaustralia'] = 'Western Australia'
df.Ironman[df.Ironman=='southafrica'] = 'south africa'
df.Ironman[df.Ironman=='lakeplacid'] = 'lake placid'
df.Ironman[df.Ironman=='newzealand'] = 'New zealand'
df.Ironman[df.Ironman=='vitoriagasteiz'] = 'Vitoria Gasteiz'
df.Ironman[df.Ironman=='worldchampionship'] = 'Kona'


/usr/local/lib/python3.5/dist-packages/ipykernel_launcher.py:2: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  
/usr/local/lib/python3.5/dist-packages/ipykernel_launcher.py:3: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  This is separate from the ipykernel package so we can avoid doing imports until
/usr/local/lib/python3.5/dist-packages/ipykernel_launcher.py:4: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  after removing the cwd from sys.path.
/usr/local/lib/python3.5/dist-packages/ipykernel_launcher.py:5: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  """
/usr/local/lib/python3.5/dist-packages/ipykernel_launcher.py:6: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  
/usr/local/lib/python3.5/dist-packages/ipykernel_launcher.py:7: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  import sys
/usr/local/lib/python3.5/dist-packages/ipykernel_launcher.py:8: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  
/usr/local/lib/python3.5/dist-packages/ipykernel_launcher.py:9: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  if __name__ == '__main__':
/usr/local/lib/python3.5/dist-packages/ipykernel_launcher.py:10: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  # Remove the CWD from sys.path while we load stuff.
/usr/local/lib/python3.5/dist-packages/ipykernel_launcher.py:11: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  # This is added back by InteractiveShellApp.init_path()
/usr/local/lib/python3.5/dist-packages/ipykernel_launcher.py:12: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  if sys.path[0] == '':
/usr/local/lib/python3.5/dist-packages/ipykernel_launcher.py:13: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  del sys.path[0]
/usr/local/lib/python3.5/dist-packages/ipykernel_launcher.py:14: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  
/usr/local/lib/python3.5/dist-packages/ipykernel_launcher.py:15: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  from ipykernel import kernelapp as app

In [14]:
df.Ironman = df.Ironman.str.title() + ' (' + df['Country'] + ')'

In [15]:
df.head(1)


Out[15]:
Year Ironman Country
4 2005 Malaysia (MYS) MYS

In [16]:
#fg = seaborn.FacetGrid(data=df, hue='Place', hue_order= df.Place, aspect=1.61, size=3)
#sns.factorplot(x="Year", y="Place", hue="Place", col="Year", data=df, kind="swarm");


a4_dims = (5, 9)
fig, ax = pyplot.subplots(figsize=a4_dims)

#dict_colors = {"Tallinn":"blue", "Haugesund":"green"}

#ax.get_yticklabels()[0].set_color((0.2980392156862745, 0.4470588235294118, 0.6901960784313725))
#ax.get_yticklabels()[1].set_color((0.3333333333333333, 0.6588235294117647, 0.40784313725490196))
#ax.get_yticklabels()[2].set_color('red')

pal=sns.color_palette("deep", 100)

sns.stripplot(x="Year", y="Ironman", ax=ax, data=df, palette=pal);
ax.invert_yaxis()
# force ticks being displayed as integer values
ax.xaxis.set_major_locator(MaxNLocator(integer=True))


[label.set_color( pal[i] ) for i, label in enumerate(ax.get_yticklabels())]


Out[16]:
[None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None]

In [17]:
# count the number of ironman events per year
for y in range(2005,2020):
    ironmans_per_year = df[df['Year'] == y].shape[0]
    print('%d: %d ironmen' % (y, ironmans_per_year))


2005: 19 ironmen
2006: 20 ironmen
2007: 21 ironmen
2008: 22 ironmen
2009: 22 ironmen
2010: 21 ironmen
2011: 22 ironmen
2012: 26 ironmen
2013: 30 ironmen
2014: 36 ironmen
2015: 40 ironmen
2016: 40 ironmen
2017: 41 ironmen
2018: 41 ironmen
2019: 42 ironmen