Skip to content

Commit

Permalink
Added vis spec for fractional urban population
Browse files Browse the repository at this point in the history
  • Loading branch information
gchure committed Sep 23, 2020
1 parent f209af5 commit 05b9a97
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 3 deletions.
Binary file modified anthro/__pycache__/io.cpython-37.pyc
Binary file not shown.
4 changes: 3 additions & 1 deletion anthro/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,14 @@ def numeric_formatter(values, digits=3, sci=False, unit=''):
if (base >=_k[0]) & (base < _k[1]):
n = _k[0]
l = _v
val = str(np.round(v*10**-n, decimals=2))
val = str(np.round(v*10**-n, decimals=digits))
if len(val) <= digits:
val = val
else:
if val[digits-1] == '.':
end = digits + 1
if val[1] == '.':
end = digits + 1
else:
end = digits
val = val[:end]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@


# %%
# Generate JSON vis for grand categories
# Generate JSON vis for subcategories
for g, d in data.groupby('category'):
d = d.groupby(['year']).sum().reset_index()
chart= alt.Chart(d).encode(
x=alt.X(field="year", type="temporal", timeUnit='year',
title="year"),
Expand All @@ -49,4 +49,6 @@



# %%
# %%
chart
# %%
54 changes: 54 additions & 0 deletions data/anthropocentric/FAOSTAT_world_population/viz/generate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#%%
import numpy as np
import pandas as pd
import altair as alt
import anthro.io

# Load the produciton data.
data = pd.read_csv('../processed/FAOSTAT_rural_urban_population.csv')
data['year'] = pd.to_datetime(data['year'], format='%Y')
agg_data = data.groupby(['year']).apply(lambda x: x[x['category']=='urban']['population'].values[0] / x['population'].sum()).reset_index()
agg_data.columns = ['year', 'urban_fraction_pct']
agg_data['urban_fraction_pct'] *= 100
agg_data['urban_population'] = anthro.io.numeric_formatter(data[data['category']=='urban']['population'].values, sci=False)
agg_data['total_population'] = anthro.io.numeric_formatter(data.groupby(['year'])['population'].sum().values,sci=False)
agg_data
#%%
# Generate the plot.
chart = alt.Chart(agg_data).encode(
x=alt.X(field='year', type='temporal',
timeUnit='year', title='year'),
y=alt.Y(field='urban_fraction_pct', type='quantitative',
title='urban population [% of total]',
scale=alt.Scale(domain=[25, 60])),
tooltip=[alt.Tooltip(field='year', type='temporal', timeUnit='year', title='year'),
alt.Tooltip(field='urban_population', type='nominal', title='urban population'),
alt.Tooltip(field='total_population', type='nominal', title='total population')]
).properties(
width="container",
height=300
)
l = chart.mark_line(color='dodgerblue')
p = chart.mark_point(color='dodgerblue', filled=True)

chart = alt.layer(l, p)
chart.save('urban_pop.json')
# # Generate JSON vis for subcategories
# for g, d in data.groupby('subcategory'):
# chart= alt.Chart(d).encode(
# x=alt.X(field="year", type="temporal", timeUnit='year',
# title="year"),
# y=alt.Y(field="mass_produced_Mt",
# type="quantitative",
# title="produced mass [Mt]"),
# tooltip=[alt.Tooltip("year:T", timeUnit="year", title="year"),
# alt.Tooltip("mass_produced_Mt:Q", format="0.0f", title="produced mass [Mt]")]
# ).properties(width="container",
# height=300
# ).mark_line(color='dodgerblue')
# l = chart.mark_line(color='dodgerblue')
# p = chart.mark_point(filled=True, color='dodgerblue')
# figure = alt.layer(l, p)
# figure.save(f'{g}.json')

# %%
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"config": {"view": {"continuousWidth": 400, "continuousHeight": 300}}, "layer": [{"mark": {"type": "line", "color": "dodgerblue"}, "encoding": {"tooltip": [{"type": "temporal", "field": "year", "timeUnit": "year", "title": "year"}, {"type": "nominal", "field": "urban_population", "title": "urban population"}, {"type": "nominal", "field": "total_population", "title": "total population"}], "x": {"type": "temporal", "field": "year", "timeUnit": "year", "title": "year"}, "y": {"type": "quantitative", "field": "urban_fraction_pct", "scale": {"domain": [25, 60]}, "title": "urban population [% of total]"}}, "height": 300, "width": "container"}, {"mark": {"type": "point", "color": "dodgerblue", "filled": true}, "encoding": {"tooltip": [{"type": "temporal", "field": "year", "timeUnit": "year", "title": "year"}, {"type": "nominal", "field": "urban_population", "title": "urban population"}, {"type": "nominal", "field": "total_population", "title": "total population"}], "x": {"type": "temporal", "field": "year", "timeUnit": "year", "title": "year"}, "y": {"type": "quantitative", "field": "urban_fraction_pct", "scale": {"domain": [25, 60]}, "title": "urban population [% of total]"}}, "height": 300, "width": "container"}], "data": {"name": "data-e37b14a80281a8aeeb5283a127955861"}, "$schema": "https://vega.github.io/schema/vega-lite/v4.8.1.json", "datasets": {"data-e37b14a80281a8aeeb5283a127955861": [{"year": "1950-01-01T00:00:00", "urban_fraction_pct": 29.606530072733396, "urban_population": "750 M", "total_population": "2.53 B"}, {"year": "1951-01-01T00:00:00", "urban_fraction_pct": 29.9970068001563, "urban_population": "775 M", "total_population": "2.58 B"}, {"year": "1952-01-01T00:00:00", "urban_fraction_pct": 30.38421948603797, "urban_population": "799 M", "total_population": "2.63 B"}, {"year": "1953-01-01T00:00:00", "urban_fraction_pct": 30.788907892699164, "urban_population": "824 M", "total_population": "2.67 B"}, {"year": "1954-01-01T00:00:00", "urban_fraction_pct": 31.20722151766593, "urban_population": "850 M", "total_population": "2.72 B"}, {"year": "1955-01-01T00:00:00", "urban_fraction_pct": 31.635357690664677, "urban_population": "877 M", "total_population": "2.77 B"}, {"year": "1956-01-01T00:00:00", "urban_fraction_pct": 32.065303492296245, "urban_population": "904 M", "total_population": "2.82 B"}, {"year": "1957-01-01T00:00:00", "urban_fraction_pct": 32.49055268598722, "urban_population": "933 M", "total_population": "2.87 B"}, {"year": "1958-01-01T00:00:00", "urban_fraction_pct": 32.91759130510588, "urban_population": "962 M", "total_population": "2.92 B"}, {"year": "1959-01-01T00:00:00", "urban_fraction_pct": 33.34046328071847, "urban_population": "992 M", "total_population": "2.97 B"}, {"year": "1960-01-01T00:00:00", "urban_fraction_pct": 33.75449322743747, "urban_population": "1.02 B", "total_population": "3.03 B"}, {"year": "1961-01-01T00:00:00", "urban_fraction_pct": 34.15311927828474, "urban_population": "1.05 B", "total_population": "3.09 B"}, {"year": "1962-01-01T00:00:00", "urban_fraction_pct": 34.559933061019215, "urban_population": "1.08 B", "total_population": "3.14 B"}, {"year": "1963-01-01T00:00:00", "urban_fraction_pct": 34.96782100057192, "urban_population": "1.12 B", "total_population": "3.21 B"}, {"year": "1964-01-01T00:00:00", "urban_fraction_pct": 35.367434162985525, "urban_population": "1.15 B", "total_population": "3.27 B"}, {"year": "1965-01-01T00:00:00", "urban_fraction_pct": 35.587250752778026, "urban_population": "1.18 B", "total_population": "3.34 B"}, {"year": "1966-01-01T00:00:00", "urban_fraction_pct": 35.79664240276676, "urban_population": "1.22 B", "total_population": "3.40 B"}, {"year": "1967-01-01T00:00:00", "urban_fraction_pct": 36.003080994014894, "urban_population": "1.25 B", "total_population": "3.47 B"}, {"year": "1968-01-01T00:00:00", "urban_fraction_pct": 36.20429683913652, "urban_population": "1.28 B", "total_population": "3.55 B"}, {"year": "1969-01-01T00:00:00", "urban_fraction_pct": 36.4001066465738, "urban_population": "1.32 B", "total_population": "3.62 B"}, {"year": "1970-01-01T00:00:00", "urban_fraction_pct": 36.59470558603196, "urban_population": "1.35 B", "total_population": "3.70 B"}, {"year": "1971-01-01T00:00:00", "urban_fraction_pct": 36.78260093799156, "urban_population": "1.38 B", "total_population": "3.77 B"}, {"year": "1972-01-01T00:00:00", "urban_fraction_pct": 36.9912519273651, "urban_population": "1.42 B", "total_population": "3.85 B"}, {"year": "1973-01-01T00:00:00", "urban_fraction_pct": 37.22887241980438, "urban_population": "1.46 B", "total_population": "3.92 B"}, {"year": "1974-01-01T00:00:00", "urban_fraction_pct": 37.496043370139304, "urban_population": "1.50 B", "total_population": "4.00 B"}, {"year": "1975-01-01T00:00:00", "urban_fraction_pct": 37.71983581901355, "urban_population": "1.53 B", "total_population": "4.07 B"}, {"year": "1976-01-01T00:00:00", "urban_fraction_pct": 37.96983490690895, "urban_population": "1.57 B", "total_population": "4.15 B"}, {"year": "1977-01-01T00:00:00", "urban_fraction_pct": 38.22043950555839, "urban_population": "1.61 B", "total_population": "4.22 B"}, {"year": "1978-01-01T00:00:00", "urban_fraction_pct": 38.54927377004415, "urban_population": "1.65 B", "total_population": "4.30 B"}, {"year": "1979-01-01T00:00:00", "urban_fraction_pct": 38.94505742874106, "urban_population": "1.70 B", "total_population": "4.38 B"}, {"year": "1980-01-01T00:00:00", "urban_fraction_pct": 39.345874996563296, "urban_population": "1.75 B", "total_population": "4.45 B"}, {"year": "1981-01-01T00:00:00", "urban_fraction_pct": 39.75928869475107, "urban_population": "1.80 B", "total_population": "4.53 B"}, {"year": "1982-01-01T00:00:00", "urban_fraction_pct": 40.14340945651125, "urban_population": "1.85 B", "total_population": "4.61 B"}, {"year": "1983-01-01T00:00:00", "urban_fraction_pct": 40.49367109511909, "urban_population": "1.90 B", "total_population": "4.70 B"}, {"year": "1984-01-01T00:00:00", "urban_fraction_pct": 40.846401855057586, "urban_population": "1.95 B", "total_population": "4.78 B"}, {"year": "1985-01-01T00:00:00", "urban_fraction_pct": 41.1987886829064, "urban_population": "2.00 B", "total_population": "4.87 B"}, {"year": "1986-01-01T00:00:00", "urban_fraction_pct": 41.55432722878855, "urban_population": "2.06 B", "total_population": "4.96 B"}, {"year": "1987-01-01T00:00:00", "urban_fraction_pct": 41.91129455674996, "urban_population": "2.11 B", "total_population": "5.05 B"}, {"year": "1988-01-01T00:00:00", "urban_fraction_pct": 42.266727465527914, "urban_population": "2.17 B", "total_population": "5.14 B"}, {"year": "1989-01-01T00:00:00", "urban_fraction_pct": 42.61120724754996, "urban_population": "2.23 B", "total_population": "5.24 B"}, {"year": "1990-01-01T00:00:00", "urban_fraction_pct": 42.96102769020927, "urban_population": "2.29 B", "total_population": "5.33 B"}, {"year": "1991-01-01T00:00:00", "urban_fraction_pct": 43.321033862964505, "urban_population": "2.34 B", "total_population": "5.41 B"}, {"year": "1992-01-01T00:00:00", "urban_fraction_pct": 43.6802702404203, "urban_population": "2.40 B", "total_population": "5.50 B"}, {"year": "1993-01-01T00:00:00", "urban_fraction_pct": 44.044054365428806, "urban_population": "2.46 B", "total_population": "5.58 B"}, {"year": "1994-01-01T00:00:00", "urban_fraction_pct": 44.41114862831571, "urban_population": "2.51 B", "total_population": "5.67 B"}, {"year": "1995-01-01T00:00:00", "urban_fraction_pct": 44.77991291824604, "urban_population": "2.57 B", "total_population": "5.75 B"}, {"year": "1996-01-01T00:00:00", "urban_fraction_pct": 45.14982811595231, "urban_population": "2.63 B", "total_population": "5.83 B"}, {"year": "1997-01-01T00:00:00", "urban_fraction_pct": 45.52547770720843, "urban_population": "2.69 B", "total_population": "5.91 B"}, {"year": "1998-01-01T00:00:00", "urban_fraction_pct": 45.90556428930162, "urban_population": "2.74 B", "total_population": "5.98 B"}, {"year": "1999-01-01T00:00:00", "urban_fraction_pct": 46.28800127007754, "urban_population": "2.80 B", "total_population": "6.06 B"}, {"year": "2000-01-01T00:00:00", "urban_fraction_pct": 46.677042322888724, "urban_population": "2.86 B", "total_population": "6.14 B"}, {"year": "2001-01-01T00:00:00", "urban_fraction_pct": 47.12974868986654, "urban_population": "2.93 B", "total_population": "6.22 B"}, {"year": "2002-01-01T00:00:00", "urban_fraction_pct": 47.6314971073317, "urban_population": "3.00 B", "total_population": "6.30 B"}, {"year": "2003-01-01T00:00:00", "urban_fraction_pct": 48.13582710742498, "urban_population": "3.07 B", "total_population": "6.38 B"}, {"year": "2004-01-01T00:00:00", "urban_fraction_pct": 48.64362312067967, "urban_population": "3.14 B", "total_population": "6.46 B"}, {"year": "2005-01-01T00:00:00", "urban_fraction_pct": 49.156641939305686, "urban_population": "3.21 B", "total_population": "6.54 B"}, {"year": "2006-01-01T00:00:00", "urban_fraction_pct": 49.66065448972817, "urban_population": "3.28 B", "total_population": "6.62 B"}, {"year": "2007-01-01T00:00:00", "urban_fraction_pct": 50.15507924767558, "urban_population": "3.36 B", "total_population": "6.70 B"}, {"year": "2008-01-01T00:00:00", "urban_fraction_pct": 50.66030945416888, "urban_population": "3.44 B", "total_population": "6.79 B"}, {"year": "2009-01-01T00:00:00", "urban_fraction_pct": 51.16326372163045, "urban_population": "3.51 B", "total_population": "6.87 B"}, {"year": "2010-01-01T00:00:00", "urban_fraction_pct": 51.663994706858205, "urban_population": "3.59 B", "total_population": "6.95 B"}, {"year": "2011-01-01T00:00:00", "urban_fraction_pct": 52.128629791791084, "urban_population": "3.67 B", "total_population": "7.04 B"}, {"year": "2012-01-01T00:00:00", "urban_fraction_pct": 52.577855743138905, "urban_population": "3.74 B", "total_population": "7.12 B"}, {"year": "2013-01-01T00:00:00", "urban_fraction_pct": 53.02598362168759, "urban_population": "3.82 B", "total_population": "7.21 B"}, {"year": "2014-01-01T00:00:00", "urban_fraction_pct": 53.474783167793525, "urban_population": "3.90 B", "total_population": "7.29 B"}, {"year": "2015-01-01T00:00:00", "urban_fraction_pct": 53.92784649280698, "urban_population": "3.98 B", "total_population": "7.38 B"}, {"year": "2016-01-01T00:00:00", "urban_fraction_pct": 54.38157369891691, "urban_population": "4.06 B", "total_population": "7.46 B"}, {"year": "2017-01-01T00:00:00", "urban_fraction_pct": 54.83503140177941, "urban_population": "4.14 B", "total_population": "7.55 B"}, {"year": "2018-01-01T00:00:00", "urban_fraction_pct": 55.285172337025024, "urban_population": "4.22 B", "total_population": "7.63 B"}]}}

0 comments on commit 05b9a97

Please sign in to comment.