【发布时间】: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