子图的子图 Matplotlib / Seaborn

兰迪

我正在尝试创建一个子图网格。每个子图看起来都像这个站点上的那个。

https://python-graph-gallery.com/24-histogram-with-a-boxplot-on-top-seaborn/

例如,如果我有 10 组不同的这种风格的情节,我想将它们制作成 5x2。

我已经通读了 Matplotlib 的文档,但似乎无法弄清楚如何去做。我可以循环子图并获得每个输出,但我无法将其放入行和列

导入pandas 作为pd 导入numpy 作为np 导入seaborn 作为sns

df = pd.DataFrame(np.random.randint(0,100,size=(100, 10)),columns=list('ABCDEFGHIJ'))

for c in df :
    # Cut the window in 2 parts
    f, (ax_box,
        ax_hist) = plt.subplots(2,
                                sharex=True,
                                gridspec_kw={"height_ratios":(.15, .85)},
                                figsize = (10, 10))
    # Add a graph in each part
    sns.boxplot(df[c], ax=ax_box)
    ax_hist.hist(df[c])
    # Remove x axis name for the boxplot
plt.show()

在这种情况下,结果只会采用这个循环并将它们放入一组行和列中 5x2

谢尔多

您有 10 列,每列创建 2 个子图:一个箱线图和一个直方图。所以你总共需要20个数字。您可以通过创建 2 行 10 列的网格来完成此操作


完整答案:(根据口味调整figsizeheight_ratios

import pandas as pd
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt

f, axes = plt.subplots(2, 10, sharex=True, gridspec_kw={"height_ratios":(.35, .35)}, 
                                    figsize = (12, 5))

df = pd.DataFrame(np.random.randint(0,100,size=(100, 10)),columns=list('ABCDEFGHIJ'))

for i, c in enumerate(df):
    sns.boxplot(df[c], ax=axes[0,i])
    axes[1,i].hist(df[c])
plt.tight_layout()
plt.show()

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章