如何在seaborn中绘制离散变量的分布图 [英] How to draw distribution plot for discrete variables in seaborn

查看:104
本文介绍了如何在seaborn中绘制离散变量的分布图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我为离散变量绘制 displot 时,分布可能与我想象的不一样.例如.

When I draw displot for discrete variables, the distribution might not be as what I think. For example.

我们可以发现barplot中有裂缝,使得kdeplot中的曲线在y轴上较低".

We can find that there are crevices in the barplot so that the curve in kdeplot is "lower" in y axis.

在我的工作中,情况更糟:

In my work, it was even worse:

我认为这可能是因为每个条形的宽度"或重量"不是 1.但是我没有找到任何可以证明它的参数.

I think it may because the "width" or "weight" was not 1 for each bar. But I didn't find any parameter that can justify it.

我想画这样的曲线(应该更平滑)

I'd like to draw such curve (It should be more smooth)

推荐答案

如果问题是直方图中有一些空的 bin,那么指定 bin 来匹配数据可能是有意义的.在这种情况下,使用 bins=np.arange(0,16) 获取数据中所有整数的 bin.

If the problem is that there are some emptry bins in the histogram, it probably makes sense to specify the bins to match the data. In this case, use bins=np.arange(0,16) to get the bins for all integers in the data.

import numpy as np; np.random.seed(1)
import matplotlib.pyplot as plt
import seaborn as sns

n = np.random.randint(0,15,10000)
sns.distplot(n, bins=np.arange(0,16), hist_kws=dict(ec="k"))

plt.show()

这篇关于如何在seaborn中绘制离散变量的分布图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆