'TypeError: argument of type 'function' is not iterable' 如何使函数可迭代?

问题描述 投票:0回答:1

我试图在python中使用seaborn生成6个小提琴子图。我遇到一个错误,说 "函数类型的参数不可迭代"。我想知道是我的代码中少了什么,还是seaborn的导入中少了什么。当我在控制台中输入 "print(dir(sns.violinplot)) "时。迭代 是不存在的,我想知道这是否是导致错误的原因?先谢谢你 以下是我得到的代码和错误信息。

hue = "Mutation"
col = "Subregion"
kind = "violin"
data = M1
title_name = "M1"



VAR = M1.columns[7:]
YL = ["Area (mm^2)","DAPI Cell Count","SST Cell Count","DAPI Cell Density (DAPI/mm^2)","SST Cell Density (SST/mm^2)","SST Cell Density (% SST/DAPI cells)"]

fig, axs = plt.subplots(3, 2,figsize = (8,8))
fig.subplots_adjust(hspace=0.4, wspace=0.4)
axs = axs.reshape(-1)
for k in range(len(VAR)):
    sns.violinplot(x = x in sns.violinplot, y = VAR[k], hue = hue, col = None, kind = kind, data = M1,ax=axs[k])
    axs[k].set_ylabel(YL[k],fontsize=8)
    axs[k].legend_.remove()
axs[-1].legend(loc='upper right', ncol=1,bbox_to_anchor=(1.5,1.5))
plt.show()```

```File "<ipython-input-70-0506b9c647bf>", line 41, in <module>
    sns.violinplot(x = x in sns.violinplot, y = VAR[k], hue = hue, col = None, kind = kind, data = M1,ax=axs[k])

TypeError: argument of type 'function' is not iterable```
python function seaborn typeerror iterable
1个回答
1
投票

你不能对一个函数进行迭代;你可能会用它来产生一个可迭代对象,但函数本身并不是一个可迭代对象。 (从技术上讲,在 Python 中构建一个既可迭代又可调用的对象是可能的,但是...不。 这不是解决这个问题的方法。)

我很确定这句话是没有意义的,说明你可能由于不理解函数的工作原理而把整个事情搞得过于复杂了。

sns.violinplot(x = x in sns.violinplot ...

从文档中看https:/seaborn.pydata.orggeneratedseaborn.violinplot.html。)它看起来像也许而不是整个循环,你只是想。

axs = sns.violinplot(y=VAR)

或类似的东西?

© www.soinside.com 2019 - 2024. All rights reserved.