Skip to main content
 首页 » 编程设计

python之如何使用颜色在直方图上表示类别

2024年11月24日8zengkefu

我在数据集中有一列,我使用 pyplot.hist 图表示。垃圾箱使用灰色。 还有一个名为“类”的专栏,我想以此为基础划分情节。 “类别”列中有两个类别(0 和 1)。 我想用灰色表示 0 类数据,用黑色表示 1 类数据。如何做到这一点

以下是我尝试过的剧情编排

import matplotlib.pyplot as plt 
x= subdf['V12'] 
y= subdf['Class'] 
plt.figure(figsize=(25,10)) 
plt.hist(x,bins=100,color='grey') 
plt.show() 

如何修改以在图上显示两个不同的类别? 或者有没有其他情节可以用来轻松实现我的动机?

请您参考如下方法:

import seborn as sns 
import matplotlib.pyplot as plt 
fig=sns.FacetGrid(subdf,hue="Class",height=5,palette=["black", "grey"]) 
fig.map(sns.distplot,"V12") 
fig.add_legend() 
plt.show()