如何在熊猫中明智地显示饼图

时间:2018-01-28 06:03:17

标签: python pandas numpy charts

我一直在制作饼图以显示基于年份的数据。我已经尝试了很长一段时间我成功地实现了切片行:

df = pd.DataFrame(dict( Year = dates[:3],
        robbery = robbery[:3],
        fraud = fraud[:3],
        sexual = sexual[:3]
    ))
    fig, axes = plt.subplots(1,3, figsize=(12,8))
for ax, idx in zip(axes, df.index):
    ax.pie(df.loc[idx],explode=explode,shadow=True, labels=df.columns, autopct='%.2f%%')
    ax.set(ylabel='', title=idx, aspect='equal')
axes[0].legend(bbox_to_anchor=(0, 0.5))
plt.show()

enter image description here

但是我已经检查了这个display pie chart的链接,但他们已经在numpy数组上工作以实现图表。 在我的场景中,我坚持一次性显示饼图上的所有数据,这是我的代码:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

df = pd.DataFrame(dict(
        robbery = robbery,
        fraud = fraud,
        assualt = sexual
    ), index=dates)

print(df)

plt.style.use('ggplot')
colors = plt.rcParams['axes.color_cycle']

fig, axes = plt.subplots(nrows=2, ncols=2)
for ax, col in zip(axes.flat, df.columns):
    ax.pie(df[col], labels=df.index, autopct='%.2f', colors=colors)
    ax.set(ylabel='', title=col, aspect='equal')

axes[0, 0].legend(bbox_to_anchor=(0, 0.5))

fig.savefig('your_file.png') # Or whichever format you'd like
plt.show()

DataFrame:  

             assualt  fraud  robbery
1997-1998     2988  11897     1212
1998-1999     6033  27660     2482
1999-2000     5924  28421     2418
2000-2001     5631  29539     2298
2001-2002     5875  30295     2481
2002-2003     7434  27141     1940
2003-2004     5673  27986     2053
2004-2005     5695  30070     1879
2005-2006     6099  26031     1903
2006-2007     7038  25845     1889
2007-2008     6671  21009     1736
2008-2009     6046  17768     1791
2009-2010     5496  18974     1934
2010-2011     5666  18458     1726
2011-2012     4933  14157     1748
2012-2013     4972  16849     1962
2013-2014     5328  18819     1762
2014-2015     5909  21915     1341
2015-2016     6067  21891     1354
2016-2017     6448  27390     1608
2017-2018     6355  25438     1822
1997-1998     2988  11897     1212
1998-1999     6033  27660     2482
1999-2000     5924  28421     2418
2000-2001     5631  29539     2298
2001-2002     5875  30295     2481
2002-2003     7434  27141     1940
2003-2004     5673  27986     2053
2004-2005     5695  30070     1879
2005-2006     6099  26031     1903
2006-2007     7038  25845     1889
2007-2008     6671  21009     1736
2008-2009     6046  17768     1791
2009-2010     5496  18974     1934
2010-2011     5666  18458     1726
2011-2012     4933  14157     1748
2012-2013     4972  16849     1962
2013-2014     5328  18819     1762
2014-2015     5909  21915     1341
2015-2016     6067  21891     1354
2016-2017     6448  27390     1608
2017-2018     6355  25438     1822

饼图如下所示:

enter image description here

1 个答案:

答案 0 :(得分:1)

我生成了一个示例9 x 3数据帧和一个3 x 3子图,然后一次填充饼图。

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randint(low=0, high=10, size=(9, 3)),
      columns=['a', 'b', 'c'])

fig, axes = plt.subplots(3,3, figsize=(12,8))
for i in range(int(len(df.index)/3)):
    for j in range(int(len(df.index)/3)):
        idx = i * 3 + j
        ax = axes[i][j]
        ax.pie(df.loc[idx],shadow=True, labels=df.columns, autopct='%.2f%%')
        ax.set(ylabel='', title=idx, aspect='equal')
axes[0][0].legend(bbox_to_anchor=(0, 0.5))

plt.show()