Plotting Matplotlib Grid Lines - gehrenfeld - Feb-20-2019
The below script works but I would like to add x minor grid lines.
I am new to Matplotlib so I also fill like I am not sure I am doing everything correctly.
Would someone kindly show me how to get minor x-axis grid lines and if there is a better way to do the plot?
The data is from my glucose meter and contains date as YYYY/MM/DD HH:MM:SS. For three days there are around 500 lines.
I would like to display just the time as HH:MM on the y-axis but can't figure that out either.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# --------------------------------- read from csv ---------------------------
df = pd.read_csv('summary.csv', delimiter='\t', skiprows=3, header=None, names=[
'id', 'rdate', 'rtype', 'hist', 'scan'], usecols=[0, 1, 2, 3, 4])
# ------------------------ Get last 3 days --------------------------------------
df.tail(600)
# ---------- Fill in 0 ---------------------------
df['hist'] = df['hist'].fillna(0)
df['scan'] = df['scan'].fillna(0)
# -------------- Change dtypes -------------------
df['rdate'] = df['rdate'].astype('datetime64')
df['hist'] = df['hist'].astype('int64')
df['scan'] = df['scan'].astype('int64')
# ------------------------------ Replace nil hist with scan ---------------------------------
df.loc[df['hist'] == 0, 'hist'] = df['scan']
# ------------------------------------- Select Record Type = 0 -----------------------------
sd = (df[df.rtype == 0])
# --------------------------------- Get 3 Days of Data -----------------------
sr = (sd[sd.rdate >= pd.Timestamp.today() - pd.to_timedelta("3day")])
# ------------------------------------- Plot Chart -------------------------
def displayplot(time, glucose):
fig, ax = plt.subplots()
plt.plot(time, glucose, color='k', marker='.', label='Glucose', linewidth=1,
markevery=1, markerfacecolor='blue')
ax.grid()
ax.tick_params(axis='y')
fig.autofmt_xdate()
plt.axhspan(70, 120, facecolor='lightgreen', alpha=0.5)
plt.axhspan(120, 180, facecolor='lightblue', alpha=0.5)
plt.axhspan(180, 310, facecolor='#EF9494', alpha=0.5)
plt.axhspan(0, 70, facecolor='#EF9494', alpha=0.5)
plt.title('Daily Glucose Chart')
plt.ylabel('Glucose')
plt.xlabel('Time')
plt.legend(loc=2)
plt.yticks(np.arange(0, 310, 10.0))
plt.tight_layout()
plt.show()
# ---------------------------------------------------------------------------------------------------------
time = []
glucose = []
for i, row in sr.iterrows():
time.append(row.rdate)
glucose.append(row['hist'])
displayplot(time, glucose) Thanks
RE: Plotting Matplotlib Grid Lines - Larz60+ - Feb-20-2019
Hate to reference other forums, but the answer is here: https://stackoverflow.com/a/9149619
RE: Plotting Matplotlib Grid Lines - gehrenfeld - Feb-21-2019
I did that and for some reason didn't work.
Was hoping someone would see why it didn't work.
RE: Plotting Matplotlib Grid Lines - Larz60+ - Feb-21-2019
In order to test here, you need to attach summary.csv
RE: Plotting Matplotlib Grid Lines - gehrenfeld - Feb-21-2019
Output: Gary Ehrenfeld
ID Time Record Type Historic Glucose (mg/dL) Scan Glucose (mg/dL) Non-numeric Rapid-Acting Insulin Rapid-Acting Insulin (units) Non-numeric Food Carbohydrates (grams) Non-numeric Long-Acting Insulin Long-Acting Insulin (units) Notes Strip Glucose (mg/dL) Ketone (mmol/L) N/A N/A N/A Previous Time Updated Time
89 2019/02/15 13:42 1 137
91 2019/02/15 13:33 0 143
92 2019/02/15 13:48 0 145
93 2019/02/15 14:03 0 137
94 2019/02/15 14:18 0 128
95 2019/02/15 14:33 0 120
96 2019/02/15 14:48 0 115
97 2019/02/15 15:03 0 118
98 2019/02/15 15:18 0 122
99 2019/02/15 15:33 0 117
100 2019/02/15 15:48 0 110
101 2019/02/15 16:03 0 103
102 2019/02/15 16:29 1 86
104 2019/02/15 16:20 0 93
105 2019/02/15 16:35 0 87
106 2019/02/15 16:50 0 87
107 2019/02/15 17:05 0 114
108 2019/02/15 17:20 0 143
109 2019/02/15 17:35 0 153
110 2019/02/15 17:50 0 141
111 2019/02/15 18:05 0 131
112 2019/02/15 18:20 0 139
113 2019/02/15 18:35 0 143
114 2019/02/15 18:50 0 143
115 2019/02/15 19:05 0 149
116 2019/02/15 19:20 0 137
117 2019/02/15 19:35 1 148
119 2019/02/15 19:35 0 143
120 2019/02/15 19:50 0 130
121 2019/02/15 20:05 0 121
122 2019/02/15 20:20 0 124
123 2019/02/15 20:35 0 128
124 2019/02/15 21:03 1 147
126 2019/02/15 20:53 0 136
127 2019/02/15 21:08 0 145
128 2019/02/15 21:23 0 152
129 2019/02/15 21:38 0 160
130 2019/02/15 21:53 0 170
131 2019/02/15 22:08 0 176
132 2019/02/15 22:23 0 175
133 2019/02/15 22:38 0 174
134 2019/02/15 22:53 0 178
135 2019/02/15 23:08 0 189
136 2019/02/15 23:23 0 185
137 2019/02/15 23:38 0 167
138 2019/02/15 23:53 0 138
139 2019/02/16 00:08 0 111
140 2019/02/16 00:23 0 118
141 2019/02/16 00:38 0 128
142 2019/02/16 00:53 0 119
143 2019/02/16 01:08 0 120
144 2019/02/16 01:23 0 128
145 2019/02/16 01:38 0 128
146 2019/02/16 01:53 0 123
147 2019/02/16 02:08 0 115
148 2019/02/16 02:23 0 110
149 2019/02/16 02:38 0 106
150 2019/02/16 02:56 1 104
152 2019/02/16 02:54 0 101
153 2019/02/16 03:09 0 103
154 2019/02/16 03:24 0 112
155 2019/02/16 03:39 0 116
156 2019/02/16 03:54 0 118
157 2019/02/16 04:09 0 123
158 2019/02/16 04:24 0 126
159 2019/02/16 04:39 0 128
160 2019/02/16 04:54 0 131
161 2019/02/16 05:09 0 132
162 2019/02/16 05:24 0 131
163 2019/02/16 05:39 0 127
164 2019/02/16 06:01 1 127
166 2019/02/16 05:55 0 127
167 2019/02/16 06:10 0 127
168 2019/02/16 06:25 0 125
169 2019/02/16 06:40 0 121
170 2019/02/16 06:55 0 118
171 2019/02/16 07:10 0 115
172 2019/02/16 07:25 0 113
173 2019/02/16 07:49 5 1
173 2019/02/16 07:49 1 117 Medication
175 2019/02/16 07:41 0 113
176 2019/02/16 07:56 0 115
177 2019/02/16 08:11 0 126
178 2019/02/16 08:26 0 154
179 2019/02/16 08:41 0 183
180 2019/02/16 08:56 0 184
181 2019/02/16 09:11 0 173
182 2019/02/16 09:26 0 172
183 2019/02/16 09:41 0 167
184 2019/02/16 09:56 0 156
185 2019/02/16 10:11 0 148
186 2019/02/16 10:26 0 145
187 2019/02/16 10:41 0 142
188 2019/02/16 10:56 0 132
189 2019/02/16 11:11 0 122
190 2019/02/16 11:32 1 124
192 2019/02/16 11:29 0 117
193 2019/02/16 11:44 0 118
194 2019/02/16 11:59 0 119
195 2019/02/16 12:14 0 112
196 2019/02/16 12:29 0 110
197 2019/02/16 12:44 0 120
198 2019/02/16 12:59 0 127
199 2019/02/16 13:14 0 128
200 2019/02/16 13:29 0 133
201 2019/02/16 13:44 0 142
202 2019/02/16 13:59 0 147
203 2019/02/16 14:14 0 147
204 2019/02/16 14:29 0 145
205 2019/02/16 14:44 0 142
206 2019/02/16 14:59 0 140
207 2019/02/16 15:14 0 139
208 2019/02/16 15:29 0 139
209 2019/02/16 15:44 0 138
210 2019/02/16 15:59 0 136
211 2019/02/16 16:14 0 138
212 2019/02/16 16:29 0 138
213 2019/02/16 16:44 0 137
214 2019/02/16 17:01 1 146
216 2019/02/16 20:19 0 162
217 2019/02/16 20:34 0 155
218 2019/02/16 20:49 0 149
219 2019/02/16 21:04 0 138
220 2019/02/16 21:19 0 123
221 2019/02/16 21:34 0 110
222 2019/02/16 21:49 0 116
223 2019/02/16 22:04 0 140
224 2019/02/16 22:19 0 163
225 2019/02/16 22:34 0 167
226 2019/02/16 22:49 0 174
227 2019/02/16 23:04 0 201
228 2019/02/16 23:19 0 186
229 2019/02/16 23:34 0 156
230 2019/02/16 23:49 0 145
231 2019/02/17 00:04 0 130
232 2019/02/17 00:19 0 116
233 2019/02/17 00:34 0 110
234 2019/02/17 00:49 0 112
235 2019/02/17 01:04 0 116
236 2019/02/17 01:19 0 116
237 2019/02/17 01:34 0 114
238 2019/02/17 01:49 0 103
239 2019/02/17 02:04 0 91
240 2019/02/17 02:19 0 95
241 2019/02/17 02:34 0 93
242 2019/02/17 02:49 0 89
243 2019/02/17 03:04 0 97
244 2019/02/17 03:19 0 105
245 2019/02/17 03:34 0 105
246 2019/02/17 03:49 0 105
247 2019/02/17 04:14 1 123
249 2019/02/17 04:04 0 116
250 2019/02/17 04:19 0 119
251 2019/02/17 04:34 0 122
252 2019/02/17 04:49 0 127
253 2019/02/17 05:04 0 128
254 2019/02/17 05:19 0 128
255 2019/02/17 05:34 0 129
256 2019/02/17 06:01 4 50.0
256 2019/02/17 06:01 1 137
258 2019/02/17 05:52 0 132
259 2019/02/17 06:07 0 134
260 2019/02/17 06:22 0 134
261 2019/02/17 06:37 0 133
262 2019/02/17 06:52 0 129
263 2019/02/17 07:07 0 123
264 2019/02/17 07:22 0 117
265 2019/02/17 07:37 0 111
266 2019/02/17 07:52 0 111
267 2019/02/17 08:07 0 110
268 2019/02/17 08:22 0 125
269 2019/02/17 08:37 0 162
270 2019/02/17 08:52 0 186
271 2019/02/17 09:07 0 183
272 2019/02/17 09:22 0 174
273 2019/02/17 09:37 0 171
274 2019/02/17 09:52 0 167
275 2019/02/17 10:07 0 161
276 2019/02/17 10:22 0 147
277 2019/02/17 10:37 0 140
278 2019/02/17 10:52 0 141
279 2019/02/17 11:07 0 133
280 2019/02/17 11:35 1 127
282 2019/02/17 11:25 0 128
283 2019/02/17 11:40 0 118
284 2019/02/17 11:55 0 111
285 2019/02/17 12:10 0 106
286 2019/02/17 12:25 0 102
287 2019/02/17 12:40 0 98
288 2019/02/17 12:55 0 91
289 2019/02/17 13:10 0 82
290 2019/02/17 13:25 0 79
291 2019/02/17 13:40 0 91
292 2019/02/17 13:55 0 123
293 2019/02/17 14:10 0 154
294 2019/02/17 14:25 0 167
295 2019/02/17 14:40 0 175
296 2019/02/17 14:55 0 185
297 2019/02/17 15:10 0 184
298 2019/02/17 15:25 0 181
299 2019/02/17 15:40 0 172
300 2019/02/17 15:55 0 157
301 2019/02/17 16:10 0 138
302 2019/02/17 16:25 0 124
303 2019/02/17 16:40 0 114
304 2019/02/17 16:55 0 101
305 2019/02/17 17:10 0 87
306 2019/02/17 17:25 0 74
307 2019/02/17 17:40 0 66
308 2019/02/17 18:02 5 1
308 2019/02/17 18:02 1 63
310 2019/02/17 17:57 0 58
311 2019/02/17 18:12 0 58
312 2019/02/17 18:27 0 79
313 2019/02/17 18:42 0 121
314 2019/02/17 18:57 0 141
315 2019/02/17 19:12 0 138
316 2019/02/17 19:27 0 137
317 2019/02/17 19:42 0 148
318 2019/02/17 19:57 0 164
319 2019/02/17 20:12 0 169
320 2019/02/17 20:27 0 159
321 2019/02/17 20:42 0 146
322 2019/02/17 20:57 0 138
323 2019/02/17 21:12 0 140
324 2019/02/17 21:27 0 154
325 2019/02/17 21:53 1 183
327 2019/02/17 21:45 0 183
328 2019/02/17 22:00 0 183
329 2019/02/17 22:15 0 182
330 2019/02/17 22:30 0 186
331 2019/02/17 22:45 0 197
332 2019/02/17 23:00 0 205
333 2019/02/17 23:15 0 202
334 2019/02/17 23:30 0 191
335 2019/02/17 23:45 0 179
336 2019/02/18 00:00 0 166
337 2019/02/18 00:15 0 143
338 2019/02/18 00:30 0 124
339 2019/02/18 00:45 0 121
340 2019/02/18 01:00 0 131
341 2019/02/18 01:15 0 136
342 2019/02/18 01:30 0 129
343 2019/02/18 01:45 0 120
344 2019/02/18 02:00 0 113
345 2019/02/18 02:15 0 106
346 2019/02/18 02:30 0 101
347 2019/02/18 02:45 0 95
348 2019/02/18 03:00 0 91
349 2019/02/18 03:15 0 90
350 2019/02/18 03:30 0 88
351 2019/02/18 03:54 1 100
353 2019/02/18 03:46 0 94
354 2019/02/18 04:01 0 98
355 2019/02/18 04:16 0 103
356 2019/02/18 04:31 0 109
357 2019/02/18 04:46 0 115
358 2019/02/18 05:01 0 119
359 2019/02/18 05:16 0 121
360 2019/02/18 05:31 0 123
361 2019/02/18 05:54 4 50.0
361 2019/02/18 05:54 1 124
363 2019/02/18 07:05 0 140
364 2019/02/18 07:20 0 155
365 2019/02/18 07:35 0 165
366 2019/02/18 07:50 0 175
367 2019/02/18 08:05 0 169
368 2019/02/18 08:20 0 153
369 2019/02/18 08:35 0 154
370 2019/02/18 08:50 0 171
371 2019/02/18 09:05 0 171
372 2019/02/18 09:20 0 167
373 2019/02/18 09:35 0 174
374 2019/02/18 09:50 0 181
375 2019/02/18 10:05 0 192
376 2019/02/18 10:20 0 189
377 2019/02/18 10:35 0 185
378 2019/02/18 10:50 0 188
379 2019/02/18 11:05 0 189
380 2019/02/18 11:20 0 178
381 2019/02/18 11:35 0 156
382 2019/02/18 11:50 0 141
383 2019/02/18 12:05 0 132
384 2019/02/18 12:20 0 114
385 2019/02/18 12:35 0 103
386 2019/02/18 12:50 0 99
387 2019/02/18 13:05 0 108
388 2019/02/18 13:20 0 126
389 2019/02/18 13:35 0 139
390 2019/02/18 13:50 0 146
391 2019/02/18 14:05 0 151
392 2019/02/18 14:20 0 151
393 2019/02/18 14:35 0 149
394 2019/02/18 15:01 1 140
396 2019/02/18 20:10 0 191
397 2019/02/18 20:25 0 200
398 2019/02/18 20:40 0 210
399 2019/02/18 20:55 0 207
400 2019/02/18 21:10 0 191
401 2019/02/18 21:25 0 173
402 2019/02/18 21:40 0 161
403 2019/02/18 21:55 0 164
404 2019/02/18 22:10 0 180
405 2019/02/18 22:25 0 195
406 2019/02/18 22:40 0 203
407 2019/02/18 22:55 0 206
408 2019/02/18 23:10 0 204
409 2019/02/18 23:25 0 198
410 2019/02/18 23:40 0 187
411 2019/02/18 23:55 0 175
412 2019/02/19 00:10 0 156
413 2019/02/19 00:25 0 134
414 2019/02/19 00:40 0 114
415 2019/02/19 00:55 0 106
416 2019/02/19 01:10 0 114
417 2019/02/19 01:25 0 123
418 2019/02/19 01:40 0 122
419 2019/02/19 01:55 0 109
420 2019/02/19 02:10 0 113
421 2019/02/19 02:25 0 125
422 2019/02/19 02:40 0 126
423 2019/02/19 02:55 0 121
424 2019/02/19 03:10 0 116
425 2019/02/19 03:25 0 111
426 2019/02/19 03:40 0 109
427 2019/02/19 03:58 1 134
429 2019/02/19 03:57 0 118
430 2019/02/19 04:12 0 121
431 2019/02/19 04:27 0 124
432 2019/02/19 04:42 0 124
433 2019/02/19 04:57 0 128
434 2019/02/19 05:12 0 131
435 2019/02/19 05:27 0 131
436 2019/02/19 05:42 0 131
437 2019/02/19 06:01 4 50.0
437 2019/02/19 06:01 1 134
439 2019/02/19 13:34 0 149
440 2019/02/19 13:49 0 150
441 2019/02/19 14:04 0 152
442 2019/02/19 14:19 0 156
443 2019/02/19 14:34 0 154
444 2019/02/19 14:49 0 140
445 2019/02/19 15:04 0 127
446 2019/02/19 15:19 0 128
447 2019/02/19 15:34 0 136
448 2019/02/19 15:49 0 140
449 2019/02/19 16:04 0 138
450 2019/02/19 16:19 0 131
451 2019/02/19 16:34 0 129
452 2019/02/19 16:49 0 132
453 2019/02/19 17:04 0 126
454 2019/02/19 17:19 0 108
455 2019/02/19 17:34 0 90
456 2019/02/19 17:49 0 85
457 2019/02/19 18:04 0 98
458 2019/02/19 18:19 0 115
459 2019/02/19 18:34 0 122
460 2019/02/19 18:49 0 113
461 2019/02/19 19:04 0 102
462 2019/02/19 19:19 0 106
463 2019/02/19 19:34 0 118
464 2019/02/19 19:49 0 124
465 2019/02/19 20:04 0 132
466 2019/02/19 20:19 0 142
467 2019/02/19 20:34 0 152
468 2019/02/19 20:49 0 163
469 2019/02/19 21:04 0 169
470 2019/02/19 21:24 1 174
472 2019/02/19 21:22 0 180
473 2019/02/19 21:37 0 183
474 2019/02/19 21:52 0 179
475 2019/02/19 22:07 0 179
476 2019/02/19 22:22 0 181
477 2019/02/19 22:37 0 187
478 2019/02/19 22:52 0 190
479 2019/02/19 23:07 0 193
480 2019/02/19 23:22 0 183
481 2019/02/19 23:37 0 161
482 2019/02/19 23:52 0 142
483 2019/02/20 00:07 0 130
484 2019/02/20 00:22 0 122
485 2019/02/20 00:37 0 114
486 2019/02/20 00:52 0 105
487 2019/02/20 01:07 0 101
488 2019/02/20 01:22 0 102
489 2019/02/20 01:37 0 99
490 2019/02/20 01:52 0 94
491 2019/02/20 02:07 0 97
492 2019/02/20 02:22 0 99
493 2019/02/20 02:37 0 95
494 2019/02/20 02:52 0 93
495 2019/02/20 03:07 0 97
496 2019/02/20 03:22 0 99
497 2019/02/20 03:37 0 98
498 2019/02/20 03:52 0 97
499 2019/02/20 04:07 1 111
501 2019/02/20 04:07 0 104
502 2019/02/20 04:22 0 106
503 2019/02/20 04:37 0 109
504 2019/02/20 04:52 0 115
505 2019/02/20 05:07 0 121
506 2019/02/20 05:22 0 127
507 2019/02/20 05:37 0 131
508 2019/02/20 06:01 4 50.0
508 2019/02/20 06:01 1 131
RE: Plotting Matplotlib Grid Lines - Larz60+ - Feb-21-2019
That display has some odd tabbing going on.
Please just attach the .csv file, not display it
something might be lost otherwise.
RE: Plotting Matplotlib Grid Lines - gehrenfeld - Feb-21-2019
Not sure I am doing this right. I added the file but not sure if I should insert into post. But here it goes.[attachment=570]
RE: Plotting Matplotlib Grid Lines - Larz60+ - Feb-21-2019
You got it.
I have to go out, and will be gone until this evening (EST time zone). If someone hasn't answered your question by then, I'll pick up when I get back.
RE: Plotting Matplotlib Grid Lines - gehrenfeld - Feb-21-2019
Thank you. I had to change one line in the code because I forgot it was still there. All is correct now.
RE: Plotting Matplotlib Grid Lines - Larz60+ - Feb-22-2019
I haven't gotten past the Pandas load of the CSV file. I'm not a big user of Pandas, so not sure what's going on (haven't looked at the docs yet, but plan on getting to it this afternoon). The CSV loads into spreadsheet without a hitch, so there's nothing wrong with the file, I just need to see what Pandas is looking for.
|