-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplot.py
223 lines (155 loc) · 7.49 KB
/
plot.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
# Databricks notebook source
# MAGIC %md
# MAGIC # Plot
# MAGIC
# MAGIC - Monthly validaciones with and without coding 0s
# COMMAND ----------
# Modules
import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# COMMAND ----------
samplesize = "_sample10"
#samplesize = "_sample1"
# COMMAND ----------
pathdb = '/mnt/DAP/data/ColombiaProject-TransMilenioRawData/'
path = '/dbfs/' + pathdb
# COMMAND ----------
cm = pd.read_csv(f'{path}Workspace/Construct/panel_with_treatment{samplesize}.csv')
# COMMAND ----------
# Variables without 0s
cm["n_validaciones_no0s"] = cm["n_validaciones"]
cm.loc[cm.n_validaciones == 0, "n_validaciones_no0s"] = np.nan
cm["n_trips_no0s"] = cm["n_trips"]
cm.loc[cm.n_trips == 0, "n_trips_no0s"] = np.nan
# COMMAND ----------
if samplesize == "_sample10":
samplesize_lab = "10"
if samplesize == "_sample1":
samplesize_lab = "1"
tsdef = ["treatment" ]
tgroup = [ 'gainedhas', 'hadkept', 'hadlost23']
tcolors = [ 'green', '#2986cc', '#cc5199']
for ts in tsdef:
tot_adulto_cards = cm.cardnumber[cm[ts] == 'adulto'].nunique()
# Aggregate by day and profile
monthly = cm.groupby(["ymonth", ts], as_index = False).agg(
{"cardnumber" : "count",
"n_validaciones": "mean",
"n_trips": "mean",
"n_validaciones_no0s": "mean",
"n_trips_no0s": "mean"}
)
for t, tcolor in zip (tgroup, tcolors):
fig, axes = plt.subplots(nrows=1,ncols=1, figsize = (12, 5))
fig.subplots_adjust(hspace = 0.4)
tot_t_cards = cm.cardnumber[cm[ts] == t].nunique()
sns.lineplot(x = monthly.ymonth[monthly[ts] == t] ,
y = monthly.loc [monthly[ts] == t, "n_validaciones_no0s"],
label = f"{t} - total cards: {str(tot_t_cards)} - cond. on travelling" ,
alpha = 0.8,
color = tcolor)
sns.lineplot(x = monthly.ymonth[monthly[ts] == t] ,
y = monthly.loc [monthly[ts] == t, "n_validaciones"],
label = f"coding 0s" ,
alpha = 0.8,
color = tcolor,
linestyle="dashed")
sns.lineplot(x = monthly.ymonth[monthly[ts] == 'adulto'] ,
y = monthly.loc [monthly[ts] == 'adulto', "n_validaciones_no0s"],
label = f'adulto - total cards: {tot_adulto_cards} - cond. on travelling',
alpha = 0.8,
color = "gray")
sns.lineplot(x = monthly.ymonth[monthly[ts] == 'adulto'] ,
y = monthly.loc [monthly[ts] == 'adulto', "n_validaciones"],
label = f'coding 0s',
alpha = 0.8,
color = "gray",
linestyle="dashed")
axes.set_ylim(0, 40)
axes.axvline(x = "2023-02", color ='black')
axes.text("2023-02", 3, 'Policy change')
xticks = plt.gca().get_xticks()
plt.xlabel("Month")
plt.ylabel(f"Validaciones")
if ts == "treatment_v2":
plt.title(f"WHOLEDATA SAMPLE {samplesize_lab}% \n Monthy validaciones by treatment group")
if ts == "treatment_v3":
if t == "gainedhas":
plt.title(f"WHOLEDATA SAMPLE {samplesize_lab}% \n Monthy validaciones by treatment group \n --- GAINED THAT WERE PRESENT BEFORE ---")
else:
plt.title(f"WHOLEDATA SAMPLE {samplesize_lab}% \n Monthy validaciones by treatment group")
if ts == "treatment_v4":
if t == "gainedhas":
plt.title(f"WHOLEDATA SAMPLE {samplesize_lab}% \n Monthy validaciones by treatment group \n --- ADULTO THAT WERE PRESENT BEFORE --- \n --- GAINED THAT WERE PRESENT BEFORE ---")
else:
plt.title(f"WHOLEDATA SAMPLE {samplesize_lab}% \n Monthy validaciones by treatment group \n --- ADULTO THAT WERE PRESENT BEFORE --- ")
if ts == "treatment_v5":
if t == "gainedhas":
plt.title(f"WHOLEDATA SAMPLE {samplesize_lab}% \n Monthy validaciones by treatment group \n --- ADULTO THAT WERE PRESENT BEFORE & AFTER--- \n --- GAINED THAT WERE PRESENT BEFORE ---")
else:
plt.title(f"WHOLEDATA SAMPLE {samplesize_lab}% \n Monthy validaciones by treatment group \n --- ADULTO THAT WERE PRESENT BEFORE & AFTER --- ")
if ts == "treatment_v5":
if t == "gainedhas":
plt.title(f"LINKED DATA \n Monthy validaciones by treatment group \n --- ADULTO THAT WERE PRESENT BEFORE & AFTER--- \n --- GAINED THAT WERE PRESENT BEFORE ---")
else:
plt.title(f"LINKED DATA \n Monthy validaciones by treatment group \n --- ADULTO THAT WERE PRESENT BEFORE & AFTER --- ")
if ts == "treatment_ba_v2":
if t == "gainedhas":
plt.title(f"WHOLEDATA SAMPLE {samplesize_lab}% \n Monthy validaciones by treatment group \n --- ADULTO PRESENT BEFORE & AFTER--- \n --- GAINED PRESENT AFTER ---")
else:
plt.title(f"WHOLEDATA SAMPLE {samplesize_lab}% \n Monthy validaciones by treatment group \n --- CARDS PRESENT BEFORE & AFTER --- ")
if ts == "treatment_ba":
plt.title(f"WHOLEDATA SAMPLE {samplesize_lab}% \n Monthy validaciones by treatment group \n --- CARDS PRESENT BEFORE & AFTER --- ")
plt.legend()
plt.grid()
plt.xticks(xticks[::2])
plt.show()
# COMMAND ----------
if samplesize == "_sample10":
samplesize_lab = "10"
if samplesize == "_sample1":
samplesize_lab = "1"
tsdef = ["treatment" ] # cards present before and after
tgroup = [ 'gainedhas', 'hadkept', 'hadlost23']
tcolors = [ 'green', '#2986cc', '#cc5199']
for ts in tsdef:
tot_adulto_cards = cm.cardnumber[cm[ts] == 'adulto'].nunique()
# Aggregate by day and profile
monthly = cm.groupby(["ymonth", ts], as_index = False).agg(
{"cardnumber" : "count",
"n_validaciones": "mean",
"n_trips": "mean",
"n_validaciones_no0s": "mean",
"n_trips_no0s": "mean"}
)
for t, tcolor in zip (tgroup, tcolors):
fig, axes = plt.subplots(nrows=1,ncols=1, figsize = (12, 5))
fig.subplots_adjust(hspace = 0.4)
tot_t_cards = cm.cardnumber[cm[ts] == t].nunique()
sns.lineplot(x = monthly.ymonth[monthly[ts] == t] ,
y = monthly.loc [monthly[ts] == t, "n_trips"],
label = f"{t} - total cards: {str(tot_t_cards)} " ,
alpha = 0.8,
color = tcolor,
linestyle="dashed")
sns.lineplot(x = monthly.ymonth[monthly[ts] == 'adulto'] ,
y = monthly.loc [monthly[ts] == 'adulto', "n_trips"],
label = f'adulto - total cards: {tot_adulto_cards} ',
alpha = 0.8,
color = "gray",
linestyle="dashed")
axes.set_ylim(0, 40)
axes.axvline(x = "2023-02", color ='black')
axes.text("2023-02", 3, 'Policy change')
xticks = plt.gca().get_xticks()
plt.xlabel("Month")
plt.ylabel(f"Validaciones")
if ts == "treatment_ba":
plt.title(f"WHOLEDATA SAMPLE {samplesize_lab}% - Monthy trips by treatment group -coding 0s ")
plt.legend()
plt.grid()
plt.xticks(xticks[::4])
plt.show()