- class seaborn.objects.Hist(stat='count', bins='auto', binwidth=None, binrange=None, common_norm=True, common_bins=True, cumulative=False, discrete=False)#
Bin observations, count them, and optionally normalize or cumulate.
Aggregate statistic to compute in each bin:
count: the number of observations
density: normalize so that the total area of the histogram equals 1
percent: normalize so that bar heights sum to 100
proportion: normalize so that bar heights sum to 1
frequency: divide the number of observations by the bin width
- binsstr, int, or ArrayLike
Generic parameter that can be the name of a reference rule, the number of bins, or the bin breaks. Passed to
Width of each bin; overrides
binsbut can be used with
- binrange(min, max)
Lowest and highest value for bin edges; can be used with either
bins(when a number) or
binwidth. Defaults to data extremes.
- common_normbool or list of variables
False, the normalization is applied across groups. Use
Trueto normalize across all groups, or pass variable name(s) that define normalization groups.
- common_binsbool or list of variables
False, the same bins are used for all groups. Use
Trueto share bins across all groups, or pass variable name(s) to share within.
If True, cumulate the bin values.
If True, set
binrangeso that bins have unit width and are centered on integer values
The choice of bins for computing and plotting a histogram can exert substantial influence on the insights that one is able to draw from the visualization. If the bins are too large, they may erase important features. On the other hand, bins that are too small may be dominated by random variability, obscuring the shape of the true underlying distribution. The default bin size is determined using a reference rule that depends on the sample size and variance. This works well in many cases, (i.e., with “well-behaved” data) but it fails in others. It is always a good to try different bin sizes to be sure that you are not missing something important. This function allows you to specify bins in several different ways, such as by setting the total number of bins to use, the width of each bin, or the specific locations where the bins should break.
For discrete or categorical variables, this stat is commonly combined with a
so.Plot(penguins, "island").add(so.Bar(), so.Hist())
When used to estimate a univariate distribution, it is better to use the
p = so.Plot(penguins, "flipper_length_mm") p.add(so.Bars(), so.Hist())
The granularity of the bins will influence whether the underlying distribution is accurately represented. Adjust it by setting the total number:
Alternatively, specify the width of the bins:
By default, the transform returns the count of observations in each bin. The counts can be normalized, e.g. to show a proportion:
When additional variables define groups, the default behavior is to normalize across all groups:
p = p.facet("island") p.add(so.Bars(), so.Hist(stat="proportion"))
common_norm=Falseto normalize each distribution independently:
p.add(so.Bars(), so.Hist(stat="proportion", common_norm=False))
Or, with more than one grouping varible, specify a subset to normalize within:
p.add(so.Bars(), so.Hist(stat="proportion", common_norm=["col"]), color="sex")
When distributions overlap it may be easier to discern their shapes with an
p.add(so.Area(), so.Hist(), color="sex")
Stackmove to represent a part-whole relationship:
p.add(so.Bars(), so.Hist(), so.Stack(), color="sex")