简体   繁体   English

matplotlib水平条形图,时间以小时为单位:分钟:x轴上的秒数

[英]matplotlib horizontal bar chart with time in hours:minutes:seconds on x axis

I want to show how much time a user spends on few tasks in a day using horizontal bar chart. 我想显示用户在一天中使用水平条形图花费多少时间花费多少时间。 y-axis is list of tasks and x-axis is time spent on those tasks. y轴是任务列表,x轴是花在这些任务上的时间。 The time is in %H:%M:%S format. 时间以%H:%M:%S格式表示。

fig, ax = plt.subplots()

# Example data
tasks = ('Reading', 'Mobile', 'Outing', 'Laptop')
y_pos = np.arange(len(tasks))
time_spent = ["01:01:34","00:05:23","00:00:09","02:34:32"]
error = np.random.rand(len(tasks))

ax.barh(y_pos, time_spent, xerr=error, align='center')
ax.set_yticks(y_pos)
ax.set_yticklabels(tasks)
ax.set_xlabel('Time spent')
ax.xaxis.set_major_locator(HourLocator())
ax.xaxis.set_major_formatter(DateFormatter('%H:%M:%S'))

plt.show()

I found this somewhere and it's not working. 我在某个地方发现了这个并没有用。 I am not even able to tweak it to get worked. 我甚至无法调整它以使其工作。 Please suggest the solution. 请建议解决方案。 Additionally, I also want to show the time spent after each horizontal bar. 另外,我还想显示每个水平条后花费的时间。 Any ideas for that are also appreciable. 对此的任何想法也是可观的。

As per importanceofbeingernest comment, I passed time in seconds and used the time string to show as label and it worked. 根据最重要的评论,我在几秒钟内通过时间并使用时间字符串显示为标签并且它起作用。

fig, ax = plt.subplots()

# Example data
tasks = ('Reading', 'Mobile', 'Outing', 'Laptop')
time_spent = ["01:01:34","00:05:23","00:00:09","02:34:32"]
timesec = []
for i in range(4):
    timesec.append(convertToSeconds(time_spent[i]))
y_pos = np.arange(len(tasks))
error = np.random.rand(len(tasks))

ax.barh(y_pos, timesec, xerr=error, align='center')
ax.set_yticks(y_pos)
ax.set_yticklabels(tasks)
ax.set_xlabel('Time Spent')
ax.get_xaxis().set_visible(False)

for i, v in enumerate(times):
    print(i)
    ax.text(v + 3, i, time_spent[i], color='blue')

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

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