admin管理员组

文章数量:1410712

The following script is based on "How to create a 2D contour plot for the density of scattered data in gnuplot?". The problem I am encountering is that inside a loop the contouring part with splot does not work. For some reason $Contours is not filled and therefore the plotting of the contours fails.

reset

set datafile missing "NaN"
set datafile columnheader

$Data << EOD
gp   HS   Age     lh_01    rh_01    lh_02  rh_02
2    0    37.74   6825.10  7774.17  77.78  71.94
2    0    26.42   7231.53  7881.17  82.45  91.83
2    0    30.20   7019.94  7592.51  71.41  81.06
2    0    27.68   6650.53  6297.27  70.95  65.84
2    1    18.87   7673.02  9077.02  79.10  94.45
2    0    34.56   7153.45  7772.87  73.05  80.02
2    0    33.54   7446.38  7475.98  76.63  75.81
3    1    21.47   8940.42  9105.92  91.78  95.23
1    0    22.00   8159.81  9212.58  85.17  88.81
1    0    24.96   7737.62  8514.96  80.01  83.79
3    1    12.10   9484.95  9580.21  93.70  98.47
2    0    24.72   8751.51  8804.71  89.20  89.51
2    0    38.00   7545.44  7722.34  74.76  79.06
2    0    25.50   8036.86  8924.18  81.61  92.17
2    0    30.10   7644.28  7932.17  73.81  80.13
2    1    19.00   7999.24  8982.45  77.74  92.37
3    1    22.00   8929.76  9353.21  90.28  94.14
1    0    23.00   8441.65  9082.74  85.61  90.79
1    0    25.00   7977.73  8536.21  81.77  85.29
2    0    24.80   8791.79  8825.06  86.37  89.34
2    0    37.90   7541.46  7830.42  75.65  78.84
2    0    26.50   7739.28  9097.79  83.06  91.95
2    0    30.30   7591.99  7939.94  75.38  79.94
2    0    27.80   6744.32  6299.78  72.39  68.46
2    1    19.10   7826.40  8984.48  77.72  92.74
2    0    34.10   6984.10  7772.61  74.83  80.35
2    0    33.10   7451.13  7545.61  75.75  76.31
3    1    22.10   8969.01  9178.56  90.47  94.74
1    0    23.10   8418.65  9123.69  85.91  91.01
1    0    25.10   8004.52  8537.93  82.05  85.45
2    0    24.90   8793.37  8827.71  87.29  89.75
2    0    38.10   7543.81  7835.48  75.94  79.15
2    0    25.60   8044.60  8928.25  81.91  92.38
2    0    30.40   7594.70  7947.19  74.90  79.89
2    0    28.10   6953.57  6309.59  72.77  67.23
2    1    19.20   7993.29  8987.95  78.03  92.96
2    0    34.20   6987.95  7774.64  74.97  80.68
2    0    33.20   7453.95  7547.34  75.98  76.61
3    1    22.20   8977.50  9179.88  90.65  94.89
1    0    23.20   8426.72  9127.87  86.10  91.12
1    0    25.20   8007.76  8539.89  82.21  85.60
2    0    25.10   8794.42  8830.71  87.41  89.88
2    0    38.20   7545.18  7839.56  76.03  79.34
2    0    25.70   8048.18  8930.73  82.03  92.59
2    0    30.50   7597.63  7957.60  75.09  80.13
2    0    28.20   6954.75  6313.57  72.88  67.37
2    1    19.30   8002.18  8991.67  78.21  93.18
2    0    34.30   6990.29  7777.06  75.08  80.82
2    0    33.30   7455.29  7549.89  76.10  76.91
3    1    22.30   8985.21  9181.11  90.83  95.04
1    0    23.30   8434.78  9132.04  86.28  91.23
1    0    25.30   8011.00  8540.90  82.36  85.74
2    0    25.20   8795.45  8834.17  87.53  90.00
EOD

LABELS = "lh_01 rh_01 lh_02 rh_02"

search_string = 'lh'

distance(x0, y0, x1, y1) = sqrt((x1 - x0)**2 + (y1 - y0)**2)

do for [selected_label in LABELS] {
    if (strstrt(selected_label, search_string) > 0) {
        selected_label = substr(selected_label, strlen(search_string) + 1, strlen(selected_label))

        left = "lh".selected_label
        right = "rh".selected_label

        set table $Temp
        plot $Data using left:right with table
        unset table
        
        R = 200.0   # Radius to check
        set print $Density
        do for [i = 1:|$Temp|] {
            x0 = real(word($Temp[i], 1))
            y0 = real(word($Temp[i], 2))
            c  = 0
            stats $Temp u (distance(x0, y0, $1, $2) <= R ? c = c + 1 : 0) nooutput
            d = c / (pi * R**2)  # density: points per unit area
            print sprintf("%g %g %f", x0, y0, d)
        }
        unset print
        
        stats $Density u 3 nooutput
        
        set table $Density_norm
        plot $Density u 1:2:($3 / STATS_max) w table
        unset table

        plot $Density_norm u 1:2:3 w p pt 7 ps 2 lc palette z notitle
        
        pause -1

        set table SURF_CONT = "SO79472868_s_c.dat"
        set table $Surface_Contour
            set dgrid3d 50, 50 gauss 0.1
            set contour surface
            set cntrparam levels incremental 0.0, 0.1, 1.0
            splot $Density_norm u 1:2:3
        set table $Contours
            splot $Density_norm u 1:2:3 nosurface
            unset dgrid3d
            unset contour
        unset table
        
        print $Contours
        
        pause -1
        
        plot $Contours u 1:2:3 w l lw 2 lc palette z
        
        pause -1
    }
}

The following script is based on "How to create a 2D contour plot for the density of scattered data in gnuplot?". The problem I am encountering is that inside a loop the contouring part with splot does not work. For some reason $Contours is not filled and therefore the plotting of the contours fails.

reset

set datafile missing "NaN"
set datafile columnheader

$Data << EOD
gp   HS   Age     lh_01    rh_01    lh_02  rh_02
2    0    37.74   6825.10  7774.17  77.78  71.94
2    0    26.42   7231.53  7881.17  82.45  91.83
2    0    30.20   7019.94  7592.51  71.41  81.06
2    0    27.68   6650.53  6297.27  70.95  65.84
2    1    18.87   7673.02  9077.02  79.10  94.45
2    0    34.56   7153.45  7772.87  73.05  80.02
2    0    33.54   7446.38  7475.98  76.63  75.81
3    1    21.47   8940.42  9105.92  91.78  95.23
1    0    22.00   8159.81  9212.58  85.17  88.81
1    0    24.96   7737.62  8514.96  80.01  83.79
3    1    12.10   9484.95  9580.21  93.70  98.47
2    0    24.72   8751.51  8804.71  89.20  89.51
2    0    38.00   7545.44  7722.34  74.76  79.06
2    0    25.50   8036.86  8924.18  81.61  92.17
2    0    30.10   7644.28  7932.17  73.81  80.13
2    1    19.00   7999.24  8982.45  77.74  92.37
3    1    22.00   8929.76  9353.21  90.28  94.14
1    0    23.00   8441.65  9082.74  85.61  90.79
1    0    25.00   7977.73  8536.21  81.77  85.29
2    0    24.80   8791.79  8825.06  86.37  89.34
2    0    37.90   7541.46  7830.42  75.65  78.84
2    0    26.50   7739.28  9097.79  83.06  91.95
2    0    30.30   7591.99  7939.94  75.38  79.94
2    0    27.80   6744.32  6299.78  72.39  68.46
2    1    19.10   7826.40  8984.48  77.72  92.74
2    0    34.10   6984.10  7772.61  74.83  80.35
2    0    33.10   7451.13  7545.61  75.75  76.31
3    1    22.10   8969.01  9178.56  90.47  94.74
1    0    23.10   8418.65  9123.69  85.91  91.01
1    0    25.10   8004.52  8537.93  82.05  85.45
2    0    24.90   8793.37  8827.71  87.29  89.75
2    0    38.10   7543.81  7835.48  75.94  79.15
2    0    25.60   8044.60  8928.25  81.91  92.38
2    0    30.40   7594.70  7947.19  74.90  79.89
2    0    28.10   6953.57  6309.59  72.77  67.23
2    1    19.20   7993.29  8987.95  78.03  92.96
2    0    34.20   6987.95  7774.64  74.97  80.68
2    0    33.20   7453.95  7547.34  75.98  76.61
3    1    22.20   8977.50  9179.88  90.65  94.89
1    0    23.20   8426.72  9127.87  86.10  91.12
1    0    25.20   8007.76  8539.89  82.21  85.60
2    0    25.10   8794.42  8830.71  87.41  89.88
2    0    38.20   7545.18  7839.56  76.03  79.34
2    0    25.70   8048.18  8930.73  82.03  92.59
2    0    30.50   7597.63  7957.60  75.09  80.13
2    0    28.20   6954.75  6313.57  72.88  67.37
2    1    19.30   8002.18  8991.67  78.21  93.18
2    0    34.30   6990.29  7777.06  75.08  80.82
2    0    33.30   7455.29  7549.89  76.10  76.91
3    1    22.30   8985.21  9181.11  90.83  95.04
1    0    23.30   8434.78  9132.04  86.28  91.23
1    0    25.30   8011.00  8540.90  82.36  85.74
2    0    25.20   8795.45  8834.17  87.53  90.00
EOD

LABELS = "lh_01 rh_01 lh_02 rh_02"

search_string = 'lh'

distance(x0, y0, x1, y1) = sqrt((x1 - x0)**2 + (y1 - y0)**2)

do for [selected_label in LABELS] {
    if (strstrt(selected_label, search_string) > 0) {
        selected_label = substr(selected_label, strlen(search_string) + 1, strlen(selected_label))

        left = "lh".selected_label
        right = "rh".selected_label

        set table $Temp
        plot $Data using left:right with table
        unset table
        
        R = 200.0   # Radius to check
        set print $Density
        do for [i = 1:|$Temp|] {
            x0 = real(word($Temp[i], 1))
            y0 = real(word($Temp[i], 2))
            c  = 0
            stats $Temp u (distance(x0, y0, $1, $2) <= R ? c = c + 1 : 0) nooutput
            d = c / (pi * R**2)  # density: points per unit area
            print sprintf("%g %g %f", x0, y0, d)
        }
        unset print
        
        stats $Density u 3 nooutput
        
        set table $Density_norm
        plot $Density u 1:2:($3 / STATS_max) w table
        unset table

        plot $Density_norm u 1:2:3 w p pt 7 ps 2 lc palette z notitle
        
        pause -1

        set table SURF_CONT = "SO79472868_s_c.dat"
        set table $Surface_Contour
            set dgrid3d 50, 50 gauss 0.1
            set contour surface
            set cntrparam levels incremental 0.0, 0.1, 1.0
            splot $Density_norm u 1:2:3
        set table $Contours
            splot $Density_norm u 1:2:3 nosurface
            unset dgrid3d
            unset contour
        unset table
        
        print $Contours
        
        pause -1
        
        plot $Contours u 1:2:3 w l lw 2 lc palette z
        
        pause -1
    }
}
Share Improve this question asked Mar 4 at 14:58 magfanmagfan 4653 silver badges10 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

The problem I am encountering is that inside a loop the contouring part with splot does not work.

Yes, but it will also not work without the loop. So, the loop is not the problem.

For some reason $Contours is not filled and therefore the plotting of the contours fails.

The reason is that your setting for set dgrid3d 50,50 gauss <dx>,<dy> is not suitable for your data, it has to be in the order of magnitude of your x,y scale.

From help dgrid3d:

The parameters dx and dy make it possible to control the radius over which data points contribute to a grid point IN THE UNITS OF THE DATA ITSELF.

So, more suitable for your data would maybe something like:

set dgrid3d 50,50 gauss 100,100

In order to illustrate, check the following example. If you change it to set dgrid3d 50,50 gauss 0.1 you will see your original problem.

Although, I guess with your provided data it might be difficult to get a "nice" contour plot.

Script:

### contour plot, importance of suitable dgrid3d parameters
reset session

# generate some random test data
set table $Data
    plot '+' u (x=rand(0)*3000-1500):(y=rand(0)*4000-2000):(x**2 + y**2) w table
unset table

set table $Contours
    set dgrid3d 50, 50 gauss 100,100
    set contour surface
    set cntrparam levels 10
    splot $Data u 1:2:3 nosurface
    unset dgrid3d
    unset contour
unset table

set key noautotitle
set grid x,y

plot  $Data     u 1:2:3 w p pt 7 lc palette z, \
      $Contours u 1:2:3 w l lc palette 
### end of script

Result:

本文标签: gnuplotHow to loop over multiple columns of a table to create contour plots for each columnStack Overflow