Home > Blockchain >  How to create a pie-chart from pandas DataFrame?
How to create a pie-chart from pandas DataFrame?

Time:01-18

I have a dataframe, with Count arranged in decending order, that looks something like this:

df = pd.DataFrame({'Topic': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M'],
                   'Count': [80, 75, 70, 65, 60, 55, 50, 45, 40, 35, 30, 25, 20]})

But with more than 50 rows.

I would like to create a pie chart for the top 10 topics and rest of them to be summed up and represent its percentange as label "Others" in the pie chart. Is it possible to exclude the pie labels against each pie, and mention them seperately in a legend?

Thanking in anticipation

CodePudding user response:

Replace Topic by Other if no top N in pie chart pie chart N=5

Percentages in the legend:

N = 5
df2 = df.iloc[:N]
df2 = df2.append({'Topic': 'Other', 'Count': df['Count'].iloc[N:].sum()}, ignore_index=True)

df2.set_index('Topic').plot.pie(y='Count', legend=False)
leg = plt.legend(labels=df2['Count'])

output:

plot with legend

CodePudding user response:

import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({'Topic': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M'],
                   'Count': [80, 75, 70, 65, 60, 55, 50, 45, 40, 35, 30, 25, 20]})
df.index = df.Topic
plot = df.plot.pie(y='Count', figsize=(5, 5))
plt.show()

Use documentation: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.plot.pie.html

  •  Tags:  
  • Related