Home > Mobile >  How to create subplots of all column combinations from two dataframes
How to create subplots of all column combinations from two dataframes

Time:06-14

I have a made a function which plots input variables against predicted variables.

dummy_data = pd.DataFrame(np.random.uniform(low=65.5,high=140.5,size=(50,4)), columns=list('ABCD'))
dummy_predicted = pd.DataFrame(np.random.uniform(low=15.5,high=17.5,size=(50,4)), columns=list('WXYZ'))

##Plot test input distriubtions
fig = plt.figure(figsize=(15,6))
n_rows = 1 
n_cols = 4
counter = 1
for i in dummy_data.keys():
    plt.subplot(n_rows, n_cols, counter)
    plt.scatter(dummy_data[i], dummy_predicted['Z'])

    plt.title(f'{i} vs Z')
    plt.xlabel(i)
    counter  = 1

plt.tight_layout() 
plt.show()

enter image description here

How do I create a 4 x 4 subplot of all combinations of 'ABCD' and 'WXYZ'? I can have any number of dummy_data and dummy_predicted columns so some dynamism would be useful.

CodePudding user response:

  • Use enter image description here

    CodePudding user response:

    just do a double for loop

    n_rows = len(dummy_data.columns)
    n_cols = len(dummy_predicted.columns)
    
    fig, axes = plt.subplots(n_rows, n_cols, figsize=(15,6))
    
    for row, data_col in enumerate(dummy_data):
        for col, pred_col in enumerate(dummy_predicted):
            ax = axes[row][col]
    
            ax.scatter(dummy_data[data_col], dummy_predicted[pred_col])
            
            ax.set_title(f'{data_col} vs {pred_col}')
    
            ax.set_xlabel(data_col)
    
    plt.tight_layout() 
    plt.show()
    

    Output:

    enter image description here

  • Related