1

Let's say I have two dataframes:

a = pd.DataFrame({"A": [2,3,4,5], 
                  "B": [4,8,9,10], 
                  "C" :[63,21,23,4],
                  "D": [56,32,12,2]
                 })

b = pd.DataFrame({"A": [12,13,14,15], 
                  "B": [41,81,91,10], 
                  "C": [3,2,5,6], 
                  "D": [4,2,3,4]
                 })

I want to be able to plot scatterplots between the variables on each dataframe with the same name. I've come up with the following code:

fig, ax = plt.subplots((round(len(b)/2)),2,figsize=(10,10))
for i, col in enumerate(b.columns):

    sns.regplot(x=a[col], y=b[col], ax=ax[i,0])
    plt.title(col)
    sns.regplot(x=a[col], y=b[col], ax = ax[i,1])
    plt.title(col)


plt.tight_layout()

However, this yields the following figure:

enter image description here

What I want is:

enter image description here

I know this will be simple, but I just can't figure out a way of looping for the second ax[i,1]. Many thanks!

0

1 Answer 1

1
fig, ax = plt.subplots((round(len(b)/2)), 2,figsize=(10,10))
v = ax.ravel()
for i, col in enumerate(b.columns):

    sns.regplot(x=a[col], y=b[col], ax=v[i])
    v[i].set_xlabel(f'{col}_a')
    v[i].set_ylabel(f'{col}_b')
    v[i].set_title(col)

plt.tight_layout()

enter image description here

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.