嗨,我想对一个数据框groupby进行子图绘制。但是在此之前,我也想从一个主要的分组中循环。以这个为例:
Year State Person Value
2011 California A 12879
2011 California B 10572
2011 California C 8645
2011 California D 9573
2011 Florida A 8645
2011 Florida B 9573
2009 California A 8764
2009 California B 9864
2009 California C 9573
2009 California D 12879
2009 Florida A 9573
2009 Florida B 10572
此后,我希望所有人都成为groupby
的州,我希望有一个每个人的价值与年份的子图。因此,每个州1个figure
,然后是一个子图,具体取决于人员数。
这是csv格式:
Year,State,Person,Value
2011,California,A,12879
2011,California,B,10572
2011,California,C,8645
2011,California,D,9573
2011,Florida,A,8645
2011,Florida,B,9573
2009,California,A,8764
2009,California,B,9864
2009,California,C,9573
2009,California,D,12879
2009,Florida,A,9573
2009,Florida,B,10572
答案 0 :(得分:2)
byState=df.groupby('State')
for name, df in byState:
byPerson = df.groupby('Person')
fig, axs = plt.subplots(figsize=(len(byPerson)*2,4), nrows=1, ncols=len(byPerson))
fig.suptitle(name)
subplot_targets = zip(byPerson.groups.keys(), axs.flatten())
for key, ax in subplot_targets:
ax.plot(byPerson.get_group(key).index, byPerson.get_group(key).Value)
ax.set_title(key)