【问题标题】:Matplotlib - TypeError: isinstance() arg 2 must be a type or tuple of typesMatplotlib - TypeError: isinstance() arg 2 必须是类型或类型的元组
【发布时间】:2021-06-13 22:38:50
【问题描述】:

我正在尝试使用 matplotlib 库制作一些图。但是,每当我尝试运行 plt.plot() 时,都会出现错误:

if (isinstance(marker, np.ndarray) and marker.ndim == 2 and
TypeError: isinstance() arg 2 must be a type or tuple of types

在库的markers.py脚本中

尝试执行测试代码时会发生此特定错误实例

plt.plot([1,2,3],[1,2,3])

进入 matplotlib 源代码并将 isinstance(VarName, np.ndarray) 的实例更改为 isinstance(VarName, type(np.ndarray)) 删除了该特定行上的错误,尽管我必须更改它我相信每次提到 isinstance 都会让它消失。这显然是一个次优的解决方案。

请注意,创建一个 numpy 数组并打印 isinstance(array, np.ndarray) 的结果也会给我错误,而打印 isinstance(array, type(np.ndarray)) 的结果正确返回 true,所以我不认为这是 matplotlibs 使用 np.ndarray 的错误,也不是正确导入 numpy,而是我的整个环境的持久性问题。

我正在使用 conda 在带有 python 3.8.10 的 Windows 机器上运行虚拟环境(该错误也发生在 python 3.7 上),据我所知,numpy 和 matplotlib 都是最新的并且可以正确导入.

我确定这是一个相对基本的错误,虽然我无法终生弄清楚,但我能找到的其他人似乎之前没有遇到过这个问题。至少不使用 matplotlib 或 ndarrays。

【问题讨论】:

  • 这是不可重现的。请使用conda update --all 更新所有适当的环境
  • 确保您没有在脚本前面重新定义np.ndarray

标签: python windows numpy matplotlib isinstance


【解决方案1】:

抱歉,我确实在代码前面的单独脚本中重新定义了 np.ndarray。我没有考虑过这一点,因为我没有意识到新定义可以在多个脚本中保持不变。

定义变量时出现简单错误或使用 '=' 而不是 ':'

【讨论】:

    猜你喜欢
    • 2019-04-26
    • 2013-01-18
    • 2019-11-28
    • 2021-10-12
    • 1970-01-01
    • 1970-01-01
    • 2021-11-14
    • 1970-01-01
    • 2013-06-03
    相关资源
    最近更新 更多