0

I am trying to make a density plot that shows several curves. I'm using displot from the seaborn module as shown below, instead of distplot, because I got a warning (in Pycharm) that the latter will be removed in the future. I can't find how to change the labels in this plot, though. Right now they're just listed as "0" and "1". Also, I would like to normalize both curves so that they can be better compared. common_norm=True does not seem to work. Does anyone know what arguments I could use? There is one called "legend", but it is a boolean. I looked at one other post about distplot legends, but it doesn't work for me.

A short working example:

import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt

mylist = [np.random.normal(loc=0.0, scale=1.0, size=None) for i in range(50)]
yourlist = [np.random.normal(loc=1.0, scale=3, size=None) for j in range(50)]
sns.displot([mylist, yourlist], kind="kde", common_norm=True, color=["blue", "red"], linewidth=1)
plt.show()

density plots

theWrongAlice
  • 99
  • 2
  • 10

1 Answers1

3

Here is a revised version that should give the plot you're expecting

sns.displot(
    {"a": mylist, "b": yourlist},  # Use a dict to assign labels to each curve
    kind="kde",
    common_norm=False,  # Normalize each distribution independently
    palette=["blue", "red"],  # Use palette for multiple colors
    linewidth=1
)

I think the only change that requires a bit more explanation is common_norm. Densities are normalized by the area under the curve (it should equal 1). If common_norm=True, the sum of the areas under all of curves equals 1; if common_norm_False, the area under each curve equals 1. I think you're expecting the normalization to be done by the maximum value, but that's not really interpretable. The common_norm parameter does not have an obvious effect here because you have the same number of observations in each group, so it will change the y axis labels but not the relative scaling of the curves.

mwaskom
  • 46,693
  • 16
  • 125
  • 127