如何在循环中创建Seaborn分布子图?

16

我有一个名为data的5D数组

for i in range(10):
     sns.distplot(data[i,0,0,0], hist=False)

但我希望它们成为子图中的一部分。我该怎么做呢?

尝试了这个:

plt.rc('figure', figsize=(4, 4))  
fig=plt.figure()
fig, ax = plt.subplots(ncols=4, nrows=3)

for i in range(10):
    ax[i].sns.distplot(data[i,0,0,0], hist=False)
plt.show()

显然,这行不通。

2个回答

25
你可以使用seaborn的distplot函数的ax参数来提供现有的轴。通过对展平后的轴数组进行循环,可以简化循环过程。

你可以使用seaborn的distplot函数的ax参数来提供现有的轴。通过对展平后的轴数组进行循环,可以简化循环过程。

fig, axes = plt.subplots(ncols=4, nrows=3)

for i, ax in zip(range(10), axes.flat):
    sns.distplot(data[i,0,0,0], hist=False, ax=ax)
plt.show()

这样,如何进一步修改子图,例如添加标题或轴标签,指定刻度等等。 - Jason Goal

4

指定每个distplot应该放置在哪个子图上:

f = plt.figure()
for i in range(10):
    f.add_subplot(4, 3, i+1)
    sns.distplot(data[i,0,0,0], hist=False)
plt.show()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接