python - seaborn 中的直方图 bin 大小

标签 python matplotlib histogram seaborn

我正在使用 Seaborn 的 FacetGrid 绘制一些直方图,我认为自动 bin 大小调整只使用每个类别的数据(而不是每个子图),这会导致一些奇怪的结果(请参阅 中的瘦绿色 bins y = 2):

g = sns.FacetGrid(df, row='y', hue='category', size=3, aspect=2, sharex='none')
_ = g.map(plt.hist, 'x', alpha=0.6)

Seaborn histogram

有没有办法(使用 Seaborn,而不是回退到 matplotlib)使每个图的直方图 bin 大小相等?

我知道我可以手动指定所有 bin 宽度,但这会强制所有直方图具有相同的 x 范围(参见笔记本)。

笔记本:https://gist.github.com/alexlouden/42b5983f5106ec92c092f8a2697847e6

最佳答案

您需要为 plt.hist 定义一个包装函数,它自己进行色调分组,例如

%matplotlib inline

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")
tips.loc[tips.time == "Lunch", "total_bill"] *= 2

def multihist(x, hue, n_bins=10, color=None, **kws):
    bins = np.linspace(x.min(), x.max(), n_bins)
    for _, x_i in x.groupby(hue):
        plt.hist(x_i, bins, **kws)

g = sns.FacetGrid(tips, row="time", sharex=False)
g.map(multihist, "total_bill", "smoker", alpha=.5, edgecolor="w")

enter image description here

关于python - seaborn 中的直方图 bin 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42264924/

相关文章:

python - 如何从 Scikit-learn 中的拟合模型获取属性列表?

python - 如何获取matplotlib中所有标记的列表?

r - 无法在直方图上添加概率曲线

c++ - c++ 中的直方图代码显示了一些疯狂的意外数字

python Pandas : How I can determine the distribution of my dataset?

python - qtreewidget 不需要的空白(额外的不可删除的行)

python - 一行中的所有匹配项 : Spacy matcher

python - matplotlib 中的最后一个图形标记未完全显示

python - Django,Mezzanine : Autodiscover error, 引用不存在的模型

python - 使用 matplotlib 绘制 pandas 数据框时出现 KeyError