Search code examples
gnuplot

Plotting data from .dat file and .csv file on a same graph using GNU plot


I have data files in two formats, one being .csv and the other being .dat. Is it possible to plot the data from these two files on the same graph?

For plotting the data from the .dat file I used the following command:

plot "test.dat" using 1:2 with lines

I intend to plot the data from .dat file using continuous line. This I am able to achieve.

And for plotting the data from the .csv file I used the following command:

set datafile separator ','
plot "test1.csv" using 1:2

I would like to plot the data from .csv file using dashed lines. i.e., something similar to this "- - - - - -"

A line of data from .dat file is

-8.14257e-01 2.04276e+00 0.00000e+00

and from .csv is

3.12487-03,1.58743-03


Solution

  • One thing you can do is replot.

    plot "test.dat" u 1:2 w lines
    set datafile separator ','
    replot "test.csv" u 1:2
    

    That will add the second line to your plot. Another thing you could do is to specify the input format.

    set datafile separator ','
    plot "test.dat" u 1:2 "%lf %lf %lf", "test.csv"
    

    Note on Gnuplot 5.2 I can plot both files without specifying anything.

    plot "test.dat", "test.csv"
    

    They both show up.

    The docs for the format specification (help using, page 98):

    Syntax: plot ’file’ using <entry> {:<entry> {:<entry> ...}} {’format’}

    If a format is specified, it is used to read in each datafile record using the C library ’scanf’ function. Otherwisethe record is interpreted as consisting of columns (fields) of data separated by whitespace (spaces and/ortabs), but see datafile separator.