Danny B
Danny B

Reputation: 355

How to plot multiple scatterplots at once elegantly

Hello fellow coding warriors :),

I'm doing data analysis of a standard CSV with 8 columns on kaggle. I want to plot a scatter-plot of all the columns vs the final columns. However when I run the code all the scatterplots are smushed together. Could you help me make all the scatter plots visible?

I'm running Python on Kaggle, already tried some code and it did not work. I've done a range of figsize values, and they are all too stretched out an not tall enough

print(my_data.columns)
plt.figure(figsize=(10,15))
for i in range(len(my_data.columns)):
    plt.subplot(5*len(my_data.columns),1,5*i+1)
    sns.scatterplot(x=my_data[my_data.columns[i]],y=my_data[my_data.columns[-1]])

The output looks like this: failed attempt

Help please, thank you!

Upvotes: 1

Views: 2133

Answers (1)

Brendan
Brendan

Reputation: 4011

Perhaps not ideal, but one option is to simply replace the ylabel.

df = pd.read_csv(r'https://vincentarelbundock.github.io/Rdatasets/csv/datasets/mtcars.csv').rename({'Unnamed: 0':'car'}, axis=1)

df = df[['mpg','hp','disp','wt']]
df.rename({'mpg':'the quick brown fox jumped over ...'}, axis=1, inplace=True)
    # Yes, some lack of creativity here...

cols_to_plot = ['hp','disp','wt']
fig, ax = plt.subplots(nrows=len(cols_to_plot), ncols=1, figsize=(8,6))
for i, col in enumerate(cols_to_plot):
    plt.subplot(ax[i])
    sns.scatterplot(x=df[col], y=df['the quick brown fox jumped over ...'])
    plt.ylabel("the quick\nbrown fox\njumped over ...")
    plt.tight_layout()

enter image description here

Upvotes: 1

Related Questions