Histogram

Seaborn basics

2 min read

Published Oct 7 2025


24
0
0
0

ChartsGraphsMatplotlibNumPyPandasPythonSeabornVisualisation

seaborn.histplot() visualises the distribution of one or two numerical variables using histograms (and optionally KDE curves).


Syntax:

sns.histplot(
    data=None,
    x=None,
    y=None,
    hue=None,
    stat="count",
    bins="auto",
    binwidth=None,
    discrete=False,
    cumulative=False,
    common_bins=True,
    common_norm=True,
    multiple="layer",
    element="bars",
    fill=True,
    shrink=1,
    kde=False,
    palette=None,
    ax=None,
    **kwargs
)

Parameters:

  • data = DataFrame containing data
  • x, y = Variables for histogram axes
  • hue = Colour by category
  • stat = What the height of bars represents: "count", "frequency", "probability", "percent", "density"
  • bins = Number of bins or bin edges
  • binwidth = Width of each bin
  • multiple = How hue groups are displayed ("layer", "stack", "dodge", "fill")
  • element = "bars", "step", or "poly" for different visual styles
  • fill = Whether to fill the bars
  • kde = Add a kernel density estimate curve
  • palette = Colour scheme
  • discrete = If True, treats x as categorical/discrete values
  • cumulative = If True, shows cumulative counts
  • shrink = Adjusts bar width when multiple hue categories are used




Basic example

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")

sns.histplot(data=tips, x="total_bill")
plt.show()

Creates a simple histogram showing how total_bill values are distributed.


seaborn histogram plot basic example





Add a KDE curve (smoothed distribution)

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")

sns.histplot(data=tips, x="total_bill", kde=True)
plt.show()

Adds a smooth density curve to visualise the probability distribution.


seaborn histogram plot kde example





Color by category (hue)

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")

sns.histplot(data=tips, x="total_bill", hue="sex", palette="Set2")
plt.show()

Different colours for each group (male vs. female).


seaborn histogram plot hue example





Display hue groups separately

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")

sns.histplot(
    data=tips,
    x="total_bill",
    hue="sex",
    multiple="dodge",
    shrink=0.8,
    palette="pastel"
)
plt.show()

Options for multiple:

  • "layer" = Overlays bars (default)
  • "dodge" = Side-by-side bars
  • "stack" = Stacks bars on top of each other
  • "fill" = Stacks but normalises to 100% height

seaborn histogram plot multiple hue example





Adjust number or width of bins

Control how detailed the histogram is.


Number of bins:

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")

sns.histplot(data=tips, x="total_bill", bins=20)
plt.show()

seaborn histogram plot num bins example


Width of bins:

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")

sns.histplot(data=tips, x="total_bill", binwidth=5)
plt.show()

seaborn histogram plot bin width example





Normalise or change stat type

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")

sns.histplot(data=tips, x="total_bill", stat="percent", bins=20)
plt.show()

Bar height shows percentages instead of raw counts.


Options for stat:

  • "count" (default)
  • "frequency"
  • "probability"
  • "percent"
  • "density"

seaborn histogram plot stat percentage example





2D histogram (Bivariate Distribution)

You can plot two numeric variables with both x and y.

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")

sns.histplot(data=tips, x="total_bill", y="tip")
plt.show()

Creates a 2D histogram (heatmap-like) plot showing joint frequency.


seaborn histogram plot 2D example





Add hue with 2D data

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")

sns.histplot(data=tips, x="total_bill", y="tip", hue="sex", palette="coolwarm")
plt.show()

Adds color separation by category.


seaborn histogram plot 2D hue example





Change style

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")

sns.histplot(
    data=tips,
    x="total_bill",
    hue="sex",
    element="step",
    fill=False,
    palette="Set1"
)
plt.show()

Options for element:

  • "bars" = Default filled bars
  • "step" = Outlined (no fill)
  • "poly" = Polygon-style shape

seaborn histogram plot style example





Discrete variable histogram

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")

sns.histplot(data=tips, x="size", discrete=True)
plt.show()

Ensures each category/bin is represented individually.


seaborn histogram plot discrete example





Cumulative distribution

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")

sns.histplot(data=tips, x="total_bill", cumulative=True)
plt.show()

Bars accumulate counts progressively from left to right.


seaborn histogram plot cumulative example





Logarithmic scale

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")

sns.histplot(data=tips, x="total_bill", log_scale=True)
plt.show()

Useful when data spans several orders of magnitude.


seaborn histogram plot log example





Combine with KDE plot for clarity

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")

sns.histplot(data=tips, x="total_bill", kde=True, bins=20, color="skyblue")
plt.show()

Combines discrete histogram bars and smooth density curve.


seaborn histogram plot combined kde example
© 2025 SimpleSteps.guide
AboutFAQPoliciesContact