Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python matplotlib making heat map out of tuples (x,y,value)

I have tuples of 3 numbers that I want to plot in a heat map. The first 2 numbers are variables/coordinates and the third is the value. I think the easiest way might be to turn them into a 40X40 data frame and use plt.pcolor(df) but I can't figure out how to turn it into a data frame. Any other way to do it would be appreciated too. I'm generating the tuples with this code tuples.append((var1,var2,value)) in a for loop so I could put them directly into a data frame or anything else(don't know how though). Thanks to anyone who can help me!

[(-0.2, -0.2, 2.46), (-0.2, -0.19, 2.58), (-0.2, -0.18, 2.53), (-0.2, -0.17, 2.48)]

like image 920
puzzler Avatar asked Mar 15 '26 06:03

puzzler


1 Answers

Instead of creating tuples, I would recommend creating three different lists of the same length. Then you can use numpy.histogram2d:

import numpy as np
import matplotlib.pyplot as plt

# here are the three lists
# for example, you could do random values to demo
x = np.random.randn(8873)
y = np.random.randn(8873)
weights = np.random.rand(8873)

heatmap, _, _ = np.histogram2d(x, y, weights=weights)

plt.clf()
plt.imshow(heatmap)
plt.show()
like image 184
T. Silver Avatar answered Mar 17 '26 19:03

T. Silver