2017-04-27 44 views
-1

我有繪製整個一週percent of scores over 100 by hourMatplotlib如何設置x軸以對應一週的幾天?

分數在一分鐘我用一個簡單的遞增器繪製x軸的曲線圖

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() 

這正確顯示數據,但是僅X軸讀取數字。

我該如何將它轉換爲工作日?由於

編輯: 我發現這個答案here,我試圖把它適合我的問題,它幾乎工程,但我不斷收到奇怪的錯誤

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 

回答

0

我找到了解決辦法here。在我的問題編輯中,我沒有改變沿X軸繪製的值。

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