Python &引用;属性错误:';列表';对象没有属性';取消堆叠'&引用;在熊猫中创建子地块时
我已使用以下代码从df创建了一个图形:Python &引用;属性错误:';列表';对象没有属性';取消堆叠'&引用;在熊猫中创建子地块时,python,pandas,matplotlib,Python,Pandas,Matplotlib,我已使用以下代码从df创建了一个图形: pri_count = df_2011.groupby(['Priority', df_2011['Create Time'].dt.to_period('m')]).Priority.count() plot_df1 = pri_count.unstack('Priority').loc[:, 1] plot_df2 = pri_count.unstack('Priority').loc[:, 2] plot_df1.index = pd.PeriodI
pri_count = df_2011.groupby(['Priority', df_2011['Create Time'].dt.to_period('m')]).Priority.count()
plot_df1 = pri_count.unstack('Priority').loc[:, 1]
plot_df2 = pri_count.unstack('Priority').loc[:, 2]
plot_df1.index = pd.PeriodIndex(plot_df1.index.tolist(), freq='m')
plot_df2.index = pd.PeriodIndex(plot_df2.index.tolist(), freq='m')
plot_df1.plot(legend=True, title='Graph 1')
plot_df2.plot(legend=True)
(我不知道如何添加图像,但这段代码似乎正在生成我需要的第一个图形。)这似乎工作正常,但问题是我有6个df,我想生成创建一个2x3子地块和标题的绘图。我得到的错误是:AttributeError:“list”对象没有属性“unstack”
我已经尝试了一些似乎走对了路的事情(在我修复之前我有一些错误),但现在我陷入了寻找解决这个错误的方法
我的代码不适用于子批次:
pri_count_list = [df_2011.groupby(['Priority', df_2011['Create Time'].dt.to_period('m')]).Priority.count(),
df_2012.groupby(['Priority', df_2012['Create Time'].dt.to_period('m')]).Priority.count(),
df_2013.groupby(['Priority', df_2013['Create Time'].dt.to_period('m')]).Priority.count(),
df_2014.groupby(['Priority', df_2014['Create Time'].dt.to_period('m')]).Priority.count(),
df_2015.groupby(['Priority', df_2015['Create Time'].dt.to_period('m')]).Priority.count(),
df_2016.groupby(['Priority', df_2016['Create Time'].dt.to_period('m')]).Priority.count()]
fig, ax = plt.subplots(2, 3)
for d, ax in zip(pri_count_list, axes.ravel()):
for i in pri_count_list:
plot_df1 = pri_count_list.unstack('Priority').loc[:, 1]
plot_df2 = pri_count_list.unstack('Priority').loc[:, 2]
plot_df1.index = pd.PeriodIndex(plot_df1.index.tolist(), freq='m')
plot_df2.index = pd.PeriodIndex(plot_df2.index.tolist(), freq='m')
plot_df1.plot(ax=ax, legend=True, figsize=(15, 7))
plot_df2.plot(ax=ax, legend=True, figsize=(15, 7))
我希望制作一个2x3子图,其类型与上面代码生成的单个图相同
我想了解这个错误的更多含义,所以请尽可能解释。谢谢
编辑:
pri\u count\u list
中的数据为:
[Priority Create Time
0.0 2011-04 1
2011-09 1
1.0 2011-01 2801
2011-02 2358
2011-03 2688
2011-04 2958
2011-05 3162
2011-06 3260
2011-07 3605
2011-08 3361
2011-09 3300
2011-10 3135
2011-11 3028
2011-12 3043
2.0 2011-01 11905
2011-02 10705
2011-03 12256
2011-04 12076
2011-05 11912
2011-06 12492
2011-07 12768
2011-08 12558
2011-09 11978
2011-10 11994
2011-11 11113
2011-12 11557
Name: Priority, dtype: int64, Priority Create Time
1.0 2012-01 3198
2012-02 2991
2012-03 3164
2012-04 3291
2012-05 3420
2012-06 3692
2012-07 3780
2012-08 3814
2012-09 3570
2012-10 3694
2012-11 3572
2012-12 3740
2.0 2012-01 11562
2012-02 10756
2012-03 12441
2012-04 12027
2012-05 12104
2012-06 12809
2012-07 12935
2012-08 12683
2012-09 11872
2012-10 12484
2012-11 11638
2012-12 12193
Name: Priority, dtype: int64, Priority Create Time
0.0 2013-01 4
2013-03 4
2013-05 2
2013-06 1
2013-07 3
2013-08 1
2013-09 2
2013-10 2
2013-12 2
1.0 2013-01 3409
2013-02 3145
2013-03 3448
2013-04 3539
2013-05 3818
2013-06 3977
2013-07 3802
2013-08 3806
2013-09 3851
2013-10 3619
2013-11 3439
2013-12 3318
2.0 2013-01 11792
2013-02 10718
2013-03 12010
2013-04 12212
2013-05 12344
2013-06 12626
2013-07 12679
2013-08 12894
2013-09 12617
2013-10 12229
2013-11 11449
2013-12 11289
Name: Priority, dtype: int64, Priority Create Time
1 2014-01 3548
2014-02 3216
2014-03 3626
2014-04 3372
2014-05 3728
2014-06 3734
2014-07 3649
2014-08 3507
2014-09 3613
2014-10 3690
2014-11 3361
2014-12 3729
2 2014-01 11956
2014-02 11006
2014-03 12373
2014-04 11888
2014-05 12601
2014-06 12293
2014-07 12384
2014-08 11752
2014-09 11627
2014-10 12626
2014-11 11478
2014-12 12723
Name: Priority, dtype: int64, Priority Create Time
0 2015-10 1
1 2015-01 3692
2015-02 3282
2015-03 3481
2015-04 3360
2015-05 3521
2015-06 3688
2015-07 3796
2015-08 3895
2015-09 3895
2015-10 3729
2015-11 2981
2015-12 3098
2 2015-01 12211
2015-02 11669
2015-03 12657
2015-04 12303
2015-05 12858
2015-06 12761
2015-07 13505
2015-08 13117
2015-09 12799
2015-10 12610
2015-11 11621
2015-12 12051
Name: Priority, dtype: int64, Priority Create Time
1.0 2016-01 3134
2016-02 3290
2016-03 3410
2016-04 3462
2016-05 3762
2016-06 3707
2016-07 3790
2.0 2016-01 11755
2016-02 11643
2016-03 12569
2016-04 12428
2016-05 12989
2016-06 12661
2016-07 12227
Name: Priority, dtype: int64]
对于d,ax在zip中(pri_count_列表,axes.ravel()):
对于优先级列表中的i:#此i
plot_df1=pri_count_list.unstack('Priority').loc[:,1]#这应该是i
这应该是i
,因为它应该是迭代的结果,而不是列表本身
固定的是:
plot_df1=i.unstack('Priority').loc[:,1]
plot_df2=i.unstack('Priority').loc[:,2]
对于d,ax在zip中(pri\u count\u列表,axes.ravel()):
对于优先级列表中的i:#此i
plot_df1=pri_count_list.unstack('Priority').loc[:,1]#这应该是i
这应该是i
,因为它应该是迭代的结果,而不是列表本身
固定的是:
plot_df1=i.unstack('Priority').loc[:,1]
plot_df2=i.unstack('Priority').loc[:,2]
对于优先级列表中的i:
解决您的问题。要取消堆栈的对象在i
中,而不是pri\u count\u list
,它是一个列表,对于pri\u count\u list中的i,它没有取消堆栈的功能:
解决您的问题。要取消堆栈的对象在i
中,而不是pri\u count\u list
,它是一个列表,没有取消堆栈
的能力。这似乎工作正常,谢谢。现在出现了一个新问题。除了子地块的轮廓外,未打印任何内容。没有线条,没有图例,x轴和y轴从0到1完全为空。你知道为什么吗?如果看不到数据,很难说这两个循环中有一个是多余的。@ImportanceOfBeingErnest-好的。我已经把d的取出来了,ax在zip中(…
也取出来了ax=ax
在plot()中
查看会发生什么。所有结果都打印在最后一张图上,而不是子图中自己的图。这是因为plot\u dfx.index=…
行吗?请参见我的编辑。d
实际上是pri\u count\u list
中的迭代项,因此您可以直接访问它,而无需附加循环,谢谢@ImpoBeingernest的重要性这似乎很好,谢谢。现在出现了一个新问题。除了子批次的轮廓外,没有打印任何内容。没有线条,没有图例。x轴和y轴从0到1是完全空白的。你知道为什么吗?如果看不到数据,很难说两个循环中的一个是多余的。@ImportanceOfBeingErnest-好的。我已经把d的取出来了,ax在zip中(…
也取出来了ax=ax
在plot()中
查看会发生什么。所有结果都打印在最后一张图上,而不是子图中自己的图。这是因为plot\u dfx.index=…
行吗?请参见我的编辑。d
实际上是pri\u count\u list
中的迭代项,因此您可以直接访问它,而无需附加循环,谢谢@Impo贝格纳斯特酒店
# Create subplots
fig, axes = plt.subplots(2, 3)
# Needs to be d, not pri_count_list
for d, ax in zip(pri_count_list, axes.ravel()):
plot_df1 = i.unstack('Priority').loc[:, 1]
# Plot with the iterated axis
ax.plot(plot_df1, legend=True, figsize=(15,7)
...