-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_vis.py
316 lines (272 loc) · 12.2 KB
/
data_vis.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import pandas as p
import os
import matplotlib.pylab as plt
import numpy as np
os.chdir("C:/github_repos/Radinfrastruktur-analytics")
class Plotter():
def plotBar(self, x, y, title, color=(0.2,0.4,0.9,1), plot_y=False, line_width=1, rotate_x=False, fully_rotate_x=False, margin=0.5, height_scale=1.1):
fig = plt.figure(figsize=(1.1*6.25984252,height_scale*3.12992126))
ax1 = fig.add_subplot(1, 1, 1)
ax1.axhline(np.mean(y), color='black', linewidth=1.4)
print(title)
print("Durchschnitt: "+str(np.mean(y)))
print("Max: "+str(max(y)))
print("Min: "+str(min(y)))
plt.bar(x, y, line_width, color=color)
ax1.grid(which='major', axis='y', linewidth=0.71, linestyle='-', color='0.75')
ax1.set_axisbelow(True)
plt.title(title)
if plot_y is not False:
h=plt.ylabel(plot_y)
fig.tight_layout()
if rotate_x:
plt.xticks(rotation=45, ha="right")
plt.subplots_adjust(bottom=margin)
if fully_rotate_x:
plt.xticks(rotation=90, ha="center")
plt.subplots_adjust(bottom=margin)
plt.show()
def plotDoubleBar(self, x, y1, y2, title, labels, color=(0.2,0.4,0.9,1), plot_y=False, line_width=1, rotate_x=False, fully_rotate_x=False, margin=0.5, height_scale=1.1):
fig = plt.figure(figsize=(1.1*6.25984252,height_scale*3.12992126))
ax1 = fig.add_subplot(1, 1, 1)
rects1 = ax1.bar( np.arange(len(x)) - line_width/2, y1, line_width, label=labels[0], color=(0.2,0.4,0.9,1))
rects2 = ax1.bar(np.arange(len(x)) + line_width/2, y2, line_width, label=labels[1], color="#e69900")
print(title)
ax1.grid(which='major', axis='y', linewidth=0.71, linestyle='-', color='0.75')
ax1.set_axisbelow(True)
plt.title(title)
ax1.set_xticks(range(len(x)))
ax1.set_xticklabels(x)
ax1.legend()
if plot_y is not False:
h=plt.ylabel(plot_y)
fig.tight_layout()
if rotate_x:
plt.xticks(rotation=45, ha="right")
plt.subplots_adjust(bottom=margin)
if fully_rotate_x:
plt.xticks(rotation=90, ha="center")
plt.subplots_adjust(bottom=margin)
plt.show()
pl = Plotter()
class Reader():
def __init__(self, filename):
self.data=p.read_csv(filename, sep=";")
print(self.data)
def getCities(self):
cities_for = [{'city': 'Barcelona', 'state': 'Catalonia', 'country': 'Spain'},
{'city': 'Utrecht', 'country': 'Netherlands'},
{'city': 'Antwerpen', 'country': 'Belgien'},
{'city': 'Montreal', 'country': 'Canada'},
{'city': 'New York City', 'country': 'USA'}
]
cities_bike=[{'city': 'Karlsruhe', 'country': 'Deutschland'},
{'city': 'Münster', 'country': 'Deutschland'},
{'city': 'Erlangen', 'country': 'Deutschland'},
{'city': 'Freiburg im Breisgau', 'country': 'Deutschland'},
{'city': 'Heidelberg', 'country': 'Deutschland'},
{'city': 'Kiel', 'country': 'Deutschland'},
{'city': 'Greifswald', 'country': 'Deutschland'},
{'city': 'München', 'country': 'Deutschland'},
{'city': 'Bonn', 'country': 'Deutschland'}
]
cities_stand = [
{'city': 'Stuttgart', 'country': 'Deutschland'},
{'city': 'Berlin', 'country': 'Deutschland'},
{'city': 'Hamburg', 'country': 'Deutschland'},
{'city': 'Leipzig', 'country': 'Deutschland'},
{'city': 'Bremen', 'country': 'Deutschland'},
{'city': 'Dresden', 'country': 'Deutschland'},
{'city': 'Düsseldorf', 'country': 'Deutschland'},
{'city': 'Erfurt', 'country': 'Deutschland'},
{'city': 'Hannover', 'country': 'Deutschland'},
{'city': 'Magdeburg', 'country': 'Deutschland'},
{'city': 'Mainz', 'country': 'Deutschland'},
{'city': 'Potsdam', 'country': 'Deutschland'},
{'city': 'Saarbrücken', 'country': 'Deutschland'},
{'city': 'Schwerin', 'country': 'Deutschland'},
{'city': 'Wiesbaden', 'country': 'Deutschland'}
]
c_foreign=[]
c_bike=[]
c_standard=[]
for c in cities_for:
c_foreign.append(c["city"])
for c in cities_bike:
c_bike.append(c["city"])
for c in cities_stand:
c_standard.append(c["city"])
return c_foreign, c_bike, c_standard
def calcFeatures(self, bike, car):
ret={}
try:
ret["r_total"]=float(bike["Länge total in m"])/float(car["Länge total in m"])
ret["r_single"]=float(bike["Länge einzeln in m"])/float(car["Länge einzeln in m"])
ret["r_avrg"]=float(bike["Durchschn. Strassenlänge in m"])/float(car["Durchschn. Strassenlänge in m"])
except:
print(bike)
print(car)
return ret
def getData(self):
cf, cb, cs = self.getCities()
y_1 = {}
y_2 = {}
y_3 = {}
for city in cf+cb+cs:
bike = self.data.loc[self.data['Stadt'] == city+", Bike"]
car = self.data.loc[self.data['Stadt'] == city+", Car"]
ret = self.calcFeatures(bike, car)
y_1[city] = ret["r_total"]
y_2[city] = ret["r_single"]
y_3[city] = ret["r_avrg"]
return y_1, y_2, y_3, self.data
r = Reader("data/cities.csv")
class Statistics():
def byRatio(self):
y1, y2, y3, _ = r.getData()
cf, cb, cs = r.getCities()
colors = []
y1={k: v for k, v in sorted(y1.items(), key=lambda item: item[1])}
for key in y1:
if key in cf:
colors.append((0.9,0.6,0,1))
elif key in cb:
colors.append((0.2,0.4,0.9,1))
elif key in cs:
colors.append((0,0.7,0.1,1))
else:
colors.append((1,1,1,1))
pl.plotBar(y1.keys(),list(y1.values()), "Verhältnis der Kantenlänge - Rad zu Auto", plot_y="r_total [-]", rotate_x=True, color=colors, line_width=0.8, margin=0.4)
colors = []
y2={k: v for k, v in sorted(y2.items(), key=lambda item: item[1])}
for key in y2:
if key in cf:
colors.append((0.9,0.6,0,1))
elif key in cb:
colors.append((0.2,0.4,0.9,1))
elif key in cs:
colors.append((0,0.7,0.1,1))
else:
colors.append((1,1,1,1))
pl.plotBar(y2.keys(),list(y2.values()), "Verhältnis der Strassenlänge - Rad zu Auto", plot_y="r_single [-]", rotate_x=True, color=colors, line_width=0.8, margin=0.4)
colors = []
y3={k: v for k, v in sorted(y3.items(), key=lambda item: item[1])}
for key in y3:
if key in cf:
colors.append((0.9,0.6,0,1))
elif key in cb:
colors.append((0.2,0.4,0.9,1))
elif key in cs:
colors.append((0,0.7,0.1,1))
else:
colors.append((1,1,1,1))
pl.plotBar(y3.keys(),list(y3.values()), "Verhältnis der durchschn. Strassenlänge - Rad zu Auto", plot_y="r_avrg [-]", rotate_x=True, color=colors, line_width=0.8, margin=0.4)
def byDensity(self):
cf, cb, cs = r.getCities()
data = r.data
y1 = {}
for city in cf+cb+cs:
bike = data.loc[data['Stadt'] == city+", Bike"]
y1[city]=float(bike["Strassendichte m/km2"])
colors = []
y1={k: v for k, v in sorted(y1.items(), key=lambda item: item[1])}
for key in y1:
if key in cf:
colors.append((0.9,0.6,0,1))
elif key in cb:
colors.append((0.2,0.4,0.9,1))
elif key in cs:
colors.append((0,0.7,0.1,1))
else:
colors.append((1,1,1,1))
pl.plotBar(y1.keys(),list(y1.values()), "Strassendichte - Rad", plot_y="d_edge [m/km^2]", rotate_x=True, color=colors, line_width=0.8, margin=0.4)
for city in cf+cb+cs:
car = data.loc[data['Stadt'] == city+", Car"]
y1[city]=float(car["Strassendichte m/km2"])
colors = []
y1={k: v for k, v in sorted(y1.items(), key=lambda item: item[1])}
for key in y1:
if key in cf:
colors.append((0.9,0.6,0,1))
elif key in cb:
colors.append((0.2,0.4,0.9,1))
elif key in cs:
colors.append((0,0.7,0.1,1))
else:
colors.append((1,1,1,1))
pl.plotBar(y1.keys(),list(y1.values()), "Strassendichte - Auto", plot_y="d_edge [m/km^2]", rotate_x=True, color=colors, line_width=0.8, margin=0.4)
def byDensity2(self):
cf, cb, cs = r.getCities()
data = r.data
y1 = {}
for city in cf+cb+cs:
bike = data.loc[data['Stadt'] == city+", Bike"]
y1[city]=float(bike["Kreuzungsdichte 1/km2"])
colors = []
y1={k: v for k, v in sorted(y1.items(), key=lambda item: item[1])}
for key in y1:
if key in cf:
colors.append((0.9,0.6,0,1))
elif key in cb:
colors.append((0.2,0.4,0.9,1))
elif key in cs:
colors.append((0,0.7,0.1,1))
else:
colors.append((1,1,1,1))
pl.plotBar(y1.keys(),list(y1.values()), "Kreuzungsdichte - Rad", plot_y="d_edge [m/km^2]", rotate_x=True, color=colors, line_width=0.8, margin=0.4)
for city in cf+cb+cs:
car = data.loc[data['Stadt'] == city+", Car"]
y1[city]=float(car["Kreuzungsdichte 1/km2"])
colors = []
y1={k: v for k, v in sorted(y1.items(), key=lambda item: item[1])}
for key in y1:
if key in cf:
colors.append((0.9,0.6,0,1))
elif key in cb:
colors.append((0.2,0.4,0.9,1))
elif key in cs:
colors.append((0,0.7,0.1,1))
else:
colors.append((1,1,1,1))
pl.plotBar(y1.keys(),list(y1.values()), "Kreuzungsdichte - Auto", plot_y="d_edge [m/km^2]", rotate_x=True, color=colors, line_width=0.8, margin=0.4)
def byTotalLength(self):
data=r.data
cf, cb, cs = r.getCities()
y1=[]
y2={}
for city in cf+cb+cs:
car = data.loc[data['Stadt'] == city+", Car"]
y2[city]=float(car["Länge total in m"])/1000
y2={k: v for k, v in sorted(y2.items(), key=lambda item: item[1])}
for city in y2.keys():
bike = data.loc[data['Stadt'] == city+", Bike"]
y1.append(float(bike["Länge total in m"])/1000)
pl.plotDoubleBar(y2.keys(),list(y2.values()), y1, "Ungerichtete Kantenlänge", ["Auto", "Rad"], plot_y="l_total [km]", rotate_x=True, line_width=0.4, margin=0.4)
def byBikeLength(self):
data=r.data
cf, cb, cs = r.getCities()
y1=[]
y2={}
for city in cf+cb+cs:
bike = data.loc[data['Stadt'] == city+", Bike"]
y2[city]=float(bike["Länge total in m"])/1000
y2={k: v for k, v in sorted(y2.items(), key=lambda item: item[1])}
for city in y2.keys():
bike = data.loc[data['Stadt'] == city+", Bike"]
y1.append(float(bike["Länge einzeln in m"])/1000)
pl.plotDoubleBar(y2.keys(),list(y2.values()), y1, "Kanten- und Strassenlänge", ["Kantenlänge", "Strassenlänge"], plot_y="l [km]", rotate_x=True, line_width=0.4, margin=0.4)
def byCarLength(self):
data=r.data
cf, cb, cs = r.getCities()
y1=[]
y2={}
for city in cf+cb+cs:
bike = data.loc[data['Stadt'] == city+", Car"]
y2[city]=float(bike["Länge total in m"])/1000
y2={k: v for k, v in sorted(y2.items(), key=lambda item: item[1])}
for city in y2.keys():
bike = data.loc[data['Stadt'] == city+", Car"]
y1.append(float(bike["Länge einzeln in m"])/1000)
pl.plotDoubleBar(y2.keys(),list(y2.values()), y1, "Kanten- und Strassenlänge", ["Kantenlänge", "Strassenlänge"], plot_y="l [km]", rotate_x=True, line_width=0.4, margin=0.4)
s = Statistics()
s.byDensity2()