Keccogrin
Keccogrin

Reputation: 11

How to get contour or surface plot form datafile (with gnuplot)?

I would like to use gnuplot to plot my 2d function ch(x,y). My datafile is structured in this way:

x1 y1 ch(x1,y1)
x2 x2 ch(x2,y2)
x3 y3 ch(x3,y3)
x1 y1 ch(x1,y1)

x4 y4 ch(x4,y4)
...
...
...

...

where I have the value of my function at the 3 vertices of every triangle which constitutes my (triangular, not structured) mesh (see figure here http://i65.tinypic.com/2mydkq9.jpg). What I would like to get is two separate figures: 1. something like this for the surface, with a colorbar as a legend: http://i68.tinypic.com/egvkzr.jpg and 2. something similar but for contour lines, with a colorbar as a legend.

How can I get these two figures with gnuplot? I've tried for example doing the first one :

set palette rgbformulae 33,13,10
set xrange [0: 0.25]
set yrange [0: 0.20]
set view map
splot "mydatafile.txt" w l pal 

but the triangles inside are white (not filled with colors) and only the edges of the triangles are coloured. How about the contour lines?

Thank you in advance,

Keccogrin

Upvotes: 1

Views: 1977

Answers (2)

bambino350
bambino350

Reputation: 304

You could let gnuplot generate the gridded data for you like so:

set dgrid3d spline
set table $Gridded
splot "mydatafile.txt"
unset table
unset dgrid3d

Then you can plot the (gridded) data together with contours like this:

set view map
set contour base
set cntrlabel onecolor
set cntrlabel  format '%h' font ',8'
set cntrlabel start 25 interval 200
set cntrparam bspline
set cntrparam order 10
set cntrparam levels 10 
set pm3d at b explicit
splot $Gridded with pm3d, \
      $Gridded with labels nosurface

The first splot line will give you the heatmap and contour, the second line will give you the contour labels. Also, you might want to read the documentation on dgrid3d and play with the options. Depending on your data spline may not give satisfactory results (I had some good success using a gauss filter with a relatively large dx value, but it really depends on your data and how "smooth" you would like the heatmap to look).

Upvotes: 1

Joce
Joce

Reputation: 2332

In order to make a colourmap, you'll need to preprocess your data so as to separate each triangle from the following one by two empty lines, and duplicate one of the points so as to make a degenerate quadrangle, with one empty line in the middle:

x1 y1 ch(x1,y1)
x2 x2 ch(x2,y2)

x3 y3 ch(x3,y3)
x3 y3 ch(x3,y3)


x1 y1 ch(x1,y1)
x2 x2 ch(x2,y2)

x4 y4 ch(x4,y4)
x4 y4 ch(x4,y4)

...

Then:

set view map
set pm3d interpolate 10,10 corners2color mean
splot "data.dat" notitle with pm3d

To process your data, you can use this awk script:

{
 print $0
 if (NR % 3 == 2) print ""
 if (NR % 3 == 0) print $0 "\n\n"
}

Under a linux shell, run it with awk -f script.awk data.txt > data.dat. If you're plotting just once, you can run it "on the fly" in the argument of plot (like plot "<awk -f script.awk data.txt"), but this is not advisable for a large file if you need to plot several times (so use only in a gnuplot script that e.g. would automate the plotting).

Upvotes: 1

Related Questions