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:
JavaScript
x
6
1
columns = ['a','b','c','d','e','f']
2
3
for pos, axis1 in enumerate(columns):
4
for axis2 in enumerate(columns[pos+1:]):
5
plt.scatter(df.loc[:, axis1], df.loc[:, axis2].iloc[:,1])
6
But in this solution I’m getting everything on one single plot, I want to make it separately, how I can achieve that?
Advertisement
Answer
Yehla has a good solution, but if you want to graph each plot on the same figure, create a new plt.subplot()
each loop.
JavaScript
1
22
22
1
import math
2
import numpy as np
3
import pandas as pd
4
import matplotlib.pyplot as plt
5
6
df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'))
7
8
col_count = 2
9
columns = list(df.columns)
10
row_count = math.ceil((len(columns)*len(columns))/col_count)
11
12
plt_dict = {}
13
count = 1
14
for k,v in enumerate(columns):
15
for column2 in columns[k:]:
16
ax = plt.subplot(row_count,col_count,count)
17
ax.set_title(f'{v} x {column2}')
18
ax.scatter(df[v],df[column2])
19
20
count += 1
21
plt.show()
22