首页 > 解决方案 > 在 python 中构建一个直方图,其中一列作为 x 轴,三列作为 y 轴

问题描述

我有以下数据

method,RequirementT,RequirementN,RequirementU
1,1,7,0
2,0,0,8
3,2,6,0
4,1,7,0
5,2,6,0
6,2,6,0

这是我的完整数据的链接 https://drive.google.com/file/d/1aBRy2uf34kjWQAo8nUDMnuAEHPR4XBMD/view?usp=sharing

我想在 python 中构建一个直方图,使 x 轴对应于我的数据(方法)的第一列,而其他三列(RequirementT、RequirementN、RequirementU)在 y 轴上表示。我希望图表采用直方图的形式。

我已经尝试过df.plot.bar(x='method', y=['RequirementT', 'RequirementN', 'RequirementU']) ,这给了我以下输出,这显然是错误的,完全不可读,我不知道为什么 x 轴周围有这些粗黑线 在此处输入图像描述

标签: pythonhistogram

解决方案


这对我有用:

df.plot.bar(x='method', y=['RequirementT', 'RequirementN', 'RequirementU'])

在此处输入图像描述

您在寻找分布图吗?尝试这个:

import seaborn as sns
import matplotlib.pyplot as plt

df = df.set_index('method', drop=True)

sns.distplot(df.iloc[:, 0], bins=10) # just use .loc and insert your 3 columns instead of 0, 1, 2
sns.distplot(df.iloc[:, 1], bins=10)
sns.distplot(df.iloc[:, 2], bins=10)
plt.show()

推荐阅读