使用groupby后,从熊猫图中删除“无”文本

时间:2019-01-24 22:46:39

标签: python pandas matplotlib

我有以下数据:

d_data = {"Name":["Jean","Luis",   "Ana", "Ana",  "Jean",  "Luis","Ana",    "Ana",  "Luis","Ana","Luis" ],
          "Money":[100,   2220,       333,   3342,  4423,  43,   5454,     4455,    421,   996,3102],
          "Type":["Cash","Cash","Cash","Cash","Digital","Cash","Digital","Cash","Cash","Digital","Digital"]}

我想为此绘制一个条形图,说明每个人的钱财类型。 为此,我正在使用以下代码:

df= pd.DataFrame(d_data)
df.groupby(["Name","Type"]).sum().unstack().plot(kind='bar', stacked=True) 

产生以下情节:

enter image description here

在此情节中,我想删除图例中的双标签:“无”和“仅保留货币”类型:“数字”和“现金”

2 个答案:

答案 0 :(得分:1)

这是解决问题的一种方法。 ax.get_legend_handles_labels()从轴对象返回图例手柄和标签。如果您打印labels,则会看到None, Type未打印。

现在,您可以剥离标签以仅获取“现金”和“数字”,并使用列表推导将其存储在labels_new中。

因此,当您再次使用plt.legend(handles, labels_new)设置图例时,只会得到所需的图例。可能还有其他直接方法,但是以下解决方案也可以。

fig, ax = plt.subplots()

d_data = {"Name":["Jean","Luis",   "Ana", "Ana",  "Jean",  "Luis","Ana",    "Ana",  "Luis","Ana","Luis" ],
          "Money":[100,   2220,       333,   3342,  4423,  43,   5454,     4455,    421,   996,3102],
          "Type":["Cash","Cash","Cash","Cash","Digital","Cash","Digital","Cash","Cash","Digital","Digital"]}

df= pd.DataFrame(d_data)
df.groupby(["Name","Type"]).sum().unstack().plot(kind='bar', stacked=True, ax=ax) 

handles, labels = ax.get_legend_handles_labels()
labels_new = [label.strip('()').split(',')[1] for label in labels]
plt.legend(handles, labels_new)

enter image description here

答案 1 :(得分:1)

也许您想使用pivot_table而不是groupbyunstack。那不会创建任何多索引,并且不会产生奇怪的图例。

import matplotlib.pyplot as plt
import pandas as pd

d_data = {"Name":["Jean","Luis",   "Ana", "Ana",  "Jean",  "Luis","Ana",    "Ana",  "Luis","Ana","Luis" ],
          "Money":[100,   2220,       333,   3342,  4423,  43,   5454,     4455,    421,   996,3102],
          "Type":["Cash","Cash","Cash","Cash","Digital","Cash","Digital","Cash","Cash","Digital","Digital"]}

df= pd.DataFrame(d_data)

pd.pivot_table(df, values="Money", index="Name", columns="Type", aggfunc="sum").plot(kind='bar', stacked=True) 

plt.show()

enter image description here