![](/img/trans.png)
[英]Is there an easy way to plot two categorical plots on the same FacetGrid in Seaborn?
[英]Is there an easy way to ensure that two pie plots of two pandas series with identical indexes plot in the same order?
我有兩個 pandas 系列。 它們每個都有五個索引,范圍(“非常低”、“低”、“中”、“高”、“非常高”)。該系列是通過將數據框分成兩個人口統計數據然后獲取值計數以編程方式創建的特定的列。
這兩個系列是通過采用 pandas 數據框創建的,根據每一行是否由特定人口統計添加,提取一列上的值計數。
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')
當我從該系列創建一個餅圖時,每個餅圖都有放置在不同位置和不同 colors 的索引。 我想確保它們兩次都具有相同的相對位置和相同的顏色。
我正在使用的當前方法是手動排序標簽和數據,如下所示:
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)
在上面,如果我在提取值計數后打印出series
變量,我得到以下 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
是否有一種內置方式讓我告訴 matplotlib,“我想要按此順序排列的標簽”,然后將標簽傳遞給它?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.