Tuesday, 21 September 2010

Projecting contours

Karl asked a question some time ago, in which he wanted to know how one can produce this graph. As I pointed out in my reply, it is rather easy, if we can rotate the data file by 90 degrees. I will only post a skeleton here, you can dress up the graph at your will.

For a start, here is our data file, which we will call 'out.dat'
-0.299  -0.265  -0.215  -0.151  -0.078  0.000   0.078   0.151   0.215   0.265   0.299
-0.513  -0.455  -0.368  -0.259  -0.134  0.000   0.134   0.259   0.368   0.455   0.513
-0.694  -0.616  -0.499  -0.351  -0.181  0.000   0.181   0.351   0.499   0.616   0.694
-0.833  -0.738  -0.596  -0.411  -0.191  0.037   0.243   0.430   0.600   0.739   0.833
-0.919  -0.812  -0.624  -0.271  0.287   0.736   0.767   0.658   0.697   0.819   0.920
-0.949  -0.832  -0.582  0.048   1.186   2.000   1.680   1.007   0.781   0.851   0.949
-0.919  -0.812  -0.624  -0.271  0.287   0.736   0.767   0.658   0.697   0.819   0.920
-0.833  -0.738  -0.596  -0.411  -0.191  0.037   0.243   0.430   0.600   0.739   0.833
-0.694  -0.616  -0.499  -0.351  -0.181  0.000   0.181   0.351   0.499   0.616   0.694
-0.513  -0.455  -0.368  -0.259  -0.134  0.000   0.134   0.259   0.368   0.455   0.513
-0.299  -0.265  -0.215  -0.151  -0.078  0.000   0.078   0.151   0.215   0.265   0.299
and its "rotated" pair, 'out2.dat'
-0.299  -0.513  -0.694  -0.833  -0.919  -0.949  -0.919  -0.833  -0.694  -0.513  -0.299
-0.265  -0.455  -0.616  -0.738  -0.812  -0.832  -0.812  -0.738  -0.616  -0.455  -0.265
-0.215  -0.368  -0.499  -0.596  -0.624  -0.582  -0.624  -0.596  -0.499  -0.368  -0.215
-0.151  -0.259  -0.351  -0.411  -0.271  0.048   -0.271  -0.411  -0.351  -0.259  -0.151
-0.078  -0.134  -0.181  -0.191  0.287   1.186   0.287   -0.191  -0.181  -0.134  -0.078
0.000   0.000   0.000   0.037   0.736   2.000   0.736   0.037   0.000   0.000   0.000
0.078   0.134   0.181   0.243   0.767   1.680   0.767   0.243   0.181   0.134   0.078
0.151   0.259   0.351   0.430   0.658   1.007   0.658   0.430   0.351   0.259   0.151
0.215   0.368   0.499   0.600   0.697   0.781   0.697   0.600   0.499   0.368   0.215
0.265   0.455   0.616   0.739   0.819   0.851   0.819   0.739   0.616   0.455   0.265
0.299   0.513   0.694   0.833   0.920   0.949   0.920   0.833   0.694   0.513   0.299

These were produced in octave by the function
f(x,y) = sin(y/4)*cos(x/4)+exp(-x*x - y*y/3)
Instead of actually rotating the date file, I simply interchanged the variables, and printed out the file for a second time, for I was a bit lazy...

Anyway, this is what we have to do:
reset
unset key
set contour base
set pm3d at ss

set xrange [-2:10]
set yrange [0:12]

splot for [i=1:10:2] 'out.dat' u (-2):0:i w l lt i, \
for [i=1:10:2] 'out2.dat' u 0:(12):i w l lt i,\
'out.dat' matrix w pm3d

This is really simple: We plot the contours by plotting 'out.dat', and 'out2.dat' column by column, and keeping the first and second coordinates constant. In this way, we "project" those columns onto the y-z and x-z planes. In order to make the contours more visible, we have to specify an xrange and yrange which is a bit bigger, than our actual data set. At the end, we plot the data file as surface. If we set the contour beforehand, we will see the contours on the bottom.
And here is the figure that we have just produced. Don't be fooled by the fact that there are only three lines on the x-z plane: since our function was symmetric in y with respect to, some contour lines will overlap. And again, this graph should still be properly annotated.

11 comments:

  1. Thank you Zoltan,

    I have one more question related to this. When you issue the following commands:

    set grid xtics
    set grid ytics
    set grid ztics

    you get a true 'grid' on the xy plane but only parallel lines on the xz and yz planes. Is it possible to extend the x and y axis grid lines onto the two z planes?

    Karl

    ReplyDelete
  2. Hi Karl,

    I think, the short answer to your question is no. The slightly longer answer is yes. I have had the same problem before, but the only thing I could come up with is this. (This should work for data files, but you might run into problems with function plots.)

    g(x) = (x > 11.9999 ? 2 : -2)
    set sample 1000
    splot g(y) w l, g(-x) w l, 'foo.dat' matrix

    This will place the grid on both the bottom, and the vertical panes. This is an ugly hack, but what the heck!
    Cheers,
    Zoltán

    ReplyDelete
  3. Greetings, Gnuplotter!

    I hate to hijack a thread, but couldn't find any other way to contact you. I'm trying to do a pm3d terrain"-style plot of a datafile with three columns, looking like this:

    25-OCT-10 45 9.2

    There will be numerous rows for each date, but no matter how I plot this I can't seem to get it to remove the duplicate labels on the xtics... i.e., the xticlabels are unreadable because gnuplot wants to have a label for every row.

    Any thoughts?

    Thanks!

    ReplyDelete
  4. Hi,
    Thanks you for sharing your GNUPLOT tricks. I benefited from your tricks to produce great looking plots for my work. I recently had a problem where I need to plot unit vectors on a sphere. With some effort I could eventually find a way to do that. But some of the data points are not visible which are on the other side and since I also wanted to have different colours for different sets of vectors. So, I ultimately realized that, for better visualization of my data, I need to plot these vectors on some MAP projection scheme(say, mollweide or aitoff, etc..) with different colours for the desired vector sets(vectors means i have corresponding (theta,phi)). If you have a solution for my problem, it would be of great help.

    Thanks,
    JQ.

    ReplyDelete
  5. Hi Johny,

    It might be a bit late for you, but if you could say a bit more about what you are trying to do, I would look at it, and come up with some solution.
    Cheers,
    Zoltán

    ReplyDelete
  6. Hi,

    You've got a really nice blog. Can you make a pdf of all your tricks?

    Thanks,

    Cheers,
    Ketan.

    ReplyDelete
  7. Hi Zoltán,

    thanks for sharing your gnuplot experience! Perhaps you would like to enter a contest about gnuplot? Or would you like to write about the contest in your blog, to give your readers a chance to win a gnuplot cookbook? It's here: http://latex-community.org/component/content/article/92-contests/431-gnuplot-book

    ReplyDelete
  8. Hi!

    Can you help me? Do you know if I can use different colorboxes in the same plot? For example one half of the plot uses one column to give the color to the points and the other half uses another column with a different scale. For example:

    pl 'data' u ($2<=0.?$2:1/0):3:7 w d lc palette z,''u ($2>=0.?$2:1/0):3:12 w d lc palette z

    where column 3 gives color to left side of the graph, and column 12 gives color to the right side. The problem is that the ranges of columns 3 and 7 are very different so they should be used independently.

    Any idea?

    Thanks!!

    ReplyDelete
  9. Fantastic website and blog, I've learned a great deal from both, used it to automate this plotting with scripts: http://www.lehigh.edu/~mtb308/spectra.html

    ReplyDelete
  10. The images don't work animore. In none of your blog posts. Such a pity.

    ReplyDelete