How to build a heatmap?

use*_*458 6 python heatmap pandas seaborn

I want to build a heatmap where on Y-axis will be number of trees, on X number of leafs, and in the center auc-roc Here is my code

df = pd.DataFrame(store,columns = ['n_trees' , 'n_leafs', 'auc-roc']) 
df.set_index(['n_trees'], inplace=True)
ax = sns.heatmap(df)
Run Code Online (Sandbox Code Playgroud)

My dataframe looks like this:

         n_leafs   auc-roc
n_trees                   
10             1  0.7
10             3  0.892529
10             5  0.107495
159            1  0.155
159            3  0.7581
...          ...       ...
1202           3  0.420
1202           5  0.422
1351           1  0.398
1351           3  0.273
1351           5  0.795
Run Code Online (Sandbox Code Playgroud)

and I get this heatmap, not something I wanted. How to delete auc-roc on X-axis and transfer it to the center?

在此处输入图片说明

Stu*_*olf 2

您需要使用示例数据集将数据转换为长格式:

import pandas as pd
import seaborn as sns
import numpy as np

np.random.seed(111)
df = pd.DataFrame({'n_trees':np.repeat([10,159,1202,1305],3),
                  'n_leafs':[1,3,5]*4,
                   'auc-roc':np.random.uniform(0,1,12)})
Run Code Online (Sandbox Code Playgroud)

枢轴将使它像这样:

df.pivot(index="n_trees",columns="n_leafs")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我们可以旋转并绘制:

sns.heatmap(df.pivot(index="n_trees",columns="n_leafs"))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述