0

I want to create 12 density plots, each one with multiple years to compare the distribution of a variable per years. The value 12 corresponds to months. For example, plot #1 would be for January and it would contain density functions for years 2017 and 2018.

This is my code snippet, but I cannot make it working. In particular, I don't know how to iterate over months in such a way that each density plot contains density functions both for 2017 and 2018.

import matplotlib.pyplot as plt
import seaborn as sns

years = [2017,2018]
months = [1,2,3,4,5,6,7,8,9,10,11,12]

axs = plt.figure(figsize=(10, 8), constrained_layout=True).subplots(4, 3)

for ax, month in zip(axs, months):
    ax.set_title("Month={}".format(month))
    subset = df[(df["Month"] == month)]
    sns.distplot(subset["Variable"], hist = False, kde = True, kde_kws = {"linewidth": 3}, label = year)

This is a sample of df (for simplicity I show only 2 months with 5 rows per year):

Year    Month    Variable
2017    1        30
2017    1        28
2017    1        28
2017    1        28
2017    1        29
2018    1        15
2018    1        16
2018    1        14
2018    1        16
2018    1        14
2017    2        32
2017    2        29
2017    2        29
2017    2        30
2017    2        29
2018    2        10
2018    2        11
2018    2        11
2018    2        11
2018    2        12

UPDATE:

I tried to use kdeplot of seaborn:

fig, axes = plt.subplots(figsize=(20,10), ncols=3, nrows=4)
sns.kdeplot(data=df, x="Year", y="Variable", col="Month", ax=axes[3,2])

However, the result looks wrong:

enter image description here

I also tried using displot with hist and kde kinds, but each time I get circles instead of density functions:

enter image description here

Fluxy
  • 2,838
  • 6
  • 34
  • 63
  • `sns.distplot` is deprecated. Update to `seaborn 0.11.2` and then use `sns.displot(data=df, x='Year', y='Variable', col='Month', col_wrap=3, kind='kde', rug=True)` – Trenton McKinney Aug 30 '21 at 16:48
  • @TrentonMcKinney: Thank you, this seems to work. However, the plots do not look as density plots. Also, the Year labels do not appear. Do you know how to fix these 2 things? – Fluxy Aug 30 '21 at 17:00
  • [`sns.displot`](https://seaborn.pydata.org/generated/seaborn.displot.html) has a number of options for how it displays the information. I'd take a look at the docs to see which options you want. – Trenton McKinney Aug 30 '21 at 17:10
  • Does [python: distplot with multiple distributions](https://stackoverflow.com/q/46045750/7758804) answer your question? I'll close as a duplicate. – Trenton McKinney Aug 30 '21 at 17:24
  • 1
    @TrentonMcKinney: Yes, it helped solving the problem. Thank you. – Fluxy Aug 30 '21 at 17:28
  • As a note, `sns.kdeplot` is an axes level plot so you would do something like `axes = axes.ravel()` and then iterate and plot onto each axes. You would also need to select the data by the relevant month for each axes[i]. See [How to plot in multiple subplots](https://stackoverflow.com/questions/31726643/how-to-plot-in-multiple-subplots) – Trenton McKinney Aug 30 '21 at 17:38

0 Answers0