微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

更改 KDE matplotlib.plot 的颜色和边界顺序

如何解决更改 KDE matplotlib.plot 的颜色和边界顺序

df.groupby("class").q1.plot(kind='kde')
plt.title('Age')
plt.xlabel('score obtained in age')
plt.ylabel('Density')
plt.legend()
plt.savefig(f'Analysis/q1-kde.png')
plt.close()
label_order=["Very Low","Low",'Average','High','Very High']

并得到类似的东西:

enter image description here

但是,图例标签按字母顺序排序。我希望图例和 kde 行的顺序与 label_order

中定义的一样

解决方法

categorical dtype 与ordered 一起使用:

label_order=["Very Low","Low",'Average','High','Very High']
df['class'] = pd.Categorical(df['class'],categories=label_order,ordered=True)

df.groupby("class").q1.plot(kind='kde')

plt.title('Age')
plt.xlabel('Score obtained in age')
plt.ylabel('Density')
plt.legend()

输出:

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。