1

I'm trying to print all the boxplots for each variable in a dataset with this python loop.

colNameList = list(df.columns)
for i in range (0, len(df.columns)):
    df.boxplot(column=colNameList[i])

Where df is my dataset.

Why this simple code shows to me only the last one boxplot?

Joe
  • 12,057
  • 5
  • 39
  • 55
Davide
  • 185
  • 3
  • 22

2 Answers2

6

IIUC, you want a box for each column, which is the default for df.boxplot().

Example dataframe:

df = pd.DataFrame({'col1':np.random.randint(0,9,100),
                   'col2':np.random.randint(2,12,100),
                   'col3':np.random.randint(4,14,100)})

>>> df.head()
   col1  col2  col3
0     6     9     4
1     5     2     8
2     0     7    11
3     0    10     9
4     0     3     8

Plotting:

df.boxplot()

enter image description here

If you want just certain columns:

df[['col1', 'col2']].boxplot()
# or
df.boxplot(column=['col1', 'col2'])

enter image description here

Edit Based on your comments, here is a way to save each individual box as a separate boxplot, so you can see them individually.

for i in df.columns:
    df.boxplot(column=i)
    plt.savefig('plot'+str(i)+'.png')
    plt.close()
sacuL
  • 49,704
  • 8
  • 81
  • 106
4

If you want a separate plot for each variable, just put the plt.show() inside the for loop:

import matplotlib.pyplot as plt
import pandas as pd

for i in df.columns:
    df.boxplot(column=i)
    plt.show()

You can write your code in a more pythonic way: df.colums is already a list and the iteration is done on the list

Joe
  • 12,057
  • 5
  • 39
  • 55