为分类变量python

时间:2019-03-21 14:49:55

标签: python pandas charts colors

我有3列的数据框,例如:

>  A        B    C 
  red     yes   100
  red     no     25
  blue    yes   200
  blue    no     20
  green   yes    40
  green   no     10
  yellow  yes    40
  yellow  no     20

我想为A列的B列的每个答案制作一个饼形图,并在其部分上赋予与其分配的标签相同的颜色。

例如,我希望饼图上分配给红色标签的部件使用红色,将蓝色分配为蓝色,等等。 有时标签可能不是颜色,但我想选择要分配给该标签的颜色。

预期输出

enter image description here

我尝试了以下代码:

import pandas as pd
import matplotlib.pyplot as plt

df_bis = df.groupby(['A','B'], axis = 0).agg('count') 

df_bis['C'].plot(kind='pie',
                    figsize=(5,4),
                    subplots=True,
                    autopct='%1.1f%%', # add in percentages
                    startangle=90,     # start angle 90° 
                    shadow=True,       # add shadow         
                    colors = 
                 {'red':"red",'blue':"blue",'yellow':"gold",'green':"green"}

                       )                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
             plt.axis('equal') # Sets the pie chart to look like a circle.

但是它不起作用。

您有实现这个想法吗? 谢谢

2 个答案:

答案 0 :(得分:0)

在这里,尝试一下:

import pandas as pd
import matplotlib.pyplot as plt

data_frame = pd.read_csv("test.csv")

df_yes = data_frame.loc[data_frame['B']=='yes',:]
df_no = data_frame.loc[data_frame['B']=='no',:]
fig, (ax1, ax2) = plt.subplots(1, 2)

# plot each pie chart in a separate subplot
ax1.pie(df_yes["C"],  labels=df_yes["A"], autopct='%1.1f%%',
        shadow=True, colors=df_yes["A"])
ax1.set_title('Yes')

ax2.pie(df_no["C"],  labels=df_no["A"], autopct='%1.1f%%',
        shadow=True, colors=df_no["A"])
ax2.set_title('No')

plt.show()


测试CSV如下所示:
enter image description here
结果如下:
enter image description here

注意:您可以调整图表(播放文字颜色,图形大小,图表样式等)并根据需要添加图例。我还没有添加图例,而是在饼图上留下了标签。

您可以删除标签(labels=df_yes["A"])并将以下代码添加到每个图表中以生成图例:

ax1.legend(labels=df_yes["A"], loc="upper center")

答案 1 :(得分:0)

colors必须像列表一样,其中每个元素都是与一块饼相关的颜色。使用字典,您可以将colors设置为:

color_dict = {'red':"red",'blue':"blue",'yellow':"gold",'green':"green"}
df_bis['C'].plot(kind='pie',
    ...
    colors=[color_dict[c] for c in df_bis['A']]
)
...