matplotlib barh的计划工作时间与实际工作时间 - python

我正在尝试为员工建立水平条形图,以显示给定日期的计划工作时间与实际工作时间。

我已经尝试了以下代码,但是正如您在下面的“图”图像中所看到的,它是将实际工作时间(蓝色)连接到计划工作时间(绿色)的末尾。而且,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')

matplotlib barh的计划工作时间与实际工作时间 - python

在返回'Response'(Python)中传递多个参数 - python

我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…

如何在Matplotlib条形图后面绘制网格线 - python

x = ['01-02', '02-02', '03-02', '04-02', '05-02'] y = [2, 2, 3, 7, 2] fig, ax = plt.subplots(1, 1) ax.bar(range(len(y)), y, width=…

Matplotlib表行标签字体的颜色和大小 - python

给出下表:import matplotlib.pyplot as plt table=plt.table(cellText=[' ', ' ', ' ', ' ', ' '], # rows of data values rowLabels=['1&…

Matplotlib'粗体'字体 - python

跟随this example:import numpy as np import matplotlib.pyplot as plt fig = plt.figure() for i, label in enumerate(('A', 'B', 'C', 'D')): ax = f…

Matplotlib:保存图形时白色边距和隐藏轴 - python

我一直在尝试保存用matplotlib制作的图,但遇到了一些问题:不仅遇到了常见的白色边距问题(我在网上找到了一些解决方案),而且看来我的坐标轴和标签都在当我保存图像时,它们消失了,尽管当我要求Python show()结果时它们看起来很好。这是MWE,是我用show()得到的结果的打印屏幕(这是我想要的结果),以及将图形保存到.png时得到的结果(我相信白…