如何在Seaborn FacetGrid中“加权”直方图?

时间:2016-02-17 04:38:48

标签: python histogram seaborn

我正在尝试使用seaborn.FacetGrid绘制一个严重的直方图。但是我没有将参数'weights'传递给函数。 该示例来自How to pass weights to a Seaborn FacetGrid

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

d = pd.DataFrame(np.array([np.random.randint(0, 6, 5000), 
                       np.random.normal(0, 1., 5000),
                       np.random.uniform(0, 1, 5000)]).T, 
             columns=('cat', 'val', 'weight'))

使用seaborn FacetGrid绘制直方图:

fg = sns.FacetGrid(d, col='cat', col_wrap=3)
fg.map(plt.hist, 'val', weights=d.weight)

ValueError: weights should have the same shape as x

我试图定义一个函数:

def weighted_hist(x, weights, **kwargs):
    plt.hist(x, weights=weights, **kwargs)   

fg = sns.FacetGrid(d, col='cat', col_wrap=3)
fg.map(weighted.hist, 'val', weights='weight')

ValueError: weights should have the same shape as x

无论如何都要将参数权重传递给函数吗?

1 个答案:

答案 0 :(得分:0)

问题解决了。我的matplotlib出了问题,我重新安装了它。