-
Notifications
You must be signed in to change notification settings - Fork 1
/
mathplot.py
111 lines (77 loc) · 3.19 KB
/
mathplot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import matplotlib
import numpy as np
import csv
import requests
import pandas as pd
import datapane as dp
import matplotlib.pyplot as plt
import ClassifyStage as cs
import ipywidgets as widgets
from IPython.display import display
from ipywidgets import interact, interactive, fixed, interact_manual
all_data = {'United States': 840, 'China': 156, 'United Kingdom': 826, 'India': 356, 'Russia': 643}
# country = input("Enter country: ")
# year = int(input("Enter decade between 1950 and 2020: "))
# @widgets.interact( x = ["United States", "India", "Russia", "China", "United Kingdom"], y = ["1950", "1960", "1970", "1980", "1990", "2000", "2010", "2020"])
def move_figure(f, x, y):
"""Move figure's upper left corner to pixel (x, y)"""
backend = matplotlib.get_backend()
if backend == 'TkAgg':
f.canvas.manager.window.wm_geometry("+%d+%d" % (x, y))
elif backend == 'WXAgg':
f.canvas.manager.window.SetPosition((x, y))
else:
# This works for QT and GTK
# You can also use window.setGeometry
f.canvas.manager.window.move(x, y)
def f(country, year):
# print(x + " " + str(y))
url = "https://www.populationpyramid.net/api/pp/" + str(all_data[country]) + "/" + str(year) + "/?csv=true"
# Send HTTP GET request via requests
data = requests.get(url)
# Convert to iterator by splitting on \n chars
lines = data.text.splitlines()
# Parse as CSV object
reader = csv.reader(lines)
y_age, x_M, x_F = [], [], []
# View Result
for row in reader:
if row[1] != "M" and row[2] != "F":
row[1] = int(row[1])
row[2] = int(row[2])
row.append(str(year))
y_age.append(row[0])
x_M.append(int(row[1]))
x_F.append(int(row[2]) * 1)
graph_data = {'Age': y_age, "Male": x_M, "Female": x_F, "Year": str(year)}
stage = cs.stage(graph_data)
# print(graph_data)
# create dataframe
# df = pd.DataFrame({'Age': ['0-9', '10-19', '20-29', '30-39', '40-49', '50-59', '60-69', '70-79', '80-89', '90+'],
# 'Male': [9000, 14000, 22000, 26000, 34000, 32000, 29000, 22000, 14000, 3000],
# 'Female': [8000, 15000, 19000, 28000, 35000, 34000, 28000, 24000, 17000, 5000]})
df = pd.DataFrame(graph_data)
# view dataframe
y = range(0, len(df))
x_male = df['Male']
x_female = df['Female']
# define plot parameters
fig, axes = plt.subplots(ncols=2, sharey=True, figsize=(9, 6))
move_figure(fig, 600, 90)
# specify background color and plot title
fig.patch.set_facecolor('xkcd:light grey')
# print(y)
plt.figtext(.5, 0.95, "Population Pyramid for " + country + " " + str(year) + " (Stage: " + str(stage) + ")", fontsize=13, ha='center')
# define male and female bars
axes[0].barh(y, x_male, align='center', color='royalblue')
axes[0].set(title='Males')
axes[1].barh(y, x_female, align='center', color='lightpink')
axes[1].set(title='Females')
# adjust grid parameters and specify labels for y-axis
axes[1].grid()
axes[0].set(yticks=y, yticklabels=df['Age'])
axes[0].invert_xaxis()
axes[0].grid()
# display plot
plt.show()
# f(country, year)