Feb-20-2019, 03:04 PM
(This post was last modified: Feb-21-2019, 06:11 PM by gehrenfeld.)
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.
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
Gary
