3

I would like to plot multiple independent variables against a single dependent variable and show them in one figure. The following code works, but I have more than the 4 variables indicated. So do all of the variables by hand would be cumbersome. Any ideas? Thanks!

fig, axs = plt.subplots(2, 2, figsize=(10, 6))

axs[0, 0].scatter(df.ind_var1, df.dep_var)
axs[0, 1].scatter(df.ind_var2, df.dep_var)
axs[1, 0].scatter(df.ind_var3, df.dep_var)
axs[1, 1].scatter(df.ind_var4, df.dep_var)

axs[0, 0].set_title('ind_var1')
axs[0, 1].set_title('ind_var2')
axs[1, 0].set_title('ind_var3')
axs[1, 1].set_title('ind_var4')

for ax in axs.flat:
    ax.set_xticklabels([])
    ax.set_ylabel('dep_var')
    ax.set_yticklabels([])
    
fig.suptitle('Plot', fontsize=20)
fig.tight_layout()
    
plt.show()

3 Answers 3

4

It looks like you can just do:

# you need to provide this
ind_vars = ['ind_var1', 'ind_var2', 'ind_var3', 'ind_var4']

fig, axes = plt.subplots(2,2, figsize=(10,6))

# this combines the two loops
for col, ax in zip(ind_vars, axes.ravel()):
    # plot the data
    ax.scatter(df[col], df['dep_var'])

    # format the axis
    ax.set_title(col)
    ax.set_xticklabels([])
    ax.set_ylabel('dep_var')
    ax.set_yticklabels([])

fig.suptitle('Plot', fontsize=20)
fig.tight_layout()
    
plt.show()
Sign up to request clarification or add additional context in comments.

Comments

0

You should check out python's seaborn package for statistical graphing.

if you want fine control over each graph, manually inputting will get you there, but a loop over df.columns with accession using the df['column'] format will get you to where you want to go.

Comments

0

If I understand correctly, I don't see why you could not use a for loops iterating over the axs, as in:

vars = [indipendent variables]
n = 0 
for row in range(i,j):
    for col in range(n, m):
        ax[row, col] = scatter(vars[n], df.dep_var)
        n += 1

Or something to that effect.

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.