Roby
Roby

Reputation: 2061

Heatmap from List

I have a list x which contains intensities in the range 0 to 1. I want to create a heatmap with a squared grid.

For example:

x = [0, 0.1, 0, 0.5, ..., 0.5]
n = len(x)    
dim = math.sqrt(n) + 1

But how can I create an array with dim × dim (the not available last values could be zero) and create the heatmap in a specified size (for example 1024×769)?

Upvotes: 0

Views: 7134

Answers (1)

giosans
giosans

Reputation: 1178

As mentioned in the comment, you could use numpy, and plot with the help of seaborn:

import numpy as np
import math
import matplotlib.pyplot as plt
import seaborn as sns

x_list = np.random.rand(100).tolist()
x = np.array((x_list))
# x_res=x.reshape(math.sqrt(len(x)),math.sqrt(len(x))) #old
x_res=x.reshape(int(math.sqrt(len(x))),int(math.sqrt(len(x))))

fig, ax = plt.subplots(figsize=(15,15))
sns.heatmap(x_res, square=True, ax=ax)
plt.yticks(rotation=0,fontsize=16);
plt.xticks(fontsize=12);
plt.tight_layout()
plt.savefig('colorlist.png')

Which producesresult

Upvotes: 2

Related Questions