1

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? Commented May 25, 2012 at 14:06
  • My problem is that gaps show up in the plot. So I would need a procedure to plot a matrix without gaps or interpolations. Commented May 28, 2012 at 8:45

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, but this does not answer the problem of the interpolation. I already have my data in the right format. The problem is that with the procedure you described for an MxN matrix, we would recover a plot with (M-1)x(N-1) frames, with colours corresponding to the interpolation between matrix values. This is not what I want. I would just want to use an equivalent procedure to plot the MxN points.
@ricoamor -- plotting with image (the latter) plots MxN points.
@ricoamor -- I've edited with a pm3d solution that I'm sure will work ... but it requires a little more effort on your part. I've tried to explain why this is the case -- Hopefully it was clear enough.
@mglison - thanks for the response. I really want to plot all the points in my matrix instead of the (M-1)x(N-1). My matrix is a reasonably long time series for ~10 cells. Thanks for providing a way to patch the defaults from Gnuplot. Being slightly inconvenient I think I will opt for the matplotlib alternative.
@ricoamor -- No problem. Matplotlib is a fantastic library that I haven't spent enough time with (despite spending a lot of time writing python code and making plots). Anyway, best of luck.
0

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

1 Comment

Thanks, but again this is not a question about how to format the data. It's about the difference between plotting an MxN matrix using (M-1)x(N-1) frames or just plotting the full matrix. That is what imshow in matplotlib would do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.