在x轴上绘制日期

时间:2017-04-24 13:42:51

标签: python matplotlib weekday

我想在x轴上绘制日期(2017/04/24)。我有一周的体积csv数据,我添加每日量,并绘制它们与当天。我有一个阵列,具体日期但没有月份和年份。

#DETERMINING WEEK DAYS-
weekday_array = []
for i in range (len(timestamp)):
    d = datetime.strptime(timestamp[i], "%Y-%m-%dT%H:%M:%S").day
    weekday_array.append(d)
#print(weekday_array)

#TOTAL DAILY VOLUME
daily_Vol = 0.0
daily_total_vols = []
week_day =[]
weekday_array.append(np.inf)
day = 0

#new_x = dates.datestr2num(date)
for i in range(len(weekday_array)-1):
    start = weekday_array[i]
    next_d = weekday_array[i+1]

    if start== next_d:
        daily_Vol = daily_Vol + volume[i]/10
        day = day+1
    else:
        week_day.append(weekday_array[i])
        daily_Vol = daily_Vol + volume[i]/10
        daily_total_vols.append(daily_Vol)
        daily_Vol = 0.0

weekday_array.remove(np.inf)   #Remove the extra value(infinity) added

#PLOT WEEKLY VOLUME PER DAY
fig = plt.figure(figsize=(20.0, 6.0))
ax = fig.add_subplot(1,1,1)
ax.set_title('WEEKLY VOLUME USDED', fontsize=16)
ax.set_xlabel('WEEKDAY', fontsize=12)
ax.set_ylabel('VOLUME (litres)', fontsize=12)
y = daily_total_vols
x = week_day
ax.bar(x,y,width =0.8,)
for a,b in zip(x,y):
    plt.text(a, b, str(b),fontsize=12)

plt.show()

基本上我想在y轴上使用daily_total_vols,在x轴上使用week_day。 x轴可以是2017/04/24或Mo,Tu,We,Thu,Fri,Sat,Sun。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

我不确定我理解你的目标,但如果你想在x轴上的工作日和y轴上的数字,你可以这样做:

x_days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
x = [1,2,3,4,5,6,7]
y = [1,2,3,4,5,6,7]

plt.xticks(x, x_days)
plt.plot(x, y)
plt.show()

plt_result