Gnuplot scripts

Visualization

Posted by JXLIU on July 24, 2024

2D plot

不同 block 数据连线

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
### combine lines of different subblocks into one line
reset session

FILE = "test.txt"

set table $Temp
    plot FILE u 1:2 w table
unset table

# 画出每个 block 的第 col 行数据
col=1
set print $Data
    N = |$Temp|
    do for [i=col:N:3] { 
        print $Temp[i]
    }
unset print
print $Data

set offsets 1,1,1,1

plot $Data u 1:2:0 w lp
### end of script

test.txt 为以下数据为例

1
2
3
4
5
6
7
8
9
10
11
1 1.1
2 1.2
3 1.3

2 2.2
3 2.3
4 2.4

3 3.3
4 3.4
5 3.5

上述脚本的输出为

1
2
3
4
gnuplot> load 'test.plt'
 1       1.1
 2       2.2
 3       3.3
<image src="https://raw.githubusercontent.com/jiaxing-liu/jiaxing-liu.github.io/master/_posts/images/test_gnuplot1.png" width=80%>

同时画多个文件对比的脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
### combine lines of different subblocks into one line
reset session

# Define the number of files, max=6
file_count = 2
FILE1="noPWSOx1_sawoff/rhois.txt"
FILE2="noPWSOx1/rhois.txt"
FILE3="noPWSOx3/rhois.txt"
FILE4="noPWSOx4/rhois.txt"
FILE5="noPWSOx5/rhois.txt"

# ????block??col???
row=50
# the column numbers of two physical variables
col1=9
col2=1

# Initialize an empty list to hold file names
file_names = ""
if (file_count >=1) file_names = file_names . FILE1;
if (file_count >=2) file_names = file_names . " " .FILE2
if (file_count >=3) file_names = file_names . " " .FILE3
if (file_count >=4) file_names = file_names . " " .FILE4
if (file_count >=5) file_names = file_names . " " .FILE5
if (file_count >=6) file_names = file_names . " " .FILE6

# Split the file names into an array
file_list = word(file_names, 1)
do for [i=2:words(file_names)] {
    file_list = file_list . " " . word(file_names, i)
}
# Initialize the plot command
plot_cmd = "plot "

# Process each file and add to the plot command
do for [i=1:words(file_list)] {
    file = word(file_list, i)
    set table $Temp
        plot file u col1:col2 w table
    unset table

    if (i==1) {set print $Data1;}
    if (i==2) {set print $Data2;}
    if (i==3) {set print $Data3;}
    if (i==4) {set print $Data4;}
    if (i==5) {set print $Data5;}
    if (i==6) {set print $Data6;}
        N = |$Temp|
        do for [j=row:N:55] { 
            print $Temp[j]." ".$Temp[j-1]
        }
    unset print

    # Add data to plot command
    if (i==1) {plot_cmd = plot_cmd . "'$Data1' u 1:2 w l title 'File".i."', "}
    if (i==2) {plot_cmd = plot_cmd . "'$Data2' u 1:2 w l title 'File".i."', "}
    if (i==3) {plot_cmd = plot_cmd . "'$Data3' u 1:2 w l title 'File".i."', "}
    if (i==4) {plot_cmd = plot_cmd . "'$Data4' u 1:2 w l title 'File".i."', "}
    if (i==5) {plot_cmd = plot_cmd . "'$Data5' u 1:2 w l title 'File".i."', "}
    if (i==6) {plot_cmd = plot_cmd . "'$Data6' u 1:($4-$2) w l title 'File".i."', "}
}

# Remove the last comma and space
plot_cmd = substr(plot_cmd, 1, strlen(plot_cmd)-2)
print plot_cmd

# Execute the plot command
eval(plot_cmd)

### end of script