这是我正在处理的分组DataFrame。我有几个国家和年份的几个变量。如何使用matplotlib绘制折线图,以显示变量“ gdp_share”在不同国家随时间的变化?
我使用以下方法将DataFrame分组:
data = data.groupby(["year", "Country"]).mean()
gdp_share military_exp pop gdp gdp_pc military_pc
year Country
2010 China 2.0 138028416.0 1359755 13615620000 10226.87 101.509769
France 2.0 54569820.0 63027 2559460000 39449.38 865.816555
Germany 1.0 41488240.0 80895 3587004000 43928.90 512.865319
Israel 6.0 15044490.0 7426 241692000 32665.37 2025.921088
2011 China 2.0 149022400.0 1367480 14957177000 11180.83 108.975927
France 2.0 53235512.0 63344 2612677000 40078.52 840.419172
Germany 1.0 40301428.0 80934 3718288000 45633.17 497.954234
Israel 6.0 15582490.0 7569 254299000 33733.19 2058.725063
2012 China 2.0 161796896.0 1375199 16211650000 12058.70 117.653442
France 2.0 52499752.0 63640 2617451000 39961.04 824.948963
Germany 1.0 41262500.0 81066 3736582000 45771.68 508.998840
Israel 6.0 15728390.0 7699 259884000 33840.45 2042.913365
答案 0 :(得分:0)
尝试
fig, ax = plt.subplots()
ax.set_xticks(data.year.unique())
data.groupby(["year", "Country"]).mean()['gdp_share'].unstack().plot(ax=ax)