python - 引用类型的 np.nditer 返回类型
问题描述
如何获得 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'>
解决方案
可能你想像这样迭代
fig, ax = plt.subplots(figsize=(16,9), ncols=3, nrows=2)
for col, elem in zip(df.columns[:-1], ax.flat):
sns.countplot(x="CLASS", hue=col, data=df, ax=elem)
它更短并且总是制作elem
一个matplotlib.axes._subplots.AxesSubplot
对象。
推荐阅读
- php - Symfony @ParamConverter 仅适用于活动行
- python - 在 python 循环中测量矩阵距离
- powershell - 您不能使用 excel 在 powershell 中调用空值表达式的方法
- jquery - jQuery 在表单值更改时显示警报
- bash - 为什么我的 docker 容器不尊重我的权限?
- php - 如何在 Phinx 迁移中为 MySQL 浮点列设置比例和精度
- r - R中编码(v)中的条件格式错误
- gradle - 如何通过 API 或命令行列出具有更多详细信息的 gradleTasks,例如 mainClass?
- arrays - 如何知道模型期望二维数组等格式的输入必须是一维数组?
- r - 按条件复制行并更改类型