GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • PYTHON CHARTS
  • matplotlib
  • seaborn
  • plotly
  • pandas
  • colors
  • COLORS
  • COLOR PALETTES
  • R CODER
  • R CHARTS
  • Contact
  • Español

This view

  • Original page
  • New address

GeezerVison is showing python-charts.com — Kernel density plot in seaborn with kdeplot

Search for a graph

HOME DISTRIBUTION KERNEL DENSITY PLOT SEABORN

Kernel density plot in seaborn with kdeplot

Basic kernel density plot in seaborn with kdeplot

The kdeplot function from seaborn calculates a kernel density estimate of the data and plots it. By default the function uses a gaussian kernel, 200 points as grid for the X-axis and a bandwidth factor of 1 for the Scott method.

import numpy as np
import seaborn as sns

# Data simulation
rng = np.random.RandomState(4)
x = rng.normal(0, 1, size = 100)
df = {'x': x}

# KDE plot
sns.kdeplot(x = x)

# Equivalent to:
sns.kdeplot(x = "x", data = df)

Kernel density estimate plot in Python with seaborn

It is possible to modify the default color of the density with the color argument of the function. Note that you could also modify the line type or width with linestyle and linewidth, respectively.

import numpy as np
import seaborn as sns

# Data simulation
rng = np.random.RandomState(4)
x = rng.normal(0, 1, size = 100)

# KDE plot
sns.kdeplot(x = x, color = "red")

Change the color of a density plot in seaborn with kdeplot

Moreover, you can also fill the area under the density plot with fill = True and specify the desired color and transparency with color and alpha, respectively.

import numpy as np
import seaborn as sns

# Data simulation
rng = np.random.RandomState(4)
x = rng.normal(0, 1, size = 100)

# KDE plot
sns.kdeplot(x = x,
            fill = True, color = "green", alpha = 0.5)

Fill the area under the density plot in seaborn

Bandwidth selection

As we pointed out before, the function uses a bandwidth factor of 1 by default for the Scott method, but you can override it with the bw_adjust argument. You can also use the Silverman method instead of the Scott method with the bw_method argument. See this documentation for more details.

Bandwidth selection in kdeplot

import numpy as np
import seaborn as sns

# Data simulation
rng = np.random.RandomState(4)
x = rng.normal(0, 1, size = 100)

# KDE plot
sns.kdeplot(x = x,
            bw_adjust = 0.5)

Silverman method for bandwidth selection in seaborn

Silverman method

import numpy as np
import seaborn as sns

# Data simulation
rng = np.random.RandomState(4)
x = rng.normal(0, 1, size = 100)

# KDE plot
sns.kdeplot(x = x,
            bw_method = "silverman")

Kernel selection was deprecated in 0.11.0, so the support of non-gaussian kernels was removed.

R CODER website
Fundamentals of Data Visualization

Fundamentals of Data Visualization

A Primer on Making Informative and Compelling Figures

Buy on Amazon
Better Data Visualizations

Better Data Visualizations

A Guide for Scholars, Researchers, and Wonks

Buy on Amazon

See also

Boxenplot (letter-value plot) in seaborn

Boxenplot (letter-value plot) in seaborn

Box plot in matplotlib

Box plot in matplotlib

Histogram with density in seaborn

Histogram with density in seaborn

Box plot in pandas

Box plot in pandas

Swarm plot in seaborn

Swarm plot in seaborn

Ridgeline plots (joy plots) in matplotlib with joypy

Ridgeline plots (joy plots) in matplotlib with joypy

Violin plots in plotly

Violin plots in plotly

Histogram by group in seaborn

Histogram by group in seaborn

Box plot in plotly

Box plot in plotly

Box plot in seaborn

Box plot in seaborn

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.