Python 如何使用此方法生成子图

Python 如何使用此方法生成子图,python,Python,我试图为数据集中的每个特征制作一个子图 下面的代码是我已经尝试解决的问题。考虑列车数据集,它有9列,并且我要绘制在3×3子图中。 import matplotlib.pyplot as plt fig, ax = plt.subplots(nrows=3, ncols=3) i=0 for row in ax: for col in row: train.iloc[:,i].hist() i=i+1 我得到了最后一个子批次中的所有直方图。这里是我的

我试图为数据集中的每个特征制作一个子图

下面的代码是我已经尝试解决的问题。考虑列车数据集,它有9列,并且我要绘制在3×3子图中。

import matplotlib.pyplot as plt

fig, ax = plt.subplots(nrows=3, ncols=3)

i=0
for row in ax:
    for col in row:
        train.iloc[:,i].hist()
        i=i+1

我得到了最后一个子批次中的所有直方图。

这里是我的建议:

import matplotlib.pyplot as plt
import random

for i in range(1,7):
  # Cut your figure into 3 row and 3 columns
  # and create the plot in the i subplot.
  # here I used the f-string formatting that is available from python3.6
  plt.subplot(f'33{i}')
  plt.hist(random.randrange(0, 10))

你可以在这个令人惊叹的网站上找到更多的想法:

可以使用一个
ax
参数,它是Matplotlib轴使用的参数。

谢谢你的回答,我找到了这个方法,我可以用它得到我想要的:train.plot(kind='box',subplot=True,layout=(3,3),sharex=False,sharey=False)