How to plot several datasets with titles from one file in Gnuplot?

前端 未结 5 1840
心在旅途
心在旅途 2020-12-04 18:28

Assuming I have a file that looks like this:

\"p = 0.1\"
1 1
3 3
4 1


\"p = 0.2\"
1 3
2 2
5 2

Is it possible to make Gnuplot plot these tw

相关标签:
5条回答
  • 2020-12-04 18:35

    It's definitely possible and your datafile is already the correct format. The functionality you're looking for is built into columnheader(N) which reads the data at the top of the N'th column and uses it as the plot title:

     plot 'test.dat' i 0 u 1:2 w lines title columnheader(1),\
          'test.dat' i 1 u 1:2 w lines title columnheader(1)
    

    which can be condensed using iteration:

    plot for [IDX=0:1] 'test.dat' i IDX u 1:2 w lines title columnheader(1)
    
    0 讨论(0)
  • 2020-12-04 18:35

    gnuplot 5.1 (2016/08/28)

    This is similar to https://stackoverflow.com/a/29495496/895245 but with some fixes for later versions.

    https://stackoverflow.com/a/43819870/895245 taught me the for [i=0:*] syntax which dispenses stats and is therefore a bit nicer.

    Script:

    datafile = 'test.dat'
    stats datafile nooutput
    plot for [IDX=0:STATS_blocks-1] \
        datafile \
        index IDX \
        using 1:2 \
        with lines \
        title columnheader(1)
    

    Test data:

    a
    1, 1
    2, 2
    3, 3
    
    
    "b"
    1, 1
    2, 4
    3, 9
    
    
    "c, c"
    1, 1
    2, 8
    3, 27
    

    Output:

    This works on gnuplot 2016/08/28 which will later become gnuplot 5.1, but not in gnuplot 5.0.3 (Ubuntu 16.04), because in 5.0.3 the stats command gives an error because the column headers are not valid data. And on 2016/08/28 it became just a warning.

    I've asked how to remove the warning at: https://groups.google.com/forum/#!topic/comp.graphics.apps.gnuplot/Pi4aBE2PwZ8

    Using comments like:

    #a
    1, 1
    2, 2
    3, 3
    

    did not work in either version I've tested, it is just ignored.

    0 讨论(0)
  • 2020-12-04 18:40

    A solution based on answers given by andyras (answer 1 and answer 2) to automatize all the process is to use:

    datafile = 'test.dat'
    stats datafile
    plot for [IDX=1:STATS_blocks] datafile index (IDX-1) u 1:2 w lines t\ 
    columnheader(1) lc variable
    

    With this, the script detects automatically the number of data blocks and it plots with different colors and with the corresponding title defined in the first line of each data block.

    0 讨论(0)
  • 2020-12-04 18:46

    This is Bruce_Warrior's and Ciro Santilli's answers but without the intermediate stats:

    # plot.gpi
    datafile = ARG1
    plot for [i=0:*] datafile index i using 1:2\
    with lines title columnheader(1)
    

    The for loop can iterate over all datasets in a file directly. It works in gnuplot 5.0.5 but I'm not sure when for acquired this capability. It is documented in the 5.0 manual but not the 4.6 manual.

    Unless the line color should be determined by a third input column consumed by linecolor variable (per Bruce's answer), gnuplot will assign different colors and line styles automatically. In this specific case using 1:2 can also be omitted.

    $ gnuplot --version
    gnuplot 5.0 patchlevel 5
    $ gnuplot --persist -c plot.gpi test.dat
    

    test.dat is

    "p = 0.1"
    1 1
    3 3
    4 1
    
    
    "p = 0.2"
    1 3
    2 2
    5 2
    
    0 讨论(0)
  • 2020-12-04 18:55

    With a slight modification of your data set (so that the titles are given as comments):

    #"p = 0.1"
    1 1
    3 3
    4 1
    
    
    #"p = 0.2"
    1 3
    2 2
    5 2
    

    You can plot these two data sets as separate lines like this:

    plot 'data.dat' i 0 t "p = 0.1", '' i 1 t "p = 0.2"
    

    The index (i for short) option to the plot command tells gnuplot to plot the ith data set. I can't find a way to get gnuplot to get the titles automatically from the header which is why I specified them manually with the title (t for short) option.

    0 讨论(0)
提交回复
热议问题