您可以将所有 dfs 组合成一个更大的数据框(按行连接),然后使用内置的箱线图方法创建所有列的箱线图,因为默认行为是为每一列创建箱线图。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
np.random.seed(42)
df1 = pd.DataFrame(np.random.randint(0,100,size=(100, 3)), columns=['col1','col2','col3'])
df2 = pd.DataFrame(np.random.randint(0,100,size=(100, 3)), columns=['col1','col2','col3'])
df3 = pd.DataFrame(np.random.randint(0,100,size=(100, 3)), columns=['col1','col2','col3'])
df4 = pd.DataFrame(np.random.randint(0,100,size=(100, 3)), columns=['col1','col2','col3'])
dfs = [df1, df2, df3, df4]
## this will create a big dataframe composed of all the dfs
all_data = pd.concat(dfs, axis=1)
## this creates one boxplot for the first dataframe, loop through this
boxplot = all_data.iloc[:,0:3].boxplot()
plt.show()