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!