jwburritt
jwburritt

Reputation: 91

How to plot multiple variables programatically

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()

Upvotes: 3

Views: 624

Answers (3)

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.

Upvotes: 0

Clinton Bradfield
Clinton Bradfield

Reputation: 226

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.

Upvotes: 0

Quang Hoang
Quang Hoang

Reputation: 150735

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()

Upvotes: 4

Related Questions