简体   繁体   English

在x轴上绘制日期

[英]Plot date on x-axis

I am trying to plot date (2017/04/24) on the x-axis. 我正在尝试在x轴上绘制日期(2017/04/24)。 I have volume csv data for a week, Im adding daily volume and plotting them against the day. 我有一个星期的体积csv数据,即时添加每日体积并将其绘制为一天。 I have an array, with the specific day but it doesnt have the month and year. 我有一个数组,有特定的日期,但没有月份和年份。

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

Basically I would like daily_total_vols on the y-axis and week_day on the x-axis. 基本上,我想在y轴上设置daily_total_vols,在x轴上使用week_day。 The x-axis can be 2017/04/24 or Mo,Tu,We,Thu,Fri,Sat,Sun. x轴可以是2017/04/24或Mo,Tu,We,Thu,Fri,Sat,Sun。 Thank you in advance. 先感谢您。

I am not sure I understand what you are after, but if you want weekdays on the x-axis and numbers on the y-axis, you can do: 我不确定我了解您要做什么,但是如果您希望在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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM