0

I want to create a Pie chart using single column of my dataframe, say my column name is 'Score'. I have stored scores in this column as below :

Score

.92
.81
.21
.46
.72
.11
.89

Now I want to create a pie chart with the range in percentage. Say 0-0.4 is 30% , 0.4-0.7 is 35 % , 0.7+ is 35% . I am using the below code using

df1['bins'] = pd.cut(df1['Score'],bins=[0,0.5,1], labels=["0-50%","50-100%"])
df1 = df.groupby(['Score', 'bins']).size().unstack(fill_value=0)

df1.plot.pie(subplots=True,figsize=(8, 3))

With the above code I am getting the Pie chart, but i don’t know how i can do this using percentage.

my pie chart look like this for now

Tim
  • 3,178
  • 1
  • 13
  • 26
Aditya
  • 15
  • 2
  • 6
  • I don't understand what it should look like. Where are the percentages coming from? – Jondiedoop Jan 26 '19 at 14:40
  • My query is that only...I want to use percentage instead...ther percentage i mentioned in my question is just an example. – Aditya Jan 26 '19 at 15:05
  • Are you asking how you can make a slice represent the relative sixe as to how many "scores" fit within a particular range? Say, from your data, 2/7th of the data is between 0 and .30, so a slice of this range should be 2/7 of the whole circle.. Do I understand you correctly? – RoyM Jan 26 '19 at 15:47
  • yeah lets consider the exact data that i have given, so i want to create pie chart with three slices ranges (0- 0.4, 0.4-0.8, 0.8-1), 1 slice should tell about the percentage of data coming in first range i.e 2/7, 2nd slice should tell about the percentage of data in in 2nd range i.e 2/7 again, 3rd one will contain 3/7. – Aditya Jan 26 '19 at 16:17

1 Answers1

0

Cutting the dataframe up into bins is the right first step. After which, you can use value_counts with normalize=True in order to get relative frequencies of values in the bins column. This will let you see percentage of data across ranges that are defined in the bins.

In terms of plotting the pie chart, I'm not sure if I understood correctly, but it seemed like you would like to display the correct legend values and the percentage values in each slice of the pie.

pandas.DataFrame.plot is a good place to see all parameters that can be passed into the plot method. You can specify what are your x and y columns to use, and by default, the dataframe index is used as the legend in the pie plot.

To show the percentage values per slice, you can use the autopct parameter as well. As mentioned in this answer, you can use all the normal matplotlib plt.pie() flags in the plot method as well.

Bringing everything together, this is the resultant code and the resultant chart:

df = pd.DataFrame({'Score': [0.92,0.81,0.21,0.46,0.72,0.11,0.89]})
df['bins'] = pd.cut(df['Score'], bins=[0,0.4,0.7,1], labels=['0-0.4','0.4-0.7','0.7-1'], right=True)

bin_percent = pd.DataFrame(df['bins'].value_counts(normalize=True) * 100)
plot = bin_percent.plot.pie(y='bins', figsize=(5, 5), autopct='%1.1f%%')

Plot of Pie Chart

Imma
  • 122
  • 1
  • 9