我想使用水平條形圖顯示用戶在一天內花費在幾項任務上的時間。y 軸是任務列表,x 軸是花費在這些任務上的時間。時間采用 %H:%M:%S 格式。fig, ax = plt.subplots()# Example datatasks = ('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()我在某個地方找到了它,但它不起作用。我什至無法對其進行調整以使其正常工作。請提出解決方案。此外,我還想顯示每個水平條后花費的時間。對此的任何想法也是可觀的。
1 回答

紅糖糍粑
TA貢獻1815條經驗 獲得超6個贊
根據重要評論,我以秒為單位傳遞時間并使用時間字符串顯示為標簽并且它有效。
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')
添加回答
舉報
0/150
提交
取消