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

标签 python matplotlib

我在数据集中有一列,我使用 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()

关于python - 如何使用颜色在直方图上表示类别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57087070/

相关文章:

python - 从 Flask 请求模块读入文件

python - 如何从时间序列重采样中获取列内的类别计数

python - 以月为单位绘制 x 轴

python - 在 html 文件中使用图表(使用 matplotlib 用 python 编写)

python - 方程对同一个变量返回不同的值

Python:创建大小为 n^2 的元组的时间和空间复杂度

python - 寻找在 3600x20x20 的 xarray 中查找阈值之间的值的最快方法

python - 为什么 django admin 有时会在索引中使用模型的某些字段,而不是 __unicode__ 方法?

python - matplotlib 搞乱了 float Axis

python - 在 Matplotlib 中用上标或下标编写单位的最佳做法是什么?