<jupyter_start><jupyter_text>_Lambda School Data Science_ Sequence Your Narrative - AssignmentToday we will create a sequence of visualizations inspired by [<NAME>'s 200 Countries, 200 Years, 4 Minutes](https://www.youtube.com/watch?v=jbkSRLYSojo).Using this [data from Gapminder](https://github.com/open-numbers/ddf--gapminder--systema_globalis/):- [Income Per Person (GDP Per Capital, Inflation Adjusted) by Geo & Time](https://raw.githubusercontent.com/open-numbers/ddf--gapminder--systema_globalis/master/ddf--datapoints--income_per_person_gdppercapita_ppp_inflation_adjusted--by--geo--time.csv)- [Life Expectancy (in Years) by Geo & Time](https://raw.githubusercontent.com/open-numbers/ddf--gapminder--systema_globalis/master/ddf--datapoints--life_expectancy_years--by--geo--time.csv)- [Population Totals, by Geo & Time](https://raw.githubusercontent.com/open-numbers/ddf--gapminder--systema_globalis/master/ddf--datapoints--population_total--by--geo--time.csv)- [Entities](https://raw.githubusercontent.com/open-numbers/ddf--gapminder--systema_globalis/master/ddf--entities--geo--country.csv)- [Concepts](https://raw.githubusercontent.com/open-numbers/ddf--gapminder--systema_globalis/master/ddf--concepts.csv)  Objectives- sequence multiple visualizations- combine qualitative anecdotes with quantitative aggregatesLinks- [<NAME>’s TED talks](https://www.ted.com/speakers/hans_rosling)- [Spiralling global temperatures from 1850-2016](https://twitter.com/ed_hawkins/status/729753441459945474)- "[The Pudding](https://pudding.cool/) explains ideas debated in culture with visual essays."- [A Data Point Walks Into a Bar](https://lisacharlotterost.github.io/2016/12/27/datapoint-in-bar/): a thoughtful blog post about emotion and empathy in data storytelling  ASSIGNMENT 1.   Replicate the Lesson Code2.   Take it further by using the same gapminder dataset to create a sequence of visualizations that combined tell a story of your choosing.Get creative! Use text annotations to call out specific countries, maybe: change how the points are colored, change the opacity of the points, change their sized, pick a specific time window. Maybe only work with a subset of countries, change fonts, change background colors, etc. make it your own!<jupyter_code>#import important libraries
import seaborn as sns
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

#load income data
income = pd.read_csv('https://raw.githubusercontent.com/open-numbers/ddf--gapminder--systema_globalis/master/ddf--datapoints--income_per_person_gdppercapita_ppp_inflation_adjusted--by--geo--time.csv')
#load lifespan data
lifespan = pd.read_csv('https://raw.githubusercontent.com/open-numbers/ddf--gapminder--systema_globalis/master/ddf--datapoints--life_expectancy_years--by--geo--time.csv')
#load population data
population = pd.read_csv('https://raw.githubusercontent.com/open-numbers/ddf--gapminder--systema_globalis/master/ddf--datapoints--population_total--by--geo--time.csv')
#load entities data
entities = pd.read_csv('https://raw.githubusercontent.com/open-numbers/ddf--gapminder--systema_globalis/master/ddf--entities--geo--country.csv')
#load convepts data
concepts = pd.read_csv('https://raw.githubusercontent.com/open-numbers/ddf--gapminder--systema_globalis/master/ddf--concepts.csv')
#check that all data is loaded
income.shape, lifespan.shape, population.shape, entities.shape, concepts.shape
income.head()
lifespan.head()
population.head()
pd.options.display.max_columns = 500
entities.head()
concepts.head()
#merge the data
print(income.shape)
print(lifespan.shape)

#Merge using default
df = pd.merge(income, lifespan)
print(df.shape)
#merge as per above with specifying the parameters
df = pd.merge(income, lifespan, on = ['geo', 'time'], how = 'inner')
print(df.shape)

df.head()
df = pd.merge(df, population)
print(df.shape)
df.head()
entities['world_6region'].value_counts()
entities['world_4region'].value_counts()
entity_columns_to_keep = ['country', 'name', 'world_4region', 'world_6region']

entities = entities[entity_columns_to_keep]

entities.head()
merged = pd.merge(df, entities, left_on = 'geo', right_on = 'country')

merged.head()
merged = merged.drop('geo', axis  ='columns')
merged.head()
merged = merged.rename(columns = {
    'country': 'country_code',
    'time': 'year', 
    'income_per_person_gdppercapita_ppp_inflation_adjusted': 'income', 
    'life_expectancy_years': 'lifespan',
    'population_total': 'population',
    'name': 'country',
    'world_6region': '6region',
    'world_4region': '4region'
})

merged.head()
#explore the data
merged.dtypes
merged.describe()
merged.describe(exclude = 'number')
merged.country.unique()
usa = merged[merged.country == 'United States']
usa.head()
usa[usa.year.isin([1818, 1918, 2018])]
china = merged[merged.country == 'China']
china[china.year.isin([1818, 1918, 2018])]
#plot visulaization
import seaborn as sns

now = merged[merged.year == 2018]

now.head()
sns.relplot(x = 'income', y = 'lifespan', hue = '6region', size = 'population', sizes = (30, 400), data = now)
plt.xscale('log')
plt.ylim(20, 85)
plt.title('The World in 2018')
plt.show()
# Analyze outliers
now.sort_values('income', ascending = False)
now_qatar = now[now.country == 'Qatar']
now_qatar.head()
sns.relplot(x='income', y='lifespan', hue='6region', size='population', sizes=(30, 400), data=now)
plt.xscale('log')
plt.ylim(20, 85)
plt.title('The World in 2018')
plt.text(x=now_qatar.income-5000, y=now_qatar.lifespan+1, s='Qatar')
plt.show()
#plot multiple years

years = [1818, 1918, 2018]

centuries = merged[merged.year.isin(years)]

fig = sns.relplot(x = 'income', y = 'lifespan', hue = '6region', size = 'population', sizes = (30, 400), col = 'year', data = centuries)

plt.xscale('log')
plt.text(x = now_qatar.income-5000, y = now_qatar.lifespan+1, s = 'Qatar')

#Set the titles on the individual axes
axes = fig.axes.flatten()
axes[0].set_title('1818')
axes[1].set_title('1918')
axes[2].set_title('2018')

plt.show()
year = [1918, 1938, 1978, 1998, 2018]

decades = merged[merged.year.isin(year)]
sns.relplot(x='income', y='lifespan', hue='6region', size='population', col='year', data=decades)
plt.show()
for year in years:
  sns.relplot(x='income', y='lifespan', hue='6region', size='population', data=merged[merged.year==year])
  plt.xscale('log')
  plt.xlim((150, 150000))
  plt.ylim((0, 90))
  plt.title(year)
  plt.axvline(x=1000, color='grey')
  plt.axhline(y=40, color='grey')
#lifespan/income 10 years before world war 2, 5 years before world war 2, during world war 2 (1943), 5 years after world war 2, 10 years after world war 2

year = [1929, 1934, 1943, 1950, 1955]
timescale = merged[merged.year.isin(year)]

fig = sns.relplot(x = 'income', y = 'lifespan', hue = '6region', size = 'population', col = 'year', data = timescale)
axes = fig.axes.flatten()
axes[0].set_title('10 years before World War 2')
axes[1].set_title('5 years before World War 2')
axes[2].set_title('Middle of World War 2')
axes[3].set_title('5 years after World War 2')
axes[4].set_title('10 years after World War 2')
plt.show()<jupyter_output><empty_output><jupyter_text>STRETCH OPTIONS 1. Animate!- [How to Create Animated Graphs in Python](https://towardsdatascience.com/how-to-create-animated-graphs-in-python-bb619cc2dec1)- Try using [Plotly](https://plot.ly/python/animations/)!- [The Ultimate Day of Chicago Bikeshare](https://chrisluedtke.github.io/divvy-data.html) (Lambda School Data Science student)- [Using Phoebe for animations in Google Colab](https://colab.research.google.com/github/phoebe-project/phoebe2-docs/blob/2.1/tutorials/animations.ipynb) 2. Study for the Sprint Challenge- Concatenate DataFrames- Merge DataFrames- Reshape data with `pivot_table()` and `.melt()`- Be able to reproduce a FiveThirtyEight graph using Matplotlib or Seaborn. 3. Work on anything related to your portfolio site / Data Storytelling Project<jupyter_code># TODO<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14847380982288658, 'fraction_numerical': 0.0321567642255998, 'mean_word_length': 5.574731626754748, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 20, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '404418', 'license': 'MIT', 'n_tokens_mistral': 3029, 'n_tokens_neox': 2662, 'n_words': 629, 'path': 'module4-sequence-your-narrative/Kyle_Haggin_LS_DS_124_Sequence_your_narrative_Assignment.ipynb', 'repo_name': 'KyleHaggin/DS-Unit-1-Sprint-2-Data-Wrangling-and-Storytelling', 'size': '7973'}