Skip to main content
adding image for clarification
Source Link

I know two approaches and use them depending on what I need and what the result looks like.

import seaborn as sns
df = sns.load_dataset("penguins")

I defined variables so you can easily compare two methods.

first_dimension = "sex"
horizontal_label = "x label"
second_dimension = "species"

Approach 1:

sns.histplot(binwidth=1,
            x=first_dimension,
            hue=second_dimension,
            data=df,
            stat="count",
            multiple="dodge")

Result approach 1

Approach 2:

sns.barplot(x=horizontal_label,
            y=first_dimension,
            hue=second_dimension,
            data=df.groupby([first_dimension, second_dimension]).size().to_frame(horizontal_label).reset_index())

Result approach 2

Histplot seaborn reference

I know two approaches and use them depending on what I need and what the result looks like.

import seaborn as sns
df = sns.load_dataset("penguins")

I defined variables so you can easily compare two methods.

first_dimension = "sex"
horizontal_label = "x label"
second_dimension = "species"

Approach 1:

sns.histplot(binwidth=1,
            x=first_dimension,
            hue=second_dimension,
            data=df,
            stat="count",
            multiple="dodge")

Result approach 1

Approach 2:

sns.barplot(x=horizontal_label,
            y=first_dimension,
            hue=second_dimension,
            data=df.groupby([first_dimension, second_dimension]).size().to_frame(horizontal_label).reset_index())

Histplot seaborn reference

I know two approaches and use them depending on what I need and what the result looks like.

import seaborn as sns
df = sns.load_dataset("penguins")

I defined variables so you can easily compare two methods.

first_dimension = "sex"
horizontal_label = "x label"
second_dimension = "species"

Approach 1:

sns.histplot(binwidth=1,
            x=first_dimension,
            hue=second_dimension,
            data=df,
            stat="count",
            multiple="dodge")

Result approach 1

Approach 2:

sns.barplot(x=horizontal_label,
            y=first_dimension,
            hue=second_dimension,
            data=df.groupby([first_dimension, second_dimension]).size().to_frame(horizontal_label).reset_index())

Result approach 2

Histplot seaborn reference

improved formatting
Source Link

I know two approaches and use them depending on what I need and what the result looks like.

import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset("penguins")

I defined variables so you can easily compare two methods.

first_dimension = "sex"
horizontal_label = "x label"
second_dimension = "species"

Approach 1:

sns.set(rc = {'figure.figsize':(10, 10)})
sns.histplot(binwidth=1,
            x=first_dimension,
            hue=second_dimension,
            data=df,
            stat="count",
            multiple="dodge")

Result approach 1

Approach 2:

sns.barplot(x=horizontal_label,
            y=first_dimension,
            hue=second_dimension,
            data=df.groupby([first_dimension, second_dimension]).size().to_frame(horizontal_label).reset_index())

Histplot seaborn reference

I know two approaches and use them depending on what I need and what the result looks like.

import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset("penguins")

I defined variables so you can easily compare two methods.

first_dimension = "sex"
horizontal_label = "x label"
second_dimension = "species"

Approach 1:

sns.set(rc = {'figure.figsize':(10, 10)})
sns.histplot(binwidth=1,
            x=first_dimension,
            hue=second_dimension,
            data=df,
            stat="count",
            multiple="dodge")

Result approach 1

Approach 2:

sns.barplot(x=horizontal_label,
            y=first_dimension,
            hue=second_dimension,
            data=df.groupby([first_dimension, second_dimension]).size().to_frame(horizontal_label).reset_index())

Histplot seaborn reference

I know two approaches and use them depending on what I need and what the result looks like.

import seaborn as sns
df = sns.load_dataset("penguins")

I defined variables so you can easily compare two methods.

first_dimension = "sex"
horizontal_label = "x label"
second_dimension = "species"

Approach 1:

sns.histplot(binwidth=1,
            x=first_dimension,
            hue=second_dimension,
            data=df,
            stat="count",
            multiple="dodge")

Result approach 1

Approach 2:

sns.barplot(x=horizontal_label,
            y=first_dimension,
            hue=second_dimension,
            data=df.groupby([first_dimension, second_dimension]).size().to_frame(horizontal_label).reset_index())

Histplot seaborn reference

Source Link

I know two approaches and use them depending on what I need and what the result looks like.

import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset("penguins")

I defined variables so you can easily compare two methods.

first_dimension = "sex"
horizontal_label = "x label"
second_dimension = "species"

Approach 1:

sns.set(rc = {'figure.figsize':(10, 10)})
sns.histplot(binwidth=1,
            x=first_dimension,
            hue=second_dimension,
            data=df,
            stat="count",
            multiple="dodge")

Result approach 1

Approach 2:

sns.barplot(x=horizontal_label,
            y=first_dimension,
            hue=second_dimension,
            data=df.groupby([first_dimension, second_dimension]).size().to_frame(horizontal_label).reset_index())

Histplot seaborn reference