0

What i would like to do is creating displots in a way that each pair of two occupies a row. So, i used these lines of codes:

columns = list(cols) # cols created from dataframe.columns
sns.set(rc = {"figure.figsize": (12, 15)})
sns.set_style(style = "white")

for i in range(len(columns)):
    plt.subplot(10, 2, i + 1)
    sns.displot(data[columns[i]], rug = True)

However, the result is a runtime error and strange shaped plots. The result

Does anyone know what i am doing wrong? Thanks

2
  • How many plots are you trying to create? Commented Aug 3, 2021 at 11:18
  • @Luke I am trying to create 20 plots. Commented Aug 3, 2021 at 12:23

1 Answer 1

1

sns.displot is a figure-level function and always creates its own new figure. To get what you want, you could create a long-form dataframe.

Here is some example code showing the general idea:

from matplotlib import pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np

data = pd.DataFrame(data=np.random.rand(30, 20), columns=[*'abcdefghijklmnopqrst'])
cols = data.columns
data_long = data.melt(value_vars=cols)
g = sns.displot(data_long, x='value', col='variable', col_wrap=2, height=2)
g.fig.subplots_adjust(top=0.97, bottom=0.07, left=0.07, hspace=0.5)
plt.show()

sns.displot using long data

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.