-1

I have a graph which plots the scores across a week percent of scores over 100 by hour

At the minute I am using a simple incrementor to plot the x axis

percent = []
count = 0
time = []
for x, y in zip(counts100, counts):
    percent.append(float(x/y))
    time.append(count)
    count += 1


plt.xlabel('weekday')
plt.ylabel('% score over 100')
plt.plot( time, percent )
plt.gcf().autofmt_xdate()
plt.show()

This displays the data correctly, however the x axis only reads the numbers.

How can I convert this to weekdays? Thanks

EDIT: I found this answer here and I am trying to fit it to my problem and it almost works but I keep getting weird errors

 File "/usr/local/lib/python3.5/dist-packages/matplotlib/dates.py", line 401, in num2date
 return _from_ordinalf(x, tz)
 File "/usr/local/lib/python3.5/dist-packages/matplotlib/dates.py", line 254, in _from_ordinalf
    dt = datetime.datetime.fromordinal(ix).replace(tzinfo=UTC)
ValueError: ordinal must be >= 1
Community
  • 1
  • 1
kopo222
  • 119
  • 11

1 Answers1

0

I found the solution here. In my questions edit I was not changing the value to be plotted along the X axis.

percent = []
count = 0
time = []
for x, y, z in zip(counts100, counts, hour):
    percent.append(float(x/y))
    time.append(count)
    count += 1
    print("the percent is : {:.4f}  for hour {}".format(float(x/y), z))



times = pd.date_range('2017-10-06 00:00', periods=count, freq='1H', tz = 'UTC')

fig, ax = plt.subplots(1)
fig.autofmt_xdate()

plt.xlabel('weekday')
plt.ylabel('% score over 100')
plt.plot( times, percent )
xfmt = mdates.DateFormatter('%d %H:%M')
ax.xaxis.set_major_formatter(xfmt)

plt.show()

enter image description here

Community
  • 1
  • 1
kopo222
  • 119
  • 11