-1

I want to draw a heatmap, I have a 2d array and I followed this answer : https://stackoverflow.com/a/32459287/2370139 Except that I use gnuplot within c++ and I removed "nonuniform" since the first row and columns are regular values, not ticks.

Gnuplot gp;
gp << "set autoscale xfix \n";
gp << "set autoscale yfix \n";
gp << "set autoscale cbfix \n";
gp << "plot '-' matrix with image notitle\n";
gp.send2d(pmat);
gp.flush();

pmat is a 2d array of size 50*50. It is filled with float values between 0 and 1. It does plot a perfectly white grid, what could the issue be ?

NOTE : The above commands work fine when used in a normal gnuplot terminal with a text file like

0.5 0.3 0.3
0.2 0.4 0.6
0.2 0.8 1

so the problem has to come from my usage of the C++ api

user2370139
  • 1,297
  • 1
  • 11
  • 13
  • I am not familiar with `gnuplot-iostream`, but according to [this](https://github.com/dstahlke/gnuplot-iostream/wiki/Datatypes#1d-vs-2d-data) it seems that `send2d` does not send the array data in matrix form, but as a one-dimensional list with blank spaces representing new rows or columns. What happens if you replace your plot command by `gp << "splot '-' with lines notitle\n";`? – user8153 Oct 20 '17 at 04:41
  • Thank you for your comment, it does work with that line, however it draws a 3D graph , while what I want is a heatmap (the color at (x,y) represents pmat[x][y]). – user2370139 Oct 20 '17 at 07:58
  • Have you simply tried to remove the `matrix` keyword, i.e. using only `plot "-" with image`? – Christoph Oct 20 '17 at 20:12

1 Answers1

0

If gnuplot-iostream doesn't have built-in support for 2-d data in matrix format you can easily implement that in your code. Something like

gp << "plot '-' matrix with image notitle\n";
for (int i = 0; i < 50; ++i) {
    for (int j = 0; j < 50; ++j)
        gp << pmat[i][j] << "\t";
    gp << "\n";
}
gp.flush();

(not tested) should work.

user8153
  • 4,049
  • 1
  • 9
  • 18