Reputation: 25366
I have a pandas dataframe pandas_df with 6 input columns: column_1, column_2, ... , column_6
, and one result column result
. Now I used the following code to plot the scatter plot for every two input column pairs (so totally I have 6*5/2 = 15 figures). I did the following code 15 times, and each generated a big figure.
I am wondering is there a way to iterate over all possible column pairs, and plot all 15 figures as small figures in one big plot? Thanks!
%matplotlib notebook
import matplotlib.pyplot as plt
import matplotlib
matplotlib.style.use('ggplot')
pandas_df.plot(x='column_1', y='column_2', kind = 'scatter', c = 'result')
Upvotes: 1
Views: 1618
Reputation: 294488
consider the dataframe df
df = pd.DataFrame(np.random.rand(10, 6), columns=pd.Series(list('123456')).radd('C'))
df
Solution
Use itertools
and matplotlib.pyplot.subplots
from itertools import combinations
import matplotlib.pyplot as plt
pairs = list(combinations(df.columns, 2))
fig, axes = plt.subplots(len(pairs) // 3, 3, figsize=(15, 12))
for i, pair in enumerate(pairs):
d = df[list(pair)]
ax = axes[i // 3, i % 3]
d.plot.scatter(*pair, ax=ax)
fig.tight_layout()
Upvotes: 5