1
votes

I'm trying to do something pretty simple, but I'm a novice Python user so this has proven a little difficult for me. I'm trying to make pie charts and the labels for the charts are appearing for the wrong slices of pie.

Here is my code:

import matplotlib.pyplot as plt

# Data to plot
labels = data['Category'].unique()
sizes = (data['Category'].value_counts()/data['Category'].value_counts().sum())*100

# Plot
plt.pie(sizes, labels=labels,
autopct='%1.1f%%', shadow=True, startangle=140)

plt.axis('equal')
plt.legend(labels, loc="best")
plt.tight_layout()

plt.show()

This code produces a pie chart, but the labels in the legend do not match the labels on the chart. I've identified that this is due to the order with which the values of 'Category' appear in the data differ from the order of the values in the line where I define "sizes".

Does anyone know how to sync sizes and labels so the appropriate label is shown on the pie chart?

Any help would be appreciated! Thank you!

1

1 Answers

2
votes

You can use labels = sizes.index so both will have the same order. If you want the labels sorted, you could first call sizes = sizes.sort_index(). Or, to have them sorted by value: sizes = sizes.sort_values(). Default, they would be sorted in order of appearance.

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

data = pd.DataFrame({'Category': np.random.choice(list('abcdefghij'), 500)})
sizes = data['Category'].value_counts().sort_index() / data['Category'].value_counts().sum() * 100
plt.pie(sizes, labels=sizes.index,
        autopct='%1.1f%%', shadow=True, startangle=140)
plt.axis('equal')
plt.legend(sizes.index, loc="best")
plt.tight_layout()
plt.show()

example plot