0

I am trying to make a program to track how many situps and pushups I do every workout and also have it plot the results. I got the tracking part done, but matplotlib is giving me some issues. I have it set to take the last 7 items from the list that contains all my workout data, then plot it with the date as the x axis and situps or pushups on the y. Here is that code:

choice3 = input("Enter stat to be plotted, situps or pushups: ")
x = 1
situplist = []
pushuplist = []
while x <= 7:
    datelist = list(user_dict['users'][user].keys())[-x]
    str_datelist = str(datelist).split('-')
    month = str(str_datelist[1])
    day = str(str_datelist[2])
    datestring += month + '-' + day + ', '
    situps = user_dict['users'][user][datelist]['Situps']
    situplist.append(situps)
    pushups = user_dict['users'][user][datelist]['Pushups']
    pushuplist.append(pushups)

    x += 1
split_datestring = datestring.split(', ', 7)
split_datestring.remove('')
split_datestring.reverse()
situplist.reverse()
pushuplist.reverse()
if choice3.upper() == 'SITUPS':
    plt.plot(split_datestring, situplist)
    plt.xlabel("DATE")
    plt.ylabel(choice3.upper())
    plt.show()

The last 7 situps values are as follows: 30, 20, 0, 50, 30, 0, 30

matplotlib plots it like this though: Plot of situp data

Obviously, I'm wanting the y axis to start at 0, but even when I add plt.axis([split_datestring[0], split_datestring[-1], 0, 50]) I just get this:

Plot of situp data

Does anyone know how to fix this? TIA!

DYZ
  • 55,249
  • 10
  • 64
  • 93
  • 2
    The "numbers" that you plot are not numbers but strings. Convert them to integer numbers before plotting. – DYZ Oct 06 '20 at 18:53
  • Does this answer your question? [Get a list of numbers as input from the user](https://stackoverflow.com/questions/4663306/get-a-list-of-numbers-as-input-from-the-user) – DYZ Oct 06 '20 at 18:54
  • Not exactly, as the values being plotted aren't being input by the user then, rather they are coming from an external .json file and then being parsed. – seismic_sans201x Oct 06 '20 at 18:58
  • It does not matter. They are still a comma-separated string. Or simply a string, still does not matter. Learn how to convert them to numbers. – DYZ Oct 06 '20 at 19:02

1 Answers1

0

Instead of having situplist.reverse() and pushuplist.reverse(), I put the following code in their place:

int_situplist = []
x = 1
while x <= len(situplist):
int_situplist.append(int(situplist[-x]))
x += 1

It finally works! As DYZ said, situplist and pushuplist were lists of strings, so I just made a new list and appended the same values but as strings to it and used the new list to plot.