How can I make seaborn distribution subplots in a loop?

混江龙づ霸主 提交于 2021-02-19 01:16:49

问题


I have a 5D array called data

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

But I want to make them inside subplots instead. How can I do it?

Tried this:

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

This obviously doesn't work.


回答1:


You would want to use the ax argument of the seaborn distplot function to supply an existing axes to it. Looping can be simplified by looping over the flattened array of axes.

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



回答2:


Specify which subplot each distplot should fall on:

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


来源:https://stackoverflow.com/questions/53310228/how-can-i-make-seaborn-distribution-subplots-in-a-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!