小编Ran*_*dy 的帖子

子图的子图 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()
Run Code Online (Sandbox Code Playgroud)

结果只是采用这个循环并将它们放入一组行和列中,在本例中为 5x2

python loops matplotlib seaborn

3
推荐指数
1
解决办法
2713
查看次数

标签 统计

loops ×1

matplotlib ×1

python ×1

seaborn ×1