【问题标题】:'TypeError: argument of type 'function' is not iterable' How do I make a function iterable?'TypeError: 'function' 类型的参数不可迭代' 如何使函数可迭代?
【发布时间】:2020-09-07 00:04:54
【问题描述】:

我正在尝试在 python 中使用 seaborn 生成 6 个小提琴子图。我遇到了一个错误,指出“类型函数的参数不可迭代”。我想知道我的代码中是否缺少某些内容,或者我的 seaborn 导入中是否缺少某些内容。当我在控制台中输入 'print(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```

【问题讨论】:

  • 你想用这个语句做什么:x = x in sns.violinplot?
  • x = x in sns.violinplot 是导致错误的位,因为它尝试通过 sns.violinplot 搜索 x 这是一个函数。不太清楚你想做什么,所以我不知道你应该把它改成什么

标签: python function seaborn typeerror iterable


【解决方案1】:

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

我很确定这句话毫无意义,并表明您可能由于不了解函数的工作原理而使整个事情过于复杂:

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

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

axs = sns.violinplot(y=VAR)

或类似的东西?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-15
    • 2011-10-04
    • 2019-08-24
    • 2020-05-07
    • 2015-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多