0

Having a list containing lists (of different sizes) of variables:

res = [[0.01, 0.9, 0.46], [0.64, 0.24], [0.87, 0.99, 0.47, 0.75], ...]

(note that the actual lists contain ~3000 variables each)

And let's say:

x = [0, 1, 2, ...]

Is there a way to make a heat-map out of it, with the values from res on the y axis ?

This being for example the heatmap of one of one of the lists: enter image description here

I would just like to to this for every list and have them all on the same heat-map

Nawra C
  • 156
  • 13
  • You mean res on y-axis and x on x-axis? You should explain your question in greater detail. – rnso Oct 21 '20 at 16:31
  • @rnso yes exactly – Nawra C Oct 21 '20 at 16:31
  • So length of x is same as length of res? Do you want to plot each x against mean of its corresponding list in res? Explain your question more. – rnso Oct 21 '20 at 16:33
  • @rnso Imagine a scatter plot. For each value on the x axis, it has a list of values from the res list on the y axis. Is there a way to make a heat-map of the whole thing ? Without doing any mean – Nawra C Oct 21 '20 at 16:42

2 Answers2

0

In order to plot a heatmap, it's likely you will need to interpolate your data into a regular grid. The following post achieves this using scipy.interpolate.interp2d

When you have done this you could use seaborn.heatmap to plot a heatmap:

import seaborn as sns
res = [[0.01, 0.9, 0.46], [0.64, 0.24, 1], [0.87, 0.99, 0.47]]
ax = sns.heatmap(res)
  • Could you elaborate ? Do you mean using interpolation to have all the lists become of the same size ? – Nawra C Oct 21 '20 at 17:04
  • Yes exactly, currently each list inside the overall list are different lengths. Once they are the same length its possible to plot using seaborn as described above or through something like plt.contour. The following [link](https://matplotlib.org/3.1.1/gallery/images_contours_and_fields/irregulardatagrid.html#sphx-glr-gallery-images-contours-and-fields-irregulardatagrid-py) explains how to contour plot irregularly spaced data in matplotlib, but it also has an interpolation phase first – Matt_Haythornthwaite Oct 21 '20 at 17:18
  • Thanks for the help. However i came up with a solution to my problem, I will post it – Nawra C Oct 21 '20 at 17:32
  • No problem, glad you found a solution! – Matt_Haythornthwaite Oct 21 '20 at 17:35
0

I found a workaround by doing the following:

y_ax = []
cpt = 0
for i, elem in enumerate(res):
    y_ax.append([i]*len(elem))

res_flat = sum(res, [])
y_flat = sum(y_ax, [])
Nawra C
  • 156
  • 13