-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plotter.py
229 lines (184 loc) · 8.91 KB
/
Plotter.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import copy
class plotter:
def __init__(self, data_frame):
self.data = data_frame
self.fig = plt.figure()
def plot_continuous_var_vs_income(self, column,
max_gross = None, min_gross = None,
max_rating = None, min_rating = None,
max_count = None, min_count = None,
oldest = None, newest = None,
acceptable_genres = None):
#plot GDP vs income
if not column in self.data.columns:
return None
df = copy.copy(self.data)
#convert strings
df['World Wide Gross'] = pd.to_numeric(df['World Wide Gross'])
df['Ratings'] = pd.to_numeric(df['Ratings'])
df['Number of Ratings'] = pd.to_numeric(df['Number of Ratings'])
df['Release Date'] = pd.to_datetime(df['Release Date'])
#Apply Filters
df = prune_df(df,
max_gross, min_gross,
max_rating, min_rating,
max_count, min_count,
oldest, newest,
acceptable_genres)
X = np.array([float(val) for (val, y) in zip(df[column], df['World Wide Gross']) if not pd.isna(y)])
Y = np.array([float(val) for val in df['World Wide Gross'] if not pd.isna(val)])
plt.scatter(X, Y)
plt.xlabel(column)
plt.ylabel('World Wide Gross')
plt.title('{} vs World Wide Gross'.format(column))
def plot_release_date_vs_income(self,
max_gross = None, min_gross = None,
max_rating = None, min_rating = None,
max_count = None, min_count = None,
oldest = None, newest = None,
acceptable_genres = None):
#Plot time series data vs world wide gross
df = self.data
#COnvert strings
df['World Wide Gross'] = pd.to_numeric(df['World Wide Gross'])
df['Ratings'] = pd.to_numeric(df['Ratings'])
df['Number of Ratings'] = pd.to_numeric(df['Number of Ratings'])
df['Release Date'] = pd.to_datetime(df['Release Date'])
#Apply filters
df = prune_df(df,
max_gross, min_gross,
max_rating, min_rating,
max_count, min_count,
oldest, newest,
acceptable_genres)
X = np.array([val for (val, y) in zip(df['Release Date'], df['World Wide Gross']) if not pd.isna(y)])
Y = np.array([float(val) for val in df['World Wide Gross'] if not pd.isna(val)])
plt.plot_date(X, Y)
plt.xlabel('Release Date')
plt.ylabel('World Wide Gross')
plt.title('Release Date vs World Wide Gross')
def plot_genre_vs_income(self,
max_gross = None, min_gross = None,
max_rating = None, min_rating = None,
max_count = None, min_count = None,
oldest = None, newest = None,
acceptable_genres = None):
#Catagorically plot the average gross of each genre
df = self.data
#Convert strigs to usable types
df['World Wide Gross'] = pd.to_numeric(df['World Wide Gross'])
df['Ratings'] = pd.to_numeric(df['Ratings'])
df['Number of Ratings'] = pd.to_numeric(df['Number of Ratings'])
df['Release Date'] = pd.to_datetime(df['Release Date'])
#Apply filters
df = prune_df(df,
max_gross, min_gross,
max_rating, min_rating,
max_count, min_count,
oldest, newest,
acceptable_genres)
vals = {}
for (genres, gross) in zip(df['Genres'], df['World Wide Gross']):
if not pd.isna(gross):
genres = genres.split()
for genre in genres:
if genre in vals.keys():
vals[genre].append(gross)
else:
vals[genre] = [gross]
Y = np.array([category for category in vals.keys()])
X = np.array([sum(grosses) / len(grosses) for grosses in vals.values()])
y_pos = np.arange(len(Y))
plt.bar(y_pos, X, align='center', alpha=0.5)
plt.xticks(y_pos, Y)
plt.ylabel('World Wide Gross')
plt.title('Genera vs Average World Wide Gross')
def plot_gross_hist(self,
max_gross = None, min_gross = None,
max_rating = None, min_rating = None,
max_count = None, min_count = None,
oldest = None, newest = None,
acceptable_genres = None):
df = self.data
# Convert strigs to usable types
df['World Wide Gross'] = pd.to_numeric(df['World Wide Gross'])
df['Ratings'] = pd.to_numeric(df['Ratings'])
df['Number of Ratings'] = pd.to_numeric(df['Number of Ratings'])
df['Release Date'] = pd.to_datetime(df['Release Date'])
# Apply filters
df = prune_df(df,
max_gross, min_gross,
max_rating, min_rating,
max_count, min_count,
oldest, newest,
acceptable_genres)
X = np.array([float(val) for val in df['World Wide Gross'] if not pd.isna(val)])
plt.hist(X)
plt.xlabel('World Wide Gross')
plt.ylabel('Count')
plt.title('World Wide Gross Distribution')
def plot(self, column,
max_gross = None, min_gross = None,
max_rating = None, min_rating = None,
max_count = None, min_count = None,
oldest = None, newest = None,
acceptable_genres = None):
if column == 'Genres':
self.plot_genre_vs_income(max_gross, min_gross,
max_rating, min_rating,
max_count, min_count,
oldest, newest,
acceptable_genres)
elif column == 'Release Date':
self.plot_release_date_vs_income(max_gross, min_gross,
max_rating, min_rating,
max_count, min_count,
oldest, newest,
acceptable_genres)
elif column == 'World Wide Gross':
self.plot_gross_hist(max_gross, min_gross,
max_rating, min_rating,
max_count, min_count,
oldest, newest,
acceptable_genres)
elif column in ['Ratings', 'Number of Ratings', 'World Wide Gross']:
self.plot_continuous_var_vs_income(column,
max_gross, min_gross,
max_rating, min_rating,
max_count, min_count,
oldest, newest,
acceptable_genres)
plt.show()
def prune_df(data,
max_gross = None, min_gross = None,
max_rating = None, min_rating = None,
max_count = None, min_count = None,
oldest = None, newest = None,
acceptable_genres = None):
#Remove any rows of data not subject to contraints
pruned_data = data
if not max_gross is None:
pruned_data = pruned_data[pruned_data['World Wide Gross'] <= max_gross]
if not min_gross is None:
pruned_data = pruned_data[pruned_data['World Wide Gross'] >= min_gross]
if not max_rating is None:
pruned_data = pruned_data[pruned_data['Ratings'] <= max_rating]
if not min_rating is None:
pruned_data = pruned_data[pruned_data['Ratings'] >= min_rating]
if not max_count is None:
pruned_data = pruned_data[pruned_data['Number of Ratings'] <= max_count]
if not min_count is None:
pruned_data = pruned_data[pruned_data['Number of Ratings'] >= min_count]
if not oldest is None:
i = [(date.year >= oldest) for date in pruned_data['Release Date']]
pruned_data = pruned_data[i]
if not newest is None:
i = [(date.year <= newest) for date in pruned_data['Release Date']]
pruned_data = pruned_data[i]
if not acceptable_genres is None:
i = [any(elem in genre_set.split(' ') for elem in acceptable_genres.split(' ')) for genre_set in pruned_data['Genres']]
pruned_data = pruned_data[i]
return pruned_data