来自时间序列数据帧的matplotlib [英] matplotlib from time series data frame
本文介绍了来自时间序列数据帧的matplotlib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说我有一个像这样的数据框:
Say I have a data frame like this:
from pandas import DataFrame
example = {'year_month': [201801,201802,201803,201801,201802,201803],
'store_id': [101,101,101,102,102,102],
'tot_employees': [100,200,150,6,7,10],
'hrs_per_employee': [30,35,20,20,18,15]
}
df = DataFrame(example,columns=["year_month", "store_id", "tot_employees", "hrs_per_employee"])
df
并且我想为每个store_id堆叠具有不同子图的子图,
and i want to have stacked subplots with a different subplot for each store_id with:
- x轴:year_month
- 第1行:第一名员工
- 情节第2行:每小时 员工
- x axis: year_month
- plots line 1: tot employees
- plot line 2: hrs per employee
使用df.plot()是否可能?我一直无法找到正确的x,y输入来获得我正在寻找的结果.如果没有,还有没有其他选择?预先感谢
is this possible with df.plot()? I haven't been able to find the correct x,y inputs to get the result i'm looking for. if not is there a close alternative? thanks in advance
推荐答案
import pandas as pd
df = pd.DataFrame(example)
df.year_month = pd.to_datetime(df.year_month, format='%Y%m', exact=True)
df.set_index('year_month', drop=True, inplace=True)
for x in df.store_id.unique():
df[['tot_employees', 'hrs_per_employee']][df.store_id == x].plot(title=f'Store ID: {x}')
df.groupby('store_id').plot(y=['tot_employees', 'hrs_per_employee'])
这篇关于来自时间序列数据帧的matplotlib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文