close

How To Plot Multiple Histograms On Same Plot With Seaborn

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How To Plot Multiple Histograms On Same Plot With Seaborn in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How To Plot Multiple Histograms On Same Plot With Seaborn?

  1. How To Plot Multiple Histograms On Same Plot With Seaborn?

    If I understand you correctly you may want to try something this:
    fig, ax = plt.subplots() for a in [x, y]:

  2. Plot Multiple Histograms On Same Plot With Seaborn

    If I understand you correctly you may want to try something this:
    fig, ax = plt.subplots() for a in [x, y]:

Method 1

If I understand you correctly you may want to try something this:

fig, ax = plt.subplots()
for a in [x, y]:
    sns.distplot(a, bins=range(1, 110, 10), ax=ax, kde=False)
ax.set_xlim([0, 100])

Which should yield a plot like this:

enter image description here

UPDATE:

Looks like you want ‘seaborn look’ rather than seaborn plotting functionality. For this you only need to:

import seaborn as sns
plt.hist([x, y], color=['r','b'], alpha=0.5)

Which will produce:

enter image description here

Method 2

Merge x and y to DataFrame, then use histplot with multiple=’dodge’ and hue option:

import random

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

x = [random.randrange(100) for _ in range(100)]
y = [random.randrange(100) for _ in range(100)]
df = pd.concat(axis=0, ignore_index=True, objs=[
    pd.DataFrame.from_dict({'value': x, 'name': 'x'}),
    pd.DataFrame.from_dict({'value': y, 'name': 'y'})
])
fig, ax = plt.subplots()
sns.histplot(
    data=df, x='value', hue='name', multiple='dodge',
    bins=range(1, 110, 10), ax=ax
)
ax.set_xlim([0, 100])
Resulting Plot

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read