-3

I have the following pandas Dataframe. alfa_value and beta_value are random, ndcg shall be the parameter deciding the color.

enter image description here

The question is: how do I do a heatmap of the pandas Dataframe?

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
oleber
  • 1,089
  • 4
  • 12
  • 25
  • With matplotlib or seaborn or other?. How many bins? – Corralien Mar 04 '22 at 16:23
  • matplotlib or seaborn is OK :) Please remember that `alfa_value` and `beta_value` are random (random search), so most likely unique. The idea is to have a grid where we average the ndcg on the case we have multiple values for the some place. – oleber Mar 04 '22 at 16:27
  • Do you need to bin your `alfa_value` and `beta_value`? – Corralien Mar 04 '22 at 16:27
  • I would say we need – oleber Mar 04 '22 at 16:36
  • 2
    Please see [ask] and [reprex] and [good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). – BigBen Mar 04 '22 at 17:18

2 Answers2

1

You can use the code below to generate a heatmap. You have to adjust the bins to group your data (analyze the mean, the std, ...)

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

rng = np.random.default_rng(2022)
df = pd.DataFrame({'alfa_value': rng.integers(1000, 10000, 1000),
                   'beta_value': rng.random(1000),
                   'ndcg': rng.random(1000)})

out = df.pivot_table('ndcg', pd.cut(df['alfa_value'], bins=10), 
                     pd.cut(df['beta_value'], bins=10), aggfunc='mean')
sns.heatmap(out)
plt.tight_layout()
plt.show()

enter image description here

Corralien
  • 109,409
  • 8
  • 28
  • 52
0

In general, Seaborn's heatmap function is a nice way to color pandas' DataFrames based on their values. Good examples and descriptions can be found here.

Since you seem to want to color the row based on a different column, you are probably looking for something more like these answers.

Leo
  • 446
  • 2
  • 9