haifengl / smile

Statistical Machine Intelligence & Learning Engine
https://haifengl.github.io
Other
6.05k stars 1.13k forks source link

Plot Heatmap clarification #787

Closed jamalromero closed 3 months ago

jamalromero commented 3 months ago

Hi, I am trying to plot a Heatmap but I don't get the desired outcome. I need some clarification. The code I am using to generate data is dependent on an external jar, but the idea is as follows. I have an array of doubles populated with values ranging from 0 to 10 called x with length 100 elements. So doube[] x = {100 elements....} I have also an array of doubles y with values ranging from 0 to 10 and length 100 elements. So doube[] y = {100 elements....} I have a 2d array z with shape (100, 100) and its values resulting from some calculations with arrays x and y. x minimum 0 and maximum 10 y minimum 0 and maximum 10 z minimum 0 and maximum 21.8

I am using the folllowing code to plot the heatmap

var canvas = Heatmap.of(x, y, z, 256).canvas();
canvas.setBound(new double[]{0, 0}, new double[]{10, 10});
canvas.toBufferedImage(600, 400);

Please see the attached image. The heatmap doesn't cover the whole area. Most of it is black. I looked at the test code example and it's using interpolation. Is that necessary? I have used Bilinear interploation but the issue is the same. I kept changing the bounds it seems to adjust but I can't any rational behind it. Also the bar on the side shows max value of 7.0 but the z array max value is 21.8

heatmap

Am I messing something with this code? Please help

haifengl commented 3 months ago

Heatmap.of(z).canvas() should be sufficient. If it is still black, check z's values.

jamalromero commented 3 months ago

Thank you! Yes indeed! I wrongly calculated the z values. The bar on the left still displays the wrong min and max. Certainly something am doing wrong, but I can live with that for now :-) Thanks!

mesh1