Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build a heatmap?

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)

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

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

enter image description here

like image 531
user458 Avatar asked May 29 '21 15:05

user458


People also ask

Can I make a heat map in Excel?

While you can create a heat map in Excel by manually color coding the cells. However, you will have to redo it when the values changes. Instead of the manual work, you can use conditional formatting to highlight cells based on the value.

How do you create a heatmap in Powerpoint?

You can change to a heat map, where colors represent your data, making it easy for people to take in lots of data at a quick glance. Click Home >Layer Pane. On the Field List tab, click Heat Map.


2 Answers

You need to pivot your data into a long format, using an example dataset:

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)})

Pivot will make it like this:

df.pivot(index="n_trees",columns="n_leafs")

enter image description here

We can pivot and plot:

sns.heatmap(df.pivot(index="n_trees",columns="n_leafs"))

enter image description here

like image 134
StupidWolf Avatar answered Oct 26 '22 15:10

StupidWolf


Reset the index via reset_index and then use pivot to transform the table.

df = df.reset_index()
ax = sns.heatmap(df.pivot(*df), annot=True)

enter image description here

like image 43
Nk03 Avatar answered Oct 26 '22 15:10

Nk03