I have two pandas series. Each of them have five indexes, ranging ("Very Low", "Low", "Medium, "High", "Very High"). The series are programmatically created by splitting a data frame into two demographics and then getting value counts for a particular column.
The two series were created by taking a pandas data frame, splitting based on whether each row was added by a particular demographic or not, extracting the value counts on one column.
demog1, demog2 = (df[df.Demographic == 'demog'], df[df.Demographic != 'demog'])
save_pie_chart(demog1, 'Column Name', 'demog1.png')
save_pie_chart(demog2, 'Column Name', 'demog2.png')
When I create a pie chart from the series, each one has the indexes placed in different positions and different colors. I want to make sure that both times they have the same relative positions and the same color.
The current method I'm using is to manually order the labels and data like this:
def save_pie_chart(df, col, path):
series = df[col].value_counts()
if 'Medium' in series.index:
labels = []
sizes = []
for label in ['Very low', 'Low', 'Medium', 'High', 'Very high']:
labels.append(label)
sizes.append(series[label])
series = sizes
else:
labels = series.keys()
fig, ax = plt.subplots()
ax.pie(series, labels=labels, autopct='%1.0f%%')
ax.axis('equal')
fig.savefig(path)
In the above, if I print out the series
variable after extracting the value counts, I get the following output:
Low 15
Very high 14
Very low 11
Medium 11
High 10
Name: Column Name, dtype: int64
High 95
Low 89
Medium 85
Very low 85
Very high 85
Name: Column Name, dtype: int64
Is there a built in way for me to tell matplotlib, "I want the labels in this order," and pass it the labels?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.