【发布时间】:2018-09-13 15:09:02
【问题描述】:
如何获得 np.nditer 的正确返回类型?我需要在这里遍历ax 对象:
fig, ax = plt.subplots(figsize=(16,9), ncols=3, nrows=2)
for col, elem in zip(df.columns[:-1], np.nditer(ax, flags = ['refs_ok'])):
sns.countplot(x="CLASS", hue=col, data=df, ax=elem)
我知道我可以在这里使用 ax 数组的维度进行迭代,但我想知道我是否可以完成这项工作。基本上,ax=elem 在迭代中应该看起来像ax=ax[i][j]。但事实证明它们有不同的类型:
print(type(elem))
print(type(ax[0][0]))
返回:
<class 'numpy.ndarray'>
<class 'matplotlib.axes._subplots.AxesSubplot'>
【问题讨论】:
-
nditer是一个专门的迭代器,大多数numpy工作不需要。如您所见,它生成数组(单个元素 0d)。elem.item()可能是ax[0,0]。但是接受的答案使用的ax.flat是一个更好的迭代器。
标签: python numpy matplotlib data-science