-
Notifications
You must be signed in to change notification settings - Fork 0
/
gdp_plots.py
91 lines (71 loc) · 1.97 KB
/
gdp_plots.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
import sys
import pandas
import matplotlib.pyplot as plt
import glob
def parse_arguments(argv):
"""
Parse the argument list passed from the command line
(after the program filename is removed) and return a list
of filenames.
Input:
------
argument list (normally sys.argv[1:])
Returns:
------
filenames: list of strings, list of files to plot
"""
if argv == []:
print("Not enough arguments have been provided")
print("Usage: python gdp_plots.py < filenames >")
print("Options:")
print("-a: plot all gdp data sets in current directory")
if "-a" in argv:
filenames = glob.glob("data/*gdp*.csv")
if filenames == []:
print("No files found in this folder.")
print("Make sure data is located in current directory.")
else:
filenames = argv
return filenames
def create_plot(filename):
"""
Creates a plot for the specified
data file.
Input:
------
filename: string, path to file to plot
Returns:
------
none
"""
data = pandas.read_csv(filename, index_col = 'country').T
split_name1 = filename.split('.')[0]
split_name2 = split_name1.split('/')[1]
save_name = 'figs/' + split_name2 + '.png'
ax = data.plot(title = filename)
ax.set_xlabel("Year")
ax.set_ylabel("GDP Per Capita")
ax.set_xticks(range(len(data.index)))
ax.set_xticklabels(data.index, rotation = 45)
plt.savefig(save_name)
def create_plots(filenames):
"""
Takes in a list of filenames to plot
and creates a plot for each file.
Input:
------
filenames: list of strings, list of files to plot
Returns:
------
none
"""
for filename in filenames:
create_plot(filename)
def main():
"""
Does all the work :)
"""
filenames = parse_arguments(sys.argv[1:])
create_plots(filenames)
if __name__ == "__main__":
main()