matplotlib barh 计划时间与实际工作时间
matplotlib barh for scheduled hours vs actual hours worked
我正在尝试为员工构建一个水平条形图,显示他们在给定日期的计划工作时间与实际工作时间。
我已经尝试了以下代码,但正如您在下面的 'Plot' 图像中看到的那样,它将实际工作时间(蓝色)与计划时间结束时(绿色)连接起来。另外 x 轴上的时间也不是很清楚。
我想要的是为每个员工设置两个条,绿色条显示顶部的计划时间,蓝色条显示下方的实际工作时间,几乎像甘特图。谁能帮助我了解我的代码哪里出错了?
#import stack
import pandas as pd
import datetime as dt
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
#dummy df
df = pd.DataFrame([['Bob', '2018-09-14 9:00:00', '2018-09-14 18:00:00', 'actual']
, ['Bob', '2018-09-14 9:15:00', '2018-09-14 18:30:00', 'scheduled']
, ['Kim', '2018-09-14 9:00:00', '2018-09-14 18:00:00', 'actual']
, ['Kim', '2018-09-14 8:45:00', '2018-09-14 17:30:00', 'scheduled']]
, columns=['name','start','finish', 'type'])
#convert timestamp columns to datetime
df[['start', 'finish']] = df[['start', 'finish']].apply(pd.to_datetime)
#scheduled time period
scheduledStart = mdates.date2num(df['start'][(df['type'] == 'scheduled')].dt.to_pydatetime())
scheduledEnd = mdates.date2num(df['finish'][(df['type'] == 'scheduled')].dt.to_pydatetime())
scheduledWidth = scheduledEnd - scheduledStart
#actual time period
actualStart = mdates.date2num(df['start'][(df['type'] == 'actual')].dt.to_pydatetime())
actualEnd = mdates.date2num(df['finish'][(df['type'] == 'actual')].dt.to_pydatetime())
actualWidth = actualEnd - actualStart
#y axis values
yval = df['name'].unique()
#generate plot
fig, ax = plt.subplots()
ax.barh(yval, width = actualWidth, left = actualStart, color = 'blue', height = 0.3, label = 'actual')
ax.barh(yval, width = scheduledWidth, left = scheduledStart, color = 'green', height = 0.3, label = 'scheduled')
#format x axis to time of day
xfmt = mdates.DateFormatter('%H:%m')
ax.xaxis.set_major_formatter(xfmt)
# autorotate the dates
fig.autofmt_xdate()
plt.show()
尝试不同的值,例如第一个 [0, 1]
和第二个 [0.3, 1.3]
ax.barh
(这样它们就不会重叠)。我们只是在移动 height
值。
ax.barh([0, 1], width = actualWidth, left = actualStart, color = 'blue', height = 0.3, label = 'actual')
ax.barh([0.3, 1.3], width = scheduledWidth, left = scheduledStart, color = 'green', height = 0.3, label = 'scheduled')
并更改yticks
(选择两个柱之间的中心):
plt.yticks([0.15, 1.15], yval)
终于修复了 xtick
:
fig.autofmt_xdate(ha='center')
我正在尝试为员工构建一个水平条形图,显示他们在给定日期的计划工作时间与实际工作时间。
我已经尝试了以下代码,但正如您在下面的 'Plot' 图像中看到的那样,它将实际工作时间(蓝色)与计划时间结束时(绿色)连接起来。另外 x 轴上的时间也不是很清楚。
我想要的是为每个员工设置两个条,绿色条显示顶部的计划时间,蓝色条显示下方的实际工作时间,几乎像甘特图。谁能帮助我了解我的代码哪里出错了?
#import stack
import pandas as pd
import datetime as dt
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
#dummy df
df = pd.DataFrame([['Bob', '2018-09-14 9:00:00', '2018-09-14 18:00:00', 'actual']
, ['Bob', '2018-09-14 9:15:00', '2018-09-14 18:30:00', 'scheduled']
, ['Kim', '2018-09-14 9:00:00', '2018-09-14 18:00:00', 'actual']
, ['Kim', '2018-09-14 8:45:00', '2018-09-14 17:30:00', 'scheduled']]
, columns=['name','start','finish', 'type'])
#convert timestamp columns to datetime
df[['start', 'finish']] = df[['start', 'finish']].apply(pd.to_datetime)
#scheduled time period
scheduledStart = mdates.date2num(df['start'][(df['type'] == 'scheduled')].dt.to_pydatetime())
scheduledEnd = mdates.date2num(df['finish'][(df['type'] == 'scheduled')].dt.to_pydatetime())
scheduledWidth = scheduledEnd - scheduledStart
#actual time period
actualStart = mdates.date2num(df['start'][(df['type'] == 'actual')].dt.to_pydatetime())
actualEnd = mdates.date2num(df['finish'][(df['type'] == 'actual')].dt.to_pydatetime())
actualWidth = actualEnd - actualStart
#y axis values
yval = df['name'].unique()
#generate plot
fig, ax = plt.subplots()
ax.barh(yval, width = actualWidth, left = actualStart, color = 'blue', height = 0.3, label = 'actual')
ax.barh(yval, width = scheduledWidth, left = scheduledStart, color = 'green', height = 0.3, label = 'scheduled')
#format x axis to time of day
xfmt = mdates.DateFormatter('%H:%m')
ax.xaxis.set_major_formatter(xfmt)
# autorotate the dates
fig.autofmt_xdate()
plt.show()
尝试不同的值,例如第一个 [0, 1]
和第二个 [0.3, 1.3]
ax.barh
(这样它们就不会重叠)。我们只是在移动 height
值。
ax.barh([0, 1], width = actualWidth, left = actualStart, color = 'blue', height = 0.3, label = 'actual')
ax.barh([0.3, 1.3], width = scheduledWidth, left = scheduledStart, color = 'green', height = 0.3, label = 'scheduled')
并更改yticks
(选择两个柱之间的中心):
plt.yticks([0.15, 1.15], yval)
终于修复了 xtick
:
fig.autofmt_xdate(ha='center')