Python 熊猫-在分组后绘制图形的问题

Python 熊猫-在分组后绘制图形的问题,python,pandas,matplotlib,Python,Pandas,Matplotlib,我在sqlite3中有一个表“Attention”,我正在使用pandas作为数据帧导入该表。数据框看起来像这样 id name date time 0 12345 Pankaj 2020-09-12 1900-01-01 23:17:49 1 12345 Pankaj 2020-09-12 1900-01-01 23:20:28 2 12345 Pankaj 2020-09-13 1900-01-01 13:36:01

我在sqlite3中有一个表“Attention”,我正在使用pandas作为数据帧导入该表。数据框看起来像这样

      id    name       date                time
0  12345  Pankaj 2020-09-12 1900-01-01 23:17:49
1  12345  Pankaj 2020-09-12 1900-01-01 23:20:28
2  12345  Pankaj 2020-09-13 1900-01-01 13:36:01
df_gp = df.groupby('date')['time']
difference = df_gp.max() - df_gp.min()
difference.plot(kind = 'line')
一个有“id”的人可以出现多次,这相当于一个人每天进进出出多次,我们正在记录每一次转换

我希望找出“最后一次外出”和“第一次进来”的时间差,找出一个人在工作中的小时数

因为我们一次只需要一个人的数据,所以我首先过滤掉一个人的数据,如下所示

df = df.loc[df['id']== id]
这就给我留下了一个人的所有条目

现在,对于最后一次进入时间和第一次进入时间的差异,我是这样计算的

      id    name       date                time
0  12345  Pankaj 2020-09-12 1900-01-01 23:17:49
1  12345  Pankaj 2020-09-12 1900-01-01 23:20:28
2  12345  Pankaj 2020-09-13 1900-01-01 13:36:01
df_gp = df.groupby('date')['time']
difference = df_gp.max() - df_gp.min()
difference.plot(kind = 'line')
现在,“差异”以熊猫系列的形式出现

date
2020-09-12   00:02:39
2020-09-13   00:00:00
当我尝试使用pandas.series.plot()方法绘制图形时,类型kind='line',如下所示

      id    name       date                time
0  12345  Pankaj 2020-09-12 1900-01-01 23:17:49
1  12345  Pankaj 2020-09-12 1900-01-01 23:20:28
2  12345  Pankaj 2020-09-13 1900-01-01 13:36:01
df_gp = df.groupby('date')['time']
difference = df_gp.max() - df_gp.min()
difference.plot(kind = 'line')
我根本看不到有人在制作图表。我没有看到这样的错误,它只是没有显示任何东西

当我打印时

print(difference.plot(kind = 'line'))
它在终端上打印这个

AxesSubplot(0.125,0.2;0.775x0.68)
所以我想,一定是由于时间的原因。sleep()导致图形被破坏,并太快退出函数,但事实并非如此,我尝试了很多东西,但都没有显示出来

我需要你的帮助-

  • 我没有;当我想得到某一天的时差时,知道这是否是绘制图表的正确方法。请建议您是否有任何优雅的方式来做同样的事情
  • 它根本不显示的原因是什么

  • 完整代码

    def main():
        emp_id = "12345"
        db = os.path.join(constants.BASE_DIR.format("db"),"db_all.db")
        with closing(sqlite3.connect(db)) as conn:
            df = pd.read_sql_query("select * from attendance where id = {} order by date ASC".format(emp_id), conn, parse_dates={'date':'%Y-%m-%d',
                'time':'%H:%M:%S'})
    
        print(df.head())
        #df = df.loc[df['id']== id]
        is_empty = df.empty
        if is_empty:
            messagebox.showerror("Error","There are not enough records of employee")
            return
        
        # Add the latest row
        emp_name = df.loc[(df['id'] == id).idxmax(),'name']
        # dt_time = datetime.datetime.now().replace(microsecond=0)
        # _date, _time = dt_time.date(),dt_time.time()
    
    
        # print(type(_date))
        # print(type(_time))
        # df.loc[-1] = [emp_id,emp_name,_date,_time]
    
        # df.index += 1
        # df = df.sort_index()
    
        # print(df.dtypes)
        df_gp = df.groupby('date')['time']
        print("Here")
        difference = df_gp.max() - df_gp.min()
        print(difference)
        print(difference.plot(kind = 'line'))
    
    
    
    if __name__ == '__main__':
        main()
    

    -感谢matplotlib中的
    将pyplot导入为plt
    并在
    plot
    之后添加
    plt.show()。非常感谢。如果您也能帮助我完成第一个请求,那就太好了?
    从matplotlib导入pyplot作为plt
    并在
    plt
    之后添加
    plt.show()
    @HenryYik这太好了。非常感谢。如果你也能帮我完成我的第一个请求,那就太好了?