【问题标题】:Creating contour plot from Pandas multi-index dataframe从 Pandas 多索引数据框创建等高线图
【发布时间】:2021-01-14 14:19:56
【问题描述】:

如何从名为 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'

非常感谢您对此的任何帮助。

【问题讨论】:

  • 如果您共享可运行代码,人们会更容易提供帮助。
  • @swatchai OP 询问为什么提供的代码没有运行。
  • @QuangHoang 我的意思是最少的可运行代码,包括创建数据框的代码等。
  • @swatchai 包含的数据框很好。您可以复制它并在大多数系统上执行pd.read_clipboard()
  • @QuangHoang 感谢您的有用提示。但它在我的机器上不起作用。

标签: python-3.x pandas matplotlib multi-index contour


【解决方案1】:

我猜原因是您的索引/列不是数字,而 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)

输出:

【讨论】:

  • 非常有帮助。欣赏它。谢谢!
猜你喜欢
  • 2014-07-24
  • 2021-04-03
  • 1970-01-01
  • 1970-01-01
  • 2020-02-22
  • 1970-01-01
  • 1970-01-01
  • 2020-10-22
  • 2021-04-24
相关资源
最近更新 更多