简体   繁体   English

如何绘制直方图的密度而非计数? (Matplotlib)

[英]How do I plot my histogram for density rather than count? (Matplotlib)

I have a data frame called 'train' with a column 'string' and a column 'string length' and a column 'rank' which has ranking ranging from 0-4. 我有一个名为'train'的数据框,其中包含列'string'和列'string length'以及列'rank',其排名范围为0-4。

I want to create a histogram of the string length for each ranking and plot all of the histograms on one graph to compare. 我想为每个排名创建一个字符串长度的直方图,并在一个图表上绘制所有直方图以进行比较。 I am experiencing two issues with this: 我遇到了两个问题:

The only way I can manage to do this is by creating separate datasets eg with the following type of code: 我可以设法做到这一点的唯一方法是创建单独的数据集,例如使用以下类型的代码:

S0 = train.loc[train['rank'] == 0]
S1 = train.loc[train['rank'] == 1]

Then I create individual histograms for each dataset using: 然后我使用以下方法为每个数据集创建单独的直方图:

plt.hist(train['string length'], bins = 100)
plt.show()

This code doesn't plot the density but instead plots the counts. 此代码不绘制密度,而是绘制计数。 How do I alter my code such that it plots density instead? 如何更改我的代码,使其绘制密度?

Is there also a way to do this without having to create separate datasets? 还有一种方法可以做到这一点,而无需创建单独的数据集? I was told that my method is 'unpythonic' 我被告知我的方法是'unpythonic'

You could do something like: 你可以这样做:

df.loc[:, df.columns != 'string'].groupby('rank').hist(density=True, bins =10, figsize=(5,5))

Basically, what it does is select all columns except string , group them by rank and make an histogram of all them following the arguments. 基本上,它所做的是选择除string以外的所有列,按rank对它们进行分组,并根据参数制作所有列的直方图。

The density argument set to density=True draws it in a normalized manner, as 设置为density=True的density参数以标准化方式绘制,如

Hope this has helped. 希望这有所帮助。

EDIT : 编辑

f there are more variables and you want the histograms overlapped, try: 如果有更多变量,并且您希望直方图重叠,请尝试:

df.groupby('rank')['string length'].hist(density=True, histtype='step', bins =10,figsize=(5,5))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM