首页 > 解决方案 > 如何使用此方法制作子图

问题描述

我正在尝试为数据集中的每个特征制作直方图的子图。

以下代码是我已经尝试解决的问题。考虑训练数据集,它有 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

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

标签: python

解决方案


这是我的建议:

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))

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


推荐阅读