1
votes

I am trying to plot a matrix in Gnuplot as I would using imshow in Matplotlib. That means I just want to plot the actual matrix values, not the interpolation between values. I have been able to do this by trying

splot "file.dat" u 1:2:3 ps 5 pt 5 palette

This way we are telling the program to use columns 1,2 and 3 in the file, use squares of size 5 and space the points with very narrow gaps. However the points in my dataset are not evenly spaced and hence I get discontinuities.

Anyone a method of plotting matrix values in gnuplot regardless of not evenly spaced in Xa and y axes?

2
Is your problem that you can't get gnuplot to plot the matrix with uneven spacings, or that the uneven spacing between points produces gaps?andyras
My problem is that gaps show up in the plot. So I would need a procedure to plot a matrix without gaps or interpolations.ricoamor

2 Answers

1
votes

Gnuplot doesn't need to have evenly space X and Y axes. ( see another one of my answers: https://stackoverflow.com/a/10690041/748858 ). I frequently deal with grids that look like x[i] = f_x(i) and y[j] = f_y(j). This is quite trivial to plot, the datafile just looks like:

#datafile.dat
x1 y1 z11
x1 y2 z12
...
x1 yN z1N
            #<--- blank line (leave these comments out of your datafile ;)
x2 y1 z21
x2 y2 z22
...
x2 yN z2N
            #<--- blank line
...
...
            #<--- blank line
xN y1 zN1
...
xN yN zNN

(note the blank lines)

A datafile like that can be plotted as:

set view map
splot "datafile.dat" u 1:2:3 w pm3d

the option set pm3d corners2color can be used to fine tune which corner you want to color the rectangle created.

Also note that you could make essentially the same plot doing this:

set view map
plot "datafile.dat" u 1:2:3 w image

Although I don't use this one myself, so it might fail with a non-equally spaced rectangular grid (you'll need to try it).

Response to your comment

Yes, pm3d does generate (M-1)x(N-1) quadrilaterals as you've alluded to in your comment -- It takes the 4 corners and (by default) averages their value to assign a color. You seem to dislike this -- although (in most cases) I doubt you'd be able to tell a difference in the plot for reasonably large M and N (larger than 20). So, before we go on, you may want to ask yourself if it is really necessary to plot EVERY POINT.

That being said, with a little work, gnuplot can still do what you want. The solution is to specify that a particular corner is to be used to assign the color to the entire quadrilateral.

#specify that the first corner should be used for coloring the quadrilateral
set pm3d corners2color c1 #could also be c2,c3, or c4.

Then simply append the last row and last column of your matrix to plot it twice (making up an extra gridpoint to accommodate the larger dataset. You're not quite there yet, you still need to shift your grid values by half a cell so that your quadrilaterals are centered on the point in question -- which way you shift the cells depends on your choice of corner (c1,c2,c3,c4) -- You'll need to play around with it to figure out which one you want.

Note that the problem here isn't gnuplot. It's that there isn't enough information in the datafile to construct an MxN surface given MxN triples. At each point, you need to know it's position (x,y) it's value (z) and also the size of the quadrilateral to be draw there -- which is more information than you've packed into the file. Of course, you can guess the size in the interior points (just meet halfway), but there's no guessing on the exterior points. but why not just use the size of the next interior point?. That's a good question, and it would (typically) work well for rectangular grids, but that is only a special case (although a common one) -- which would (likely) fail miserably for many other grids. The point is that gnuplot decided that averaging the corners is typically "close enough", but then gives you the option to change it.

0
votes

See the explanation for the input data here. You may have to change your data file's format accordingly.