0

I were searching how create scatterplot between each column with each column. Similar question to this one and I followed the code from answer:

How to make a loop for multiple scatterplots in python?

What I done is:

columns = ['a','b','c','d','e','f']

for pos, axis1 in enumerate(columns):   
    for axis2 in enumerate(columns[pos+1:]): 
        plt.scatter(df.loc[:, axis1], df.loc[:, axis2].iloc[:,1])

But in this solution I'm getting everything on one single plot, I want to make it separately, how I can achieve that?

3
  • 1
    Consider Seaborn's pairplot. Commented Feb 18, 2021 at 17:20
  • I want to have every graph separately, not all at the one so sns.pairplot for me is not solution but thank you Commented Feb 18, 2021 at 17:21
  • 5
    put plt.show() after plt.scatter (inside the two loops) Commented Feb 18, 2021 at 17:22

2 Answers 2

2

Yehla has a good solution, but if you want to graph each plot on the same figure, create a new plt.subplot() each loop.

import math
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'))

col_count = 2
columns = list(df.columns)
row_count = math.ceil((len(columns)*len(columns))/col_count)

plt_dict = {}
count = 1
for k,v in enumerate(columns):
    for column2 in columns[k:]:     
        ax = plt.subplot(row_count,col_count,count)
        ax.set_title(f'{v} x {column2}')
        ax.scatter(df[v],df[column2])
        
        count += 1
plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

2

As I understood it you want to do this:

import pandas as pd
df = pd.DataFrame([[5.1, 3.5, 0], [4.9, 3.0, 1], [7.0, 3.2, 2],
                   [6.4, 3.2, 3], [5.9, 3.0, 4]],
                  columns=['col1', 'col2', 'col3'])
cols = ['col1', 'col2', 'col3']
for i, col1 in enumerate(['col1', 'col2','col3']):
    for col2 in cols[i:]:
        df.plot.scatter(x=col1, y=col2, c='DarkBlue')

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.