Skip to content

Instantly share code, notes, and snippets.

@sushinoya
Created June 6, 2020 03:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sushinoya/21d5707c6803833bf28eb86ea5e458a6 to your computer and use it in GitHub Desktop.
Save sushinoya/21d5707c6803833bf28eb86ea5e458a6 to your computer and use it in GitHub Desktop.
from pandas import read_csv
from pandas import DataFrame
from pandas import Grouper
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
DATES_FASTED_ON = {"16/4/2020", "17/4/2020", "18/4/2020", "19/4/2020", "20/4/2020", "21/4/2020", "22/4/2020", "8/5/2020", "9/5/2020", "10/5/2020",
"11/5/2020", "18/5/2020", "22/5/2020", "23/5/2020", "24/5/2020", "25/5/2020", "26/5/2020", "27/5/2020", "28/5/2020", "5/6/2020", "6/6/2020",}
plt.rcParams["figure.figsize"] = (60,15)
df = pd.read_csv("Daily Summaries.csv")
last_x_entries = 80
interpolate = True
pull_close = False
if pull_close:
df = df[df["Average weight (kg)"].notnull()]
dates = df["Date"].tail(last_x_entries)
mins = df["Min weight (kg)"].tail(last_x_entries)
means = df["Average weight (kg)"].tail(last_x_entries)
maxs = df["Max weight (kg)"].tail(last_x_entries)
# Must be one of ['linear', 'time', 'index', 'values', 'nearest', 'zero',
# 'slinear', 'quadratic', 'cubic', 'barycentric', 'polynomial', 'krogh',
#'piecewise_polynomial', 'pchip', 'akima', 'spline', 'from_derivatives']
method='linear'
order=1
if interpolate:
mins, means, maxs = mins.interpolate(method=method, order=order), means.interpolate(method=method, order=order), maxs.interpolate(method=method, order=order)
# Finding indexes of dates fasted on
def consecutive(data, stepsize=1):
return np.split(np.array(data), np.where(np.diff(data) != stepsize)[0]+1)
fast_dates_indexes = sorted([np.where(dates == date)[0][0] for date in DATES_FASTED_ON])
fasting_sections = [(min(group), max(group)) for group in consecutive(fast_dates_indexes)]
fig, ax = plt.subplots()
# Highlight fasting indexes
for start_date_index, end_date_index in fasting_sections:
if start_date_index == end_date_index:
start_date_index -= 0.20
end_date_index += 0.20
ax.axvspan(start_date_index, end_date_index, alpha=0.2, color='lightgreen')
plt.plot(dates, mins, 'o-', color='g', label='Min')
plt.plot(dates, maxs, 'o-', color='r', label='Max')
plt.plot(dates, means, 'o-', color='b', label='Mean')
plt.fill_between(dates, mins, maxs, alpha=0.1)
plt.xlabel('Date')
plt.ylabel('Weight (kg)')
plt.savefig('weight.png')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment