Python 如何制作这种条形图,将列表事务分组为12组条形图?

Python 如何制作这种条形图,将列表事务分组为12组条形图?,python,pandas,matplotlib,Python,Pandas,Matplotlib,我正在尝试可视化数据集 transaction = [1480000.0, 1035000.0, 1465000.0, 850000.0, 1600000.0, 941000.0, 1876000.0, 1636000.0, 300000.0, 1097000.0, 700000.0, 1350000.0, 750000.0, 1172500.0, 441000.0, 1310000.0, 1200000.0, 1176500.0, 955000.0, 890000.0, 1330000.0,

我正在尝试可视化数据集

transaction = [1480000.0, 1035000.0, 1465000.0, 850000.0, 1600000.0, 941000.0, 1876000.0, 1636000.0, 300000.0, 1097000.0, 700000.0, 1350000.0, 750000.0, 1172500.0, 441000.0, 1310000.0, 1200000.0, 1176500.0, 955000.0, 890000.0, 1330000.0, 900000.0, 1090000.0, 500000.0, 1100000.0, 1315000.0, 426000.0, 1447500.0, 457000.0]
date = ['3/12/2016', '4/02/2016', '4/03/2017', '4/03/2017', '4/06/2016', '7/05/2016', '7/05/2016', '8/10/2016', '8/10/2016', '8/10/2016', '12/11/2016', '12/11/2016', '12/11/2016', '13/08/2016', '14/05/2016', '15/10/2016', '16/07/2016', '16/07/2016', '17/09/2016', '17/09/2016', '18/03/2017', '18/03/2017', '18/03/2017', '18/06/2016', '18/06/2016', '19/11/2016', '22/08/2016', '22/08/2016', '22/08/2016']
以下是一些交易:

transaction = [1480000.0, 1035000.0, 1465000.0, 850000.0, 1600000.0, 941000.0, 1876000.0, 1636000.0, 300000.0, 1097000.0, 700000.0, 1350000.0, 750000.0, 1172500.0, 441000.0, 1310000.0, 1200000.0, 1176500.0, 955000.0, 890000.0, 1330000.0, 900000.0, 1090000.0, 500000.0, 1100000.0, 1315000.0, 426000.0, 1447500.0, 457000.0]
date = ['3/12/2016', '4/02/2016', '4/03/2017', '4/03/2017', '4/06/2016', '7/05/2016', '7/05/2016', '8/10/2016', '8/10/2016', '8/10/2016', '12/11/2016', '12/11/2016', '12/11/2016', '13/08/2016', '14/05/2016', '15/10/2016', '16/07/2016', '16/07/2016', '17/09/2016', '17/09/2016', '18/03/2017', '18/03/2017', '18/03/2017', '18/06/2016', '18/06/2016', '19/11/2016', '22/08/2016', '22/08/2016', '22/08/2016']
举行日期:

transaction = [1480000.0, 1035000.0, 1465000.0, 850000.0, 1600000.0, 941000.0, 1876000.0, 1636000.0, 300000.0, 1097000.0, 700000.0, 1350000.0, 750000.0, 1172500.0, 441000.0, 1310000.0, 1200000.0, 1176500.0, 955000.0, 890000.0, 1330000.0, 900000.0, 1090000.0, 500000.0, 1100000.0, 1315000.0, 426000.0, 1447500.0, 457000.0]
date = ['3/12/2016', '4/02/2016', '4/03/2017', '4/03/2017', '4/06/2016', '7/05/2016', '7/05/2016', '8/10/2016', '8/10/2016', '8/10/2016', '12/11/2016', '12/11/2016', '12/11/2016', '13/08/2016', '14/05/2016', '15/10/2016', '16/07/2016', '16/07/2016', '17/09/2016', '17/09/2016', '18/03/2017', '18/03/2017', '18/03/2017', '18/06/2016', '18/06/2016', '19/11/2016', '22/08/2016', '22/08/2016', '22/08/2016']
我试图将这些交易分为12组,每组中分别有2组代表2016年和2017年

transaction = [1480000.0, 1035000.0, 1465000.0, 850000.0, 1600000.0, 941000.0, 1876000.0, 1636000.0, 300000.0, 1097000.0, 700000.0, 1350000.0, 750000.0, 1172500.0, 441000.0, 1310000.0, 1200000.0, 1176500.0, 955000.0, 890000.0, 1330000.0, 900000.0, 1090000.0, 500000.0, 1100000.0, 1315000.0, 426000.0, 1447500.0, 457000.0]
date = ['3/12/2016', '4/02/2016', '4/03/2017', '4/03/2017', '4/06/2016', '7/05/2016', '7/05/2016', '8/10/2016', '8/10/2016', '8/10/2016', '12/11/2016', '12/11/2016', '12/11/2016', '13/08/2016', '14/05/2016', '15/10/2016', '16/07/2016', '16/07/2016', '17/09/2016', '17/09/2016', '18/03/2017', '18/03/2017', '18/03/2017', '18/06/2016', '18/06/2016', '19/11/2016', '22/08/2016', '22/08/2016', '22/08/2016']
df = pd.DataFrame(np.array([transaction, date]).transpose(),
                 columns=['transaction', 'date'])
di = pd.to_datetime(df['date'])
from collections import defaultdict
tem1 = defaultdict(list)
for i in di:
    tem1['year'].append(i.year)
    tem1['month'].append(i.month)
df['year'] = tem1['year']
df['month'] = tem1['month']
bar_df = df.groupby(['month','year']).count()
bar_df.plot.bar()
我得到了这个:

transaction = [1480000.0, 1035000.0, 1465000.0, 850000.0, 1600000.0, 941000.0, 1876000.0, 1636000.0, 300000.0, 1097000.0, 700000.0, 1350000.0, 750000.0, 1172500.0, 441000.0, 1310000.0, 1200000.0, 1176500.0, 955000.0, 890000.0, 1330000.0, 900000.0, 1090000.0, 500000.0, 1100000.0, 1315000.0, 426000.0, 1447500.0, 457000.0]
date = ['3/12/2016', '4/02/2016', '4/03/2017', '4/03/2017', '4/06/2016', '7/05/2016', '7/05/2016', '8/10/2016', '8/10/2016', '8/10/2016', '12/11/2016', '12/11/2016', '12/11/2016', '13/08/2016', '14/05/2016', '15/10/2016', '16/07/2016', '16/07/2016', '17/09/2016', '17/09/2016', '18/03/2017', '18/03/2017', '18/03/2017', '18/06/2016', '18/06/2016', '19/11/2016', '22/08/2016', '22/08/2016', '22/08/2016']

transaction = [1480000.0, 1035000.0, 1465000.0, 850000.0, 1600000.0, 941000.0, 1876000.0, 1636000.0, 300000.0, 1097000.0, 700000.0, 1350000.0, 750000.0, 1172500.0, 441000.0, 1310000.0, 1200000.0, 1176500.0, 955000.0, 890000.0, 1330000.0, 900000.0, 1090000.0, 500000.0, 1100000.0, 1315000.0, 426000.0, 1447500.0, 457000.0]
date = ['3/12/2016', '4/02/2016', '4/03/2017', '4/03/2017', '4/06/2016', '7/05/2016', '7/05/2016', '8/10/2016', '8/10/2016', '8/10/2016', '12/11/2016', '12/11/2016', '12/11/2016', '13/08/2016', '14/05/2016', '15/10/2016', '16/07/2016', '16/07/2016', '17/09/2016', '17/09/2016', '18/03/2017', '18/03/2017', '18/03/2017', '18/06/2016', '18/06/2016', '19/11/2016', '22/08/2016', '22/08/2016', '22/08/2016']

如何解决此问题?

您可以为列
date
分配回发日期时间,这样就可以通过
transaction = [1480000.0, 1035000.0, 1465000.0, 850000.0, 1600000.0, 941000.0, 1876000.0, 1636000.0, 300000.0, 1097000.0, 700000.0, 1350000.0, 750000.0, 1172500.0, 441000.0, 1310000.0, 1200000.0, 1176500.0, 955000.0, 890000.0, 1330000.0, 900000.0, 1090000.0, 500000.0, 1100000.0, 1315000.0, 426000.0, 1447500.0, 457000.0]
date = ['3/12/2016', '4/02/2016', '4/03/2017', '4/03/2017', '4/06/2016', '7/05/2016', '7/05/2016', '8/10/2016', '8/10/2016', '8/10/2016', '12/11/2016', '12/11/2016', '12/11/2016', '13/08/2016', '14/05/2016', '15/10/2016', '16/07/2016', '16/07/2016', '17/09/2016', '17/09/2016', '18/03/2017', '18/03/2017', '18/03/2017', '18/06/2016', '18/06/2016', '19/11/2016', '22/08/2016', '22/08/2016', '22/08/2016']

transaction = [1480000.0, 1035000.0, 1465000.0, 850000.0, 1600000.0, 941000.0, 1876000.0, 1636000.0, 300000.0, 1097000.0, 700000.0, 1350000.0, 750000.0, 1172500.0, 441000.0, 1310000.0, 1200000.0, 1176500.0, 955000.0, 890000.0, 1330000.0, 900000.0, 1090000.0, 500000.0, 1100000.0, 1315000.0, 426000.0, 1447500.0, 457000.0]
date = ['3/12/2016', '4/02/2016', '4/03/2017', '4/03/2017', '4/06/2016', '7/05/2016', '7/05/2016', '8/10/2016', '8/10/2016', '8/10/2016', '12/11/2016', '12/11/2016', '12/11/2016', '13/08/2016', '14/05/2016', '15/10/2016', '16/07/2016', '16/07/2016', '17/09/2016', '17/09/2016', '18/03/2017', '18/03/2017', '18/03/2017', '18/06/2016', '18/06/2016', '19/11/2016', '22/08/2016', '22/08/2016', '22/08/2016']
然后对于
bar_df
使用或与
重命名一起使用:

transaction = [1480000.0, 1035000.0, 1465000.0, 850000.0, 1600000.0, 941000.0, 1876000.0, 1636000.0, 300000.0, 1097000.0, 700000.0, 1350000.0, 750000.0, 1172500.0, 441000.0, 1310000.0, 1200000.0, 1176500.0, 955000.0, 890000.0, 1330000.0, 900000.0, 1090000.0, 500000.0, 1100000.0, 1315000.0, 426000.0, 1447500.0, 457000.0]
date = ['3/12/2016', '4/02/2016', '4/03/2017', '4/03/2017', '4/06/2016', '7/05/2016', '7/05/2016', '8/10/2016', '8/10/2016', '8/10/2016', '12/11/2016', '12/11/2016', '12/11/2016', '13/08/2016', '14/05/2016', '15/10/2016', '16/07/2016', '16/07/2016', '17/09/2016', '17/09/2016', '18/03/2017', '18/03/2017', '18/03/2017', '18/06/2016', '18/06/2016', '19/11/2016', '22/08/2016', '22/08/2016', '22/08/2016']
df = pd.DataFrame({'transaction':transaction , 'date':date})
df['date'] = pd.to_datetime(df['date'])
#print (df)

bar_df = df.groupby([df['date'].dt.month,df['date'].dt.year]).size().unstack(fill_value=0)
或:

transaction = [1480000.0, 1035000.0, 1465000.0, 850000.0, 1600000.0, 941000.0, 1876000.0, 1636000.0, 300000.0, 1097000.0, 700000.0, 1350000.0, 750000.0, 1172500.0, 441000.0, 1310000.0, 1200000.0, 1176500.0, 955000.0, 890000.0, 1330000.0, 900000.0, 1090000.0, 500000.0, 1100000.0, 1315000.0, 426000.0, 1447500.0, 457000.0]
date = ['3/12/2016', '4/02/2016', '4/03/2017', '4/03/2017', '4/06/2016', '7/05/2016', '7/05/2016', '8/10/2016', '8/10/2016', '8/10/2016', '12/11/2016', '12/11/2016', '12/11/2016', '13/08/2016', '14/05/2016', '15/10/2016', '16/07/2016', '16/07/2016', '17/09/2016', '17/09/2016', '18/03/2017', '18/03/2017', '18/03/2017', '18/06/2016', '18/06/2016', '19/11/2016', '22/08/2016', '22/08/2016', '22/08/2016']


您可以为列
date
分配回发日期时间,以便可以通过以下方式获得
year
s和
month
s

transaction = [1480000.0, 1035000.0, 1465000.0, 850000.0, 1600000.0, 941000.0, 1876000.0, 1636000.0, 300000.0, 1097000.0, 700000.0, 1350000.0, 750000.0, 1172500.0, 441000.0, 1310000.0, 1200000.0, 1176500.0, 955000.0, 890000.0, 1330000.0, 900000.0, 1090000.0, 500000.0, 1100000.0, 1315000.0, 426000.0, 1447500.0, 457000.0]
date = ['3/12/2016', '4/02/2016', '4/03/2017', '4/03/2017', '4/06/2016', '7/05/2016', '7/05/2016', '8/10/2016', '8/10/2016', '8/10/2016', '12/11/2016', '12/11/2016', '12/11/2016', '13/08/2016', '14/05/2016', '15/10/2016', '16/07/2016', '16/07/2016', '17/09/2016', '17/09/2016', '18/03/2017', '18/03/2017', '18/03/2017', '18/06/2016', '18/06/2016', '19/11/2016', '22/08/2016', '22/08/2016', '22/08/2016']
然后对于
bar_df
使用或与
重命名一起使用:

transaction = [1480000.0, 1035000.0, 1465000.0, 850000.0, 1600000.0, 941000.0, 1876000.0, 1636000.0, 300000.0, 1097000.0, 700000.0, 1350000.0, 750000.0, 1172500.0, 441000.0, 1310000.0, 1200000.0, 1176500.0, 955000.0, 890000.0, 1330000.0, 900000.0, 1090000.0, 500000.0, 1100000.0, 1315000.0, 426000.0, 1447500.0, 457000.0]
date = ['3/12/2016', '4/02/2016', '4/03/2017', '4/03/2017', '4/06/2016', '7/05/2016', '7/05/2016', '8/10/2016', '8/10/2016', '8/10/2016', '12/11/2016', '12/11/2016', '12/11/2016', '13/08/2016', '14/05/2016', '15/10/2016', '16/07/2016', '16/07/2016', '17/09/2016', '17/09/2016', '18/03/2017', '18/03/2017', '18/03/2017', '18/06/2016', '18/06/2016', '19/11/2016', '22/08/2016', '22/08/2016', '22/08/2016']
df = pd.DataFrame({'transaction':transaction , 'date':date})
df['date'] = pd.to_datetime(df['date'])
#print (df)

bar_df = df.groupby([df['date'].dt.month,df['date'].dt.year]).size().unstack(fill_value=0)
或:

transaction = [1480000.0, 1035000.0, 1465000.0, 850000.0, 1600000.0, 941000.0, 1876000.0, 1636000.0, 300000.0, 1097000.0, 700000.0, 1350000.0, 750000.0, 1172500.0, 441000.0, 1310000.0, 1200000.0, 1176500.0, 955000.0, 890000.0, 1330000.0, 900000.0, 1090000.0, 500000.0, 1100000.0, 1315000.0, 426000.0, 1447500.0, 457000.0]
date = ['3/12/2016', '4/02/2016', '4/03/2017', '4/03/2017', '4/06/2016', '7/05/2016', '7/05/2016', '8/10/2016', '8/10/2016', '8/10/2016', '12/11/2016', '12/11/2016', '12/11/2016', '13/08/2016', '14/05/2016', '15/10/2016', '16/07/2016', '16/07/2016', '17/09/2016', '17/09/2016', '18/03/2017', '18/03/2017', '18/03/2017', '18/06/2016', '18/06/2016', '19/11/2016', '22/08/2016', '22/08/2016', '22/08/2016']