Search code examples
gnuplotunstructured-data

Contour plot with only one contour label per line for unstructured or ungridded (non-rectangular) data


In cntrlabel contour plot there is an excellent approach to the same problem when the 3D datafile corresponds to a rectangular grid.

Now, when the values are ungridded (that is, randomly scattered), gnuplot needs to grid them by interpolation, via the set dgrid3d nx,ny command where nx and ny are the number of points along the x and y axes respectively.

I've done that then I've applied the solution proposed at cntrlabel contour plot

The best I've obtained, after having tried a number of variants, is this: if several lines happen to correspond to the same contour value, only one line is labeled (with -fortunately!- only one cntrlabel). The others are not labeled.

My (reduced) datafile "datos.txt" (previously sorted with zsort) is as follows:

 0   66  0.2
 0   69  0.3
 0   70  0.3
 0   72  0.7
 0   74  0.1
 0   74.1    0.1
 0   79  0.3
 0   80  0.2
 0   81  0.3
 0.1     61  -0.1
 0.1     70  0.3
 0.1     74  0.7
 0.1     75  0.8
 0.1     75.1    0.3
 0.1     78  0.2
 0.2     73  0.4
 0.2     75  0.5
 0.3     59  0.5
 0.3     74  0.4
 0.3     75  0.1
 0.3     76  0.6
 0.3     78  0.2
 0.4     71  0.3
 0.4     73  0.4
 0.4     74  0.6
 0.5     54  1.1
 0.5     69  0.2
 0.5     76  0.6
 0.5     84  0.3
 0.6     55  0.3
 0.6     67  0.3
 0.6     68  0.4
 0.6     76  0.4
 0.6     77  0.5
 0.6     79  0.2
 0.6     80  0.4
 0.7     68  0.2
 0.7     72  0.1
 0.7     73  0
 0.7     76  0.4
 0.7     76.1    0.4
 0.7     77  0.5
 0.7     77.1    0.3
 0.7     81  0.2
 0.8     46  1
 0.8     54  0.1
 0.8     63  0.5
 0.8     68  0.3
 0.8     73  -0.1
 0.8     74  0.3
 0.8     78  0.3
 0.8     81  0.3
 0.8     82  0.2
 0.9     50  0.8
 0.9     56  0.5
 0.9     60  1.2
 0.9     65  0.3
 0.9     70  0.7
 0.9     74  0.2
 0.9     79  0
 0.9     80  0.2
 0.9     80.1    0.1
 1   69  0.4
 1   76  0.5
 1   81  0.2
 1.1     54  0.3
 1.1     73  0.1
 1.1     76  0.3
 1.1     76.1    0.4
 1.1     81  0.2
 1.2     56  0
 1.2     75  0.3
 1.2     79  0.1
 1.3     68  0.3
 1.3     75  0.3
 1.3     82  0.3
 1.4     46  0.2
 1.4     66  0.7
 1.4     71  0.2
 1.4     78  0
 1.4     81  0.3
 1.4     81.1    0.4
 1.5     45  0.9
 1.5     60  1.3
 1.5     71  0.1
 1.5     76  0.1
 1.5     82  0.2
 1.5     82.1    0.2
 1.5     82.2    0.2
 1.5     84  0.2
 1.6     55  0.9
 1.6     57  1.4
 1.6     71  0
 1.6     81  0.1
 1.6     82  0.1
 1.6     83  0.1
 1.6     84  0.3
 1.7     62  0.3
 1.7     66  1
 1.7     75  0.4
 1.7     76  0.3
 1.7     78  0.3
 1.7     78.1    0.1
 1.7     80  0.1
 1.7     84  0.3
 1.8     56  0.3
 1.8     63  0.3
 1.8     67  0.6
 1.8     69  0.3
 1.8     77  0.1
 1.8     77.1    0.4
 1.8     78  0.3
 1.8     79  0.3
 1.8     84  0.2
 1.9     49  0.2
 1.9     53  0.3
 1.9     53.1    0.3
 1.9     54  0.2
 1.9     79  0.1
 1.9     82  0.3
 1.9     84  0.3
 2   60  1
 2   68  0.2
 2   73  0.5
 2   77  0.2
 2.1     45  1
 2.1     61  0.6
 2.1     66  0.4
 2.1     82  0.1
 2.2     61  0.1
 2.2     64  0.2
 2.3     64  0.5
 2.3     70  0.2
 2.3     76  0.1
 2.4     73  0.1
 2.5     53  0.2
 2.5     68  -0.1
 2.5     74  0.1
 2.5     79  0.1
 2.6     43  0.9
 2.6     65  0.8
 2.6     70  0.1
 2.8     61  0.4
 2.9     61  0.5
 3   54  0.4
 3.1     69  0.2
 3.2     57  0.3
 3.2     67  0.4
 3.5     65  1.1
 3.8     54  0.3
 3.8     60  0.2
 3.9     53  0.6
 3.9     65  0
 4.1     46  0.9
 4.1     48  0.8
 4.2     56  0.8
 4.3     43  1.1
 4.3     45  1.1
 4.6     44  1
 4.7     44  0.7
 4.7     46  0.9
 4.7     52  0.7
 5.5     44  0.9
 6.6     70  -0.5

And my code is

reset session
set dgrid3d 900,900 qnorm 9 # I use large parameters because data are highly oscillatory
set table $Contour1
    splot "datos.txt" u 1:2:3
unset table
set contour
set view 0,0,1
set cntrparam levels incremental -0.5,0.1,1.5 
set palette rgbformulae 33,13,10
        set cbrange[-0.5:1.5]
        set colorbox vertical user origin 0.93,0.38 size .02,.585

set xrange [0:*]
unset surface
set table $ContourLabels
     splot "datos.txt" u 1:2:3
#    splot $Contour1 u 1:2:3   # THIS VARIANT YIELDS A CONTOUR PLOT WITH NUMERICAL ARTIFACTS (SEE THE 2ND PLOT BELOW)
unset table

set cntrlabel format "{/:Bold %.1f}" font "Times-New Roman, 12" # THIS HAS NO EFFECT (?)
plot $Contour1 u 1:2:3 w image notitle, \
     $ContourLabels      u 1:2 w l notitle lc "black", \
     $ContourLabels      u 1:2:3 every ::1:1:1:1 w labels notitle 

The plot is as follows (except for labels and minor details): contour plot And using the variant (see the script), it looks like this: contour plot from variant with numerical artifacts

I guess the reason why it displays one cntrlabel for only one line among all lines with same cntrvalue, is that the every command (see at the end of the script) deals only with blocks but not with sub-blocks. And I have many sub-blocks per block (as many as contours for the same value) So does anyone know of any workaround to produce one cntrlabel for each contour ? Even better: one cntrlabel for every other contour ? (this, I guess with every)


Solution

  • I suggest that the resulting plot would be easier to interpret if you assigned a distinct color and/or dash pattern for each contour level that you want labelled, and then provide a key that shows which pattern indicates which level.

    Alternatively, in version 6 you could plot it with contourfill and provide a distinct color for each level. In this case the contour levels will match the colors perfectly. If you want fewer colors than contour lines (or vice versa), that is quite possible by adjusting the contour increment of the lines separately from that of the color fill boundaries.

    set dgrid3d 900,900 qnorm 9
    
    set view map
    unset key
    
    set palette rgbformulae 33,13,10
    set cbrange[-0.5:1.5]
    set colorbox vertical user origin graph 1.05, 0 size graph 0.02, 1
    
    set xrange noextend
    set yrange noextend
    
    # These control the fill
    set cbtics  -0.5,0.1,1.5 font ",8"
    set contourfill cbtics
    
    # These control the lines
    set contour
    set cntrparam levels incremental -0.5,0.1,1.5 
    set cntrlabel onecolor
    
    # Use a coarser grid when generating labels
    function $smallgrid() << EOF
        set dgrid3d 45,45 qnorm 9
    EOF
    
    splot 'contour.dat' with contourfill, \
          '' with lines lc "black" nosurface, \
          g=$smallgrid() '' with labels pointnumber 1
    

    See also https://gnuplot.sourceforge.net/demo_6.0/contourfill.html

    Revised answer

    To the best of my knowledge there is currently no way to tell gnuplot "place only one label for this contour level even though it is represented by multiple line segments in the plot". The closest I can think of is to use a coarser grid so that contours are not broken up into so many pieces. Setting the limit to one label per line segment is then closer to the desired limit.

    The functionblock syntax in gnuplot 6 can be used to change the gridding from inside a plot command. The revised plot below adds contour labels on a 45 x 45 grid with one label per line segment. The trick is to use a dummy definition g = $smallgrid() to force a single execution of a function that resets the grid parameters.

    contour labels using 45x45 grid