简体   繁体   中英

Is there an easy way to ensure that two pie plots of two pandas series with identical indexes plot in the same order?

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?

You can use value_counts(sort=False) and df.plot.bar :

axes = (df.groupby('Demographic')['Medium'].value_counts()
   .unstack('Demographic')
   .plot.pie(subplots=True, autopct='%1.0f%%')
)

for ax in axes: ax.get_legend().remove()

Output:

在此处输入图像描述

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM