19

I'd like to have a weights option in seaborn distplot, similar to that in numpy histogram. Without this option, the only alternative would be to apply the weighting to the input array, which could result in an impractical size (and time).

JohnE
  • 29,156
  • 8
  • 79
  • 109
nbecker
  • 1,645
  • 5
  • 17
  • 23

2 Answers2

14

You can provide weights by passing them to the underlying matplotlib's histogram function using the hist_kws argument, as:

sns.distplot(..., hist_kws={'weights': your weights array}, ...)

Take note though, that the weights will be passed only to the underlying histogram; neither the kde, nor the fit functions of the distplot will be affected.

Smajl
  • 7,555
  • 29
  • 108
  • 179
vlasisva
  • 141
  • 3
5

As @vlasisla already mentioned in their answer, weights should be provided through the keyword argument hist_kws so they would be passed to mathpolotlib's hist function. Though, this will not make any effect unless kde (kernel density estimation) option is disabled at the same time. This code would actually have a desired effect:

sns.distplot(x, hist_kws={'weights': x_weights}, kde=False)

To understand why both weights and kde are not allowed, let's consider the following example, where x_weights is calculated as x_weights = np.ones_like(x) / len(x) so that all bins' heights sum to 1:

# generate 1000 samples from a normal distribution
np.random.seed(8362) 
x = np.random.normal(size=1000)

# calculate weights
x_weights = np.ones_like(x) / len(x)

# figure 1 - use weights
sns.distplot(x, hist_kws={'weights': x_weights}, kde=False)
# figure 2 - default plot with kde
sns.distplot(x)

Figure 1. Using dist with weights and not KDE Figure 2. Using dist with default parameters

In Figure 1 we provided dist function with weights, so in this figure all bins' heights sum to 1. In Figure 2 the default behaviour of dist is enabled, so the area under the KDE function sums to 1 and bins' heights are normalised correspondingly. It can be easily seen now, that plotting KDE when weights are provided indeed would not make much sense.

myrs
  • 627
  • 1
  • 7
  • 11