Sleep Cycle of a Newborn Baby

Sleep Cycle of a Newborn Baby

Between my interest in all things data related and my wife’s interest in keeping the baby on a schedule, this project was bound to happen eventually. We chose the Hatch Baby app to track important events (eating, sleeping, and “output”) before Myles was even born. Around the same time, I started tracking my own activity and I thought it might be fun to track the baby’s too.

Concentric rings represent days, with midnight at the top of the circle. Dark blue shading represents sleep, while grey represents awake time. Awake is the default, so some days (weeks 2-3) are erroneously devoid of “sleep” because we were too tired to record!

Images like this one have been floating around on sites like r/dataisbeautiful/, so I can’t claim the idea as my own. However, I still think there’s value in applying the same technique to my own data. The process was fairly straightforward.

  1. Collect data in the Hatch App.
  2. Export from Hatch to .csv file.
  3. Transform time data from to a usable format.
    1. Hatch exported time in a format that Python, R, Excel, and Google Sheets all disliked. The info was there, but it was not processing cleanly.
    2. I used Excel to parse a date/time from the existing data for speed (this was an afternoon project) but if I were going to be replicating the process I would probably have set up a Python function or a Knime flow.
  4. Import transformed .csv to Python with pandas package.
  5. Use matplotlib to place the data on a polar graph.

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
# Import sleep data in .csv format.  This data has already been cleaned.
sleepData = pd.read_csv('sleep.csv')

# Change the resolution of our plot figure to a ridiculously high value
fig = plt.figure(dpi=2000)

# Change the plot to a polar coordinate system
ax = fig.add_subplot(111, polar=True)

# Loop through each of the baby's recorded naps
for nap in sleepData.iterrows():
    # For each nap, draw a grey arc from the start of the nap to the end of the nap.  
    # The arc should be at a distance from the origin that corresponds to the date.
    ax.plot(np.linspace(2*nap[1]['startTime']*np.pi, 2*nap[1]['endTime']*np.pi, 50), np.ones(50)+5*(nap[1]['pyDate']+50), color='lightslategrey', linewidth=.4, linestyle='-')

# Turn off the axes and set the background color.
ax.axes.get_xaxis().set_visible(False)
ax.axes.get_yaxis().set_visible(False)
ax.set(facecolor = "darkblue")

# Show the plot!
plt.show()

Leave a Reply

Your email address will not be published. Required fields are marked *