2

I am a type 1 diabetic and wear a continuous glucose monitor that measures my blood glucose levels every 5 minutes. The company that makes the CGM generates a report with a graph that looks like the figure at the bottom of this post. My goal is to learn how to recreate this graph for myself in a Jupyter notebook.

The data that I have, for example, looks like this:

Timestamp Glucose Value (mg/dL)
2021-07-11 00:11:25 116.0
2021-07-11 00:16:25 118.0
2021-07-11 00:21:25 121.0
2021-07-11 00:26:24 123.0
2021-07-11 00:31:25 124.0

The graph is using data from a 30 day period and summarizing the distribution of values at each point in time. Is there a name for this type of graph, and how can I create it myself using Pandas/matplotlib/seaborn?

So far, I have tried creating a graph with the IQR split by day which is rather easy - using ploty:

glucose['Day'] = glucose['Timestamp'].dt.day_name()

fig = px.box(glucose, x="Day", y="Glucose Value (mg/dL)",
         points="all", color='Day')
fig.show()

But now I am unsure how to easily calculate the IQR for specific time periods and average them.

Thank you so much for your help!

enter image description here

walker967
  • 136
  • 1
  • 10
  • 2
    This question has what you need for the percentile plots: https://stackoverflow.com/questions/47503718/plot-percentiles-using-matplotlib. The Matplotlib documentation will show you how to fill color between curves: https://matplotlib.org/stable/gallery/lines_bars_and_markers/fill_between_demo.html – Joe Jan 31 '22 at 03:04
  • 1
    @Joe Wow thanks a lot for that link! I was able to figure it out by by adapting what was in there to grouping by hour instead of day. – walker967 Feb 01 '22 at 18:38

1 Answers1

2

Answering my own question with help from the links that Joe provided in the comments:

I was able to group the dataframe by hour, then use .quantile to generate a new dataframe with rows as hours and columns as 10%, 25%, 50%, 75%, and 90%. From there it was a matter of simple formatting with matplotlib to copy the original one.

grouped = df.groupby([df['Timestamp'].dt.hour])
i = grouped['bgl'].quantile([.1, .25, .5, .75, .9]).unstack()

Thanks a lot Joe! ambulatory-glucose-profile

walker967
  • 136
  • 1
  • 10