'类型错误:类型 'function' 的参数不可迭代" 如何使函数可迭代?



我正在尝试使用 python 中的 seaborn 生成 6 个小提琴子图。我遇到了一个错误,指出"函数类型的参数不可迭代"。我想知道我的代码中是否缺少某些内容,或者我的 seaborn 导入中是否缺少某些内容。当我在控制台中输入"打印(dir(sns.violinplot(("时,iter不存在,我想知道这是否导致了错误?提前感谢!这是我收到的代码和错误消息。

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 中构造一个既可迭代又可调用的对象是可能的,但是......不。 这不是解决这个问题的方法。

我很确定这个陈述是没有意义的,并表明由于不了解函数的工作原理,您可能使整个事情过于复杂:

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

从查看文档 (https://seaborn.pydata.org/generated/seaborn.violinplot.html( 来看,看起来可能不是您想要的整个循环:

axs = sns.violinplot(y=VAR)

还是接近那个的东西?

最新更新