1

I have dataframe with 3 columns like:

>  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

I would like to make a pie chart for each answer at column B by column A and give the same color at the part than the label which it assigned.

For example, I would like the color red for the part on pie chart assign at red label, blue for blue, etc. Sometimes the label couldn't be a colour but I want to choose the color to assign at that label.

EXPECTED OUTPOUT:

enter image description here

I tried this code:

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.

But it doesn't work.

Have you an idea to make that? Thanks

Jason Aller
  • 3,541
  • 28
  • 38
  • 38

2 Answers2

0

Here, Try this:

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


Where the Test CSV Looks like following:
enter image description here
And the result Looks like:
enter image description here

Note: You can tweak the graph (Play with text color, figure size, chart style etc) and add legends based on your requirement. I have not added legend and I left the labels on the pie chart.

You can remove the labels (labels=df_yes["A"]) and add following code to each charts to generate legends:

ax1.legend(labels=df_yes["A"], loc="upper center")
BetaDev
  • 4,516
  • 3
  • 21
  • 47
  • thks for you answer @webDev. But like i wrote on the post, you can have an item on column A who couldn't be a color, so the code 'colors = df_yes["A"]' won't work because the item is not a color. I search a mean to assign different color for each item. – BENAMARA ILAN Mar 21 '19 at 15:32
0

colors needs to be list-like where each element is the color associated with a piece of the pie. Using your dictionary, you can set colors to:

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']]
)
...
busybear
  • 10,194
  • 1
  • 25
  • 42
  • thks @busybear a lot it work. I just have last question. how can i raise the size of my pie chart and space between the 2 chart ? Thks a lot – BENAMARA ILAN Mar 21 '19 at 16:55
  • Glad to hear it works! The pie chart will just fill the Axes it's in. If you want to adjust the spacing between Axes (pie charts) [this](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.subplots_adjust.html) might be useful. To change the entire figure size, refer to this [post](https://stackoverflow.com/questions/14770735/changing-figure-size-with-subplots). – busybear Mar 21 '19 at 17:10