<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!  Part one Replicate<jupyter_code>#Merge data
merged = pd.merge(income,lifespan)
merged.head()
merged.duplicated(subset=['geo','time']).value_counts()
df = pd.merge(merged, population)
df.head()
#Drop the columns we don't need from entities when we merge
df = pd.merge(df, 
              entities[['country', 'name', 'world_4region', 'world_6region']], 
              left_on='geo', right_on='country')
df.head()
df.shape
df = df.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'})

df.head()
#Explore
df.dtypes
df.describe()
df.describe(exclude='number')
usa = df[df.country == 'United States']
usa.head()
usa.describe()
usa[usa.year.isin([1818,1918,2018])]
china = df[df.country=='China']
china[china.year.isin([1818,1918,2018])]
#Plot time
import seaborn as sns

now = df[df.year == 2018]
then = df[df.year == 1918]
now.head()
sns.relplot(x='income', y='lifespan',hue='6region', size='population',
            sizes=(30, 400), alpha=0.7, data=then)
plt.xscale('log')
plt.title('The World in 1918')
plt.ylim(0,85)
plt.xlim(483.5453734847516,100000)


sns.relplot(x='income', y='lifespan', hue='6region', size='population', 
            sizes=(30, 400), alpha=0.7, data=now)
plt.xscale('log')
plt.title("the World in 2018")
plt.ylim(0,85)
plt.xlim(483.5453734847516,100000)
fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(16,8))

sns.relplot(x='income', y='lifespan', hue='6region', size='population', 
            sizes=(30, 400), alpha=0.7, data=then, ax=ax[0]);

sns.relplot(x='income', y='lifespan', hue='6region', size='population', 
                        sizes=(30, 400), alpha=0.7, data=now, ax=ax[1]);
now.income.nlargest(20)
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(0,90)
plt.title("Qatar is really rich")
plt.text(x=now_qatar.income-5000, y= now_qatar.lifespan+1, s='Qatar')
plt.show()
years = [1818, 1918, 2018]
centuries = df[df.year.isin(years)]

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

axes = fig.axes.flatten()
axes[0].set_title('Poor and Sick in 1818')
axes[1].set_title('Healthy and Poor in 1918')
axes[2].set_title('Healthy and Wealthy in 2018');
years = [1918, 1938, 1978, 1998, 2018]
for year in years:
  sns.relplot(x='income', y='lifespan', hue='6region', size='population', 
              sizes=(30,400), data=df[df.year==year])
  plt.xscale('log')
  plt.xlim((150, 150000))
  plt.ylim((0, 90))
  plt.title('Countries Above the Poverty Line in ' + str(year))
  plt.axvline(x=1000, color='grey')
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
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')
lifespan = pd.read_csv('https://raw.githubusercontent.com/open-numbers/ddf--gapminder--systema_globalis/master/ddf--datapoints--life_expectancy_years--by--geo--time.csv')
population = pd.read_csv('https://raw.githubusercontent.com/open-numbers/ddf--gapminder--systema_globalis/master/ddf--datapoints--population_total--by--geo--time.csv')
entities = pd.read_csv('https://raw.githubusercontent.com/open-numbers/ddf--gapminder--systema_globalis/master/ddf--entities--geo--country.csv')
concepts = pd.read_csv('https://raw.githubusercontent.com/open-numbers/ddf--gapminder--systema_globalis/master/ddf--concepts.csv')
income.shape, lifespan.shape, population.shape, entities.shape, concepts.shape
income.head()
lifespan.head()
population.head()
entities.head()
concepts.head()<jupyter_output><empty_output><jupyter_text>Part Two<jupyter_code>import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
import altair as alt
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')
lifespan = pd.read_csv('https://raw.githubusercontent.com/open-numbers/ddf--gapminder--systema_globalis/master/ddf--datapoints--life_expectancy_years--by--geo--time.csv')
population = pd.read_csv('https://raw.githubusercontent.com/open-numbers/ddf--gapminder--systema_globalis/master/ddf--datapoints--population_total--by--geo--time.csv')
entities = pd.read_csv('https://raw.githubusercontent.com/open-numbers/ddf--gapminder--systema_globalis/master/ddf--entities--geo--country.csv')
income.shape, lifespan.shape, population.shape, entities.shape
income.head()
lifespan.head()
population.head()
entities.head()
entities.shape
entities.main_religion_2008.isna().sum()
entities = entities[~entities['main_religion_2008'].isna()]
entities.shape
merged.head()
final = pd.merge(merged, entities[['country', 'main_religion_2008']], how='inner', left_on='geo', right_on='country')
final.head()
final = pd.merge(final, population)
final = final.rename(columns={'income_per_person_gdppercapita_ppp_inflation_adjusted': 'avg_income/person'})
final.isna().sum()
final[final['time']==1800].head()
brush = alt.selection(type='interval')
base = alt.Chart(final[final['time']==1800]).mark_point().encode(
    y='life_expectancy_years',
    x='avg_income/person',
    size='population_total',
    tooltip=['geo', 'avg_income/person', 'main_religion_2008', 'population_total'],
    color=alt.condition(brush, 'main_religion_2008', alt.ColorValue('gray'))
).add_selection(brush).properties(width=400,height=400)
base.encode(x='avg_income/person')
brush = alt.selection(type='interval')
base = alt.Chart(final[final['time']==1900]).mark_point().encode(
    y='life_expectancy_years',
    x='avg_income/person',
    size='population_total',
    tooltip=['geo', 'avg_income/person', 'main_religion_2008', 'population_total'],
    color=alt.condition(brush, 'main_religion_2008', alt.ColorValue('gray'))
).add_selection(brush).properties(width=400,height=400)
base.encode(x='avg_income/person')
brush = alt.selection(type='interval')
base = alt.Chart(final[final['time']==2000]).mark_point().encode(
    alt.X('avg_income/person:Q', scale=alt.Scale(type='log')),
    alt.Y('life_expectancy_years:Q', scale=alt.Scale(zero=False)),
    size='population_total',
    tooltip=['geo', 'avg_income/person', 'main_religion_2008', 'population_total'],
    color=alt.condition(brush, 'main_religion_2008', alt.ColorValue('gray')),
).add_selection(brush).properties(width=400,height=400)
base.encode(x='avg_income/person')<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.16152232544201378, 'fraction_numerical': 0.03496154230346619, 'mean_word_length': 5.710455764075067, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 24, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '347807', 'license': 'MIT', 'n_tokens_mistral': 3850, 'n_tokens_neox': 3413, 'n_words': 605, 'path': 'module4-sequence-your-narrative/LS_DS_124_Sequence_your_narrative_Assignment.ipynb', 'repo_name': 'HakujouRyu/DS-Unit-1-Sprint-2-Data-Wrangling-and-Storytelling', 'size': '10023'}