knowm / XChart

XChart is a light-weight Java library for plotting data.
http://knowm.org/open-source/xchart
Apache License 2.0
1.51k stars 398 forks source link

Heatmap support for double values #610

Open wcneill opened 3 years ago

wcneill commented 3 years ago

Hello,

It appears that a heatmap is only available for integer matrices? Am I missing something?

Thanks Wes

wcneill commented 3 years ago

Okay, my boss figured it out.

Assuming you have your heatmap data in a Double[][] heatData, getting a heatmap is not very straightforward. It doesn't work the same as adding a series of integer values. The List<Number[]> argument is a list of N Number[] objects where N = width * height of your heatmap data. Therefore you ned to convert your Double[][] heatData like so:

    private static List<Number[]> arrayToList(Double[][] heatData) {
        List<Number[]> list = new ArrayList<>();
        Number[] numbers = null;
        Double[] array = null;
        for (int i = 0; i < heatData.length; i++) {
          array = heatData[i];
          for (int j = 0; j < array.length; j++) {
            numbers = new Number[3];
            numbers[0] = heatData.length - i;
            numbers[1] = j;
            numbers[2] = heatData[i][j];
            list.add(numbers);
          }
        }
        return list;
      }

There are two remaining I found. One is that, unlike other plotting libraries, the coordinates (0, 0) are in the bottom left of the visualization. So I had to flip the data. That's not a big deal.

The second issue is that the axis labels do not scale well at all. Here's a 500 by 500 element heatmap representing an affinity matrix of a fully connected graph:

image