【问题标题】:How to Display Pie Chart Year wise in Pandas如何在 Pandas 中按年显示饼图
【发布时间】:2018-01-29 09:03:42
【问题描述】:

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

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()

但我已经检查了指向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

饼图如下所示:

【问题讨论】:

  • 你想要的输出是什么?
  • 就像图一一样,它必须显示从 1997 年到 2017 年的数据以及年份。

标签: python pandas numpy charts


【解决方案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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-29
    • 1970-01-01
    • 1970-01-01
    • 2022-12-23
    • 2023-02-04
    • 1970-01-01
    相关资源
    最近更新 更多