0

I am writing an API that collects my fitbit data and an extract is shown below. I wanted to ask if anyone knows how to display the x axis as 24 hour time. The program creates a csv and in the file i do have Date and Time fields however could not get it to display on the graph.

(i have deleted the beginning bit of this code but it just contained the import functions and CLIENT_ID and CLIENT_SECRET.)


server = Oauth2.OAuth2Server(CLIENT_ID, CLIENT_SECRET)
server.browser_authorize()
ACCESS_TOKEN = str(server.fitbit.client.session.token['access_token'])
REFRESH_TOKEN = 
str(server.fitbit.client.session.token['refresh_token'])
auth2_client = fitbit.Fitbit(CLIENT_ID, CLIENT_SECRET, oauth2=True, 
access_token=ACCESS_TOKEN, refresh_token=REFRESH_TOKEN)

yesterday = str((datetime.datetime.now() -
datetime.timedelta(days=1)).strftime("%Y%m%d"))
yesterday2 = str((datetime.datetime.now() - 
datetime.timedelta(days=1)).strftime("%Y-%m-%d"))
yesterday3 = str((datetime.datetime.now() - 
datetime.timedelta(days=1)).strftime("%d/%m/%Y"))

today = str(datetime.datetime.now().strftime("%Y%m%d"))

fit_statsHR = auth2_client.intraday_time_series('activities/heart', 
base_date=yesterday2, detail_level='15min')
time_list = []
val_list = []

for i in fit_statsHR['activities-heart-intraday']['dataset']:
    val_list.append(i['value'])
    time_list.append(i['time'])
heartdf = pd.DataFrame({'Heart 
Rate':val_list,'Time':time_list,'Date':yesterday3})


heartdf.to_csv('/Users/zabiullahmohebzadeh/Desktop/python-fitbit-
master/python-fitbit-master/Data/HeartRate - '+ \
           yesterday+'.csv', \
           columns=['Date','Time','Heart Rate'], header=True, \
           index = False)

plt.plot(val_list, 'r-')
plt.ylabel('Heart Rate')
plt.show()
  • 1
    You should include your imports, or at least say what you're using to plot (IE: what is `plt`). I would include an [mcve] and just edit out the CLIENT_ID / SECRET). – Ironcache Feb 12 '18 at 20:01

1 Answers1

1

You can pass your x-values to plt.plot:

plt.plot(time_list, val_list, 'r-')

Without knowing how your time_list is formatted I can't advise on the best way to get it into 24hr time I'm afraid.

ACascarino
  • 4,340
  • 1
  • 13
  • 16
  • Thanks for the quick reply. i tried that but i get an error. The last line says 'ValueError: could not convert string to float: '00:00:00'' time_list should be %Y-%m-%d Everything else works and the graph is displayed. it is just the xaxis that is not showing properly – Zabi Mohebzadeh Feb 12 '18 at 20:00
  • Do you currently understand what that means? What I presume to be the first element in time_list is a string, "00:00:00", which as the interpreter so rightly puts it can't be converted straight into a float, which is what matplotlib requires for its x axes. time_list is not a list of %Y-%m-%d - it appears to be %H:%M:%S. I would suggest reformatting time_list to one of the [acceptable formats of a float-castable string](https://stackoverflow.com/a/20929983/3791827) and then try again. – ACascarino Feb 13 '18 at 20:44