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 — Histogram by group in seaborn

Search for a graph

HOME DISTRIBUTION HISTOGRAM GROUP SEABORN

Histogram by group in seaborn

Data simulation

The following data represents two different variables stacked in a data frame. Each variable will represent a group which will define each histogram of the examples below.

import numpy as np
import pandas as pd

# Data simulation
rng = np.random.RandomState(1)
x1 = rng.normal(0, 1, size = 500)
x2 = rng.normal(3, 1.5, size = 500)
x = np.concatenate((x1, x2), axis = 0)
group = np.repeat(np.array(["G1", "G2"]), [500, 500], axis = 0)
df = {'x': x, 'group': group}

# Pandas data frame
df = pd.DataFrame(data = df)

Histogram by group

In order to create a histogram by group in seaborn you will need to pass your data to the histplot function and the grouping variable to the hue argument.

import seaborn as sns

# Histogram
sns.histplot(x = x, hue = group)

# Equivalent to:
sns.histplot(x = "x", hue = "group", data = df)

Histogram by group in seaborn

Dodge

Note that by default, the histograms could overlap, as in the previous example. Setting the multiple argument to "dodge" will avoid the overlapping making the bars of the histograms thinner.

import seaborn as sns

# Histogram
sns.histplot(x = x, hue = group,
             multiple = "dodge")

Dodged histogram by group in seaborn

Stack

Another option is stacking the histograms setting the argument to "stack". However, this tends to be more difficult to interpret.

import seaborn as sns

# Histogram
sns.histplot(x = x, hue = group,
             multiple = "stack")

Stacked histogram by group in seaborn

Fill

The last option is setting the argument to "fill", which will create the following visualization, representing the percentage for each histogram over the bins of the X-axis.

import seaborn as sns

# Histogram
sns.histplot(x = x, hue = group,
             multiple = "fill")

Filled histogram by group in seaborn

Color customization

The palette argument of the function can be used to customize the colors of the histograms. You can pass either a color palette or a dictionary containing the desired colors for each group, as shown in the examples below.

Histogram by group color palette in seaborn

Color palette

import seaborn as sns

# Histogram
sns.histplot(x = x, hue = group,
             palette = "Set1")

Histogram by group with custom colors in Python with seaborn

Custom colors

import seaborn as sns

colors = {'G1': 'brown', 'G2': 'green'}

# Histogram
sns.histplot(x = x, hue = group,
             palette = colors)
R CODER website
Data visualization

Data visualization

A practical introduction

Buy on Amazon
Fundamentals of Data Visualization

Fundamentals of Data Visualization

A Primer on Making Informative and Compelling Figures

Buy on Amazon

See also

Strip plot in seaborn

Strip plot in seaborn

Histogram with density in seaborn

Histogram with density in seaborn

Kernel density plot in seaborn with kdeplot

Kernel density plot in seaborn with kdeplot

Boxenplot (letter-value plot) in seaborn

Boxenplot (letter-value plot) in seaborn

Box plot in seaborn

Box plot in seaborn

Histogram in pandas

Histogram in pandas

Ridgeline plots (joy plots) in matplotlib with joypy

Ridgeline plots (joy plots) in matplotlib with joypy

Histogram in seaborn with histplot

Histogram in seaborn with histplot

Swarm plot in seaborn

Swarm plot in seaborn

Violin plots in plotly

Violin plots in plotly

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.