我有一系列饼图的情节和类别(标签)是字符串。即使标签是字符串而不是整数,我如何仍然绘制饼图?
df = pd.DataFrame({'Kanton': ['AG', 'AG', 'AG', 'AG', 'AG', 'AG', 'AG', 'ZH', 'ZH', 'ZH', 'ZH', 'ZH', 'ZH'],
'Kat': ['1b','4','5','6','1c','4','2a','3','3','3','2b','5','1d']})
kantone=df['Kanton'].unique()
for i in kantone:
#print(i)
df_n=df.loc[df['Kanton'] == i, 'Kat'].value_counts()
#print(df_n)
#define data
data = df_n
labels = df_n.index.values
colors = sns.color_palette("bright", len(df['Kat'].unique()))
#plot
plt.pie(data, labels = labels, colors=[colors[k-1] for k in labels], autopct='%.0f%%')
plt.title("Kanton " + i)
plt.show()
1条答案
按热度按时间t1qtbnec1#
您可以:
此代码使用一个颜色Map表来保持所有图表上每个标签的颜色相同。
输出: