admin 管理员组

文章数量: 887032

【解决报错原因分析】画图plt.contourf(X,Y,Z)报错TypeError: unhashable type: ‘numpy.ndarray‘(含详细示例讲解)

今天简化画图代码的时候发现了很奇怪的报错现象,经过一系列尝试找到了根源,希望帮助后来人,主要问题出现在如下语句中(为了体现问题、方便比对,特意在这改变了x为xx,如果你不想看这冗长的示例,可以直接按照下文红字的方法修改

import numpy as np
import matplotlib.pyplot as plt
#===========做等值线图===========
y = x = np.arange(10)
xx = [str(i) for i in x]#将x中的元素转化为字符串类型
X,Y = np.meshgrid(xx,y)
Z = np.sin(x)[:, np.newaxis] * np.cos(y)[np.newaxis, :]
plt.contourf(X,Y,Z)
#===========报错信息==============
'''
Traceback (most recent call last):Input In [63] in <cell line: 1>plt.contourf(X,Y,Z)...TypeError: unhashable type: 'numpy.ndarray'
'''
#X、Y、Z的输出形式
'''
X
Out[90]: 
array([['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']], dtype='<U1')Y
Out[91]: 
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],[2, 2, 2, 2, 2, 2, 2, 2, 2, 2],[3, 3, 3, 3, 3, 3, 3, 3, 3, 3],[4, 4, 4, 4, 4, 4, 4, 4, 4, 4],[5, 5, 5, 5, 5, 5, 5, 5, 5, 5],[6, 6, 6, 6, 6, 6, 6, 6, 6, 6],[7, 7, 7, 7, 7, 7, 7, 7, 7, 7],[8, 8, 8, 8, 8, 8, 8, 8, 8, 8],[9, 9, 9, 9, 9, 9, 9, 9, 9, 9]])Z
Out[92]: 
array([[ 0.        ,  0.        , -0.        , -0.        , -0.        ,0.        ,  0.        ,  0.        , -0.        , -0.        ],[ 0.84147098,  0.45464871, -0.35017549, -0.83304996, -0.55002214,0.2386935 ,  0.80795544,  0.63438687, -0.12243406, -0.76668968],[ 0.90929743,  0.4912955 , -0.37840125, -0.90019763, -0.59435646,0.2579333 ,  0.87308037,  0.68552138, -0.13230281, -0.8284884 ],[ 0.14112001,  0.07624747, -0.05872664, -0.13970775, -0.09224219,0.04003041,  0.13549924,  0.10639069, -0.02053297, -0.12857871],[-0.7568025 , -0.40890213,  0.31494096,  0.74922879,  0.49467912,-0.21467625, -0.72665927, -0.57055511,  0.11011479,  0.68954566],[-0.95892427, -0.518109  ,  0.3990533 ,  0.94932784,  0.62679474,-0.27201056, -0.9207306 , -0.72293517,  0.13952351,  0.87370493],[-0.2794155 , -0.15096884,  0.11627788,  0.27661925,  0.18263816,-0.07925961, -0.26828646, -0.21065197,  0.04065496,  0.25458392],[ 0.6569866 ,  0.35497137, -0.27340289, -0.6504118 , -0.4294351 ,0.18636225,  0.63081901,  0.49530368, -0.09559157, -0.59860037],[ 0.98935825,  0.53455254, -0.4117183 , -0.97945724, -0.64668771,0.28064352,  0.94995239,  0.74587941, -0.14395166, -0.90143424],[ 0.41211849,  0.22266857, -0.1715018 , -0.40799421, -0.26937862,0.11690243,  0.39570392,  0.31069706, -0.05996325, -0.37549362]])
'''

经过多次尝试,发现这是由于X和Y中含有'string'(字符)类型的元素导致的,只需更改为'int'或'float'(数字),就能防止报错,并不需要改变X和Y的类型,他们可以依然是'numpy.ndarray'。

更改示例如下,

y = x = np.arange(10)
X,Y = np.meshgrid(x,y)
Z = np.sin(x)[:, np.newaxis] * np.cos(y)[np.newaxis, :]
plt.contourf(X,Y,Z)
plt.show()
'''
X
Out[85]: 
array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])Y
Out[86]: 
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],[2, 2, 2, 2, 2, 2, 2, 2, 2, 2],[3, 3, 3, 3, 3, 3, 3, 3, 3, 3],[4, 4, 4, 4, 4, 4, 4, 4, 4, 4],[5, 5, 5, 5, 5, 5, 5, 5, 5, 5],[6, 6, 6, 6, 6, 6, 6, 6, 6, 6],[7, 7, 7, 7, 7, 7, 7, 7, 7, 7],[8, 8, 8, 8, 8, 8, 8, 8, 8, 8],[9, 9, 9, 9, 9, 9, 9, 9, 9, 9]])Z
Out[87]: 
array([[ 0.        ,  0.        , -0.        , -0.        , -0.        ,0.        ,  0.        ,  0.        , -0.        , -0.        ],[ 0.84147098,  0.45464871, -0.35017549, -0.83304996, -0.55002214,0.2386935 ,  0.80795544,  0.63438687, -0.12243406, -0.76668968],[ 0.90929743,  0.4912955 , -0.37840125, -0.90019763, -0.59435646,0.2579333 ,  0.87308037,  0.68552138, -0.13230281, -0.8284884 ],[ 0.14112001,  0.07624747, -0.05872664, -0.13970775, -0.09224219,0.04003041,  0.13549924,  0.10639069, -0.02053297, -0.12857871],[-0.7568025 , -0.40890213,  0.31494096,  0.74922879,  0.49467912,-0.21467625, -0.72665927, -0.57055511,  0.11011479,  0.68954566],[-0.95892427, -0.518109  ,  0.3990533 ,  0.94932784,  0.62679474,-0.27201056, -0.9207306 , -0.72293517,  0.13952351,  0.87370493],[-0.2794155 , -0.15096884,  0.11627788,  0.27661925,  0.18263816,-0.07925961, -0.26828646, -0.21065197,  0.04065496,  0.25458392],[ 0.6569866 ,  0.35497137, -0.27340289, -0.6504118 , -0.4294351 ,0.18636225,  0.63081901,  0.49530368, -0.09559157, -0.59860037],[ 0.98935825,  0.53455254, -0.4117183 , -0.97945724, -0.64668771,0.28064352,  0.94995239,  0.74587941, -0.14395166, -0.90143424],[ 0.41211849,  0.22266857, -0.1715018 , -0.40799421, -0.26937862,0.11690243,  0.39570392,  0.31069706, -0.05996325, -0.37549362]])
'''

输出等值线图成功了:

 

显而易见地,两种写法并没有改变X和Y的numpy.adarray类型,只是做了列表元素类型上的修改。

###如有疑问 欢迎留言###

本文标签: 解决报错原因分析画图pltcontourf(X Y Z)报错TypeError unhashable type numpyndarray(含详细示例讲解)