从 Pandas 多索引数据帧创建等高线图

Creating contour plot from Pandas multi-index dataframe

如何根据以下名为 think_or_feel 的 Pandas 数据框创建等高线图:

              think     feel
cNEU    cOPN        
y         n     27      20
n         n     40      23
y         y     43      25
n         y     97      63

我试过以下方法:

X=think_or_feel.columns
Y=think_or_feel.index 
Z=think_or_feel.values
x,y=np.meshgrid(X, Y)
plt.contourf(x,y,Z)

我收到以下错误: unhashable type: 'numpy.ndarray'

非常感谢任何帮助。

我想原因是您的 index/columns 不是数字,而 plt.contourf 需要数字。让我们试试:

X=np.arange(think_or_feel.shape[1])
Y=np.arange(think_or_feel.shape[0])
Z=think_or_feel.values

plt.contourf(x,y,Z)
plt.xticks(X, think_or_feel.columns)
plt.yticks(Y, think_or_feel.index)

输出: