0

I have two 1D arrays. One containing temperature and the other radial distance (for each respective temperature). I want to produce a heat map type plot using this information. Here is where I'm running into issues: 1. If I create a 2d numpy grid, how do I correlate a radial distance to each one? Say the radial distance is 5 units, how to I find all grid squares that are 5 units from the center? 2. Then how to I correlate to each temperature its respective set of grid points. So say the temperate is 20 degrees at radial distance 5, how do I express this as it is 20 degrees at the following set of x,y grid squares?

Thanks for any assistance.

Gabrielle
  • 1
  • 3

1 Answers1

0

meshgrid is your friend here. First set up the grid plus x and y coordinate grids (you will have two 5 by 5 arrays):

import numpy as np
x, y = np.meshgrid(np.arange(-2, 3), np.arange(-2, 3))
heatmap = 0 * x  # easy way to get shape right

Now, fake some data:

r = np.array((0, 0.5, 1.5, 2.5))  # Your radial distance
T = np.array((100, 90, 70, 40))   # Your temperature at distance

Overlay the data from the inside outward, starting from middle (assuming r is monotonically increasing):

r2 = r**2
xy2 = x**2 + y**2
for ii in range(r.size):
   heatmap[np.where(xy2 >= r2[ii])] = T[ii]

That's it. Here's the resulting heatmap:

array([[ 40,  70,  70,  70,  40],
       [ 70,  90,  90,  90,  70],
       [ 70,  90, 100,  90,  70],
       [ 70,  90,  90,  90,  70],
       [ 40,  70,  70,  70,  40]])
Frank M
  • 1,550
  • 15
  • 15
  • Hi, I tried this out using my actual data. I should mention that my actual data has arrays with more entries (closer to 2000 or so). But in any event. I plot using plt.contourf(heatmap) and it produces an image that is just a blue square. Might I be doing something wrong? The only thing that I changed were r and T, PROFILER/T are just functions from earlier in the code r = PROFILER(1.3,4.3e13) T = PROFILET(1.3,4.3e13) plt.contourf(heatmap, 100) plt.show() – Gabrielle Apr 22 '15 at 02:40
  • @Gabrielle: Did you try the "fake" data and get the same result as I did? I'm assuming you order the `r` values from low to high, so if they are randomized you'll have to do something a bit more sophisticated, like sort the two arrays. – Frank M Apr 22 '15 at 18:20
  • Yep I tried with the fake data and I got the same results you did. For my real data, luckily r is in ascending order so that is not an issue. When I place in my arrays, both 1890 elements long verses the 4 element long arrays used in the example code, I get an output heat map of all zeros except at the center, where the value is, oddly, 15. – Gabrielle Apr 22 '15 at 19:59
  • Also when I do the code I have to exclude the word "where" in the last line. – Gabrielle Apr 22 '15 at 20:05
  • OK, does the size of your `heatmap` match the range of radial distances? You may have to scale `r` if, for example your radial distances go up to 1000 (of some unit) but you want `heatmap` to be 256 by 256 (just guessing at numbers here). – Frank M Apr 22 '15 at 20:06
  • `where` is supposed to be from numpy, so it should probably be `np.where`. Sorry about that (I use IPython, and `where` is already pulled into the global namespace). – Frank M Apr 22 '15 at 20:08