3

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

7
  • I think you need to provide more information about the files. Have you been able to plot the data from either of them with gnuplot? Commented Apr 14, 2020 at 14:27
  • I am new to GNU plot. Yes I am able to plot the data from .csv and .dat separately. But I do not how to plot them on the same graph. Commented Apr 14, 2020 at 14:34
  • Maybe you can add to your question the plot commands you run to plot them separately. Then also include a description of what you would like the plot look like when they're plotted together. Commented Apr 14, 2020 at 14:36
  • Did you try plot "test1.csv" using 1:2, "test.dat" using 1:2 w lines ? Commented Apr 14, 2020 at 15:05
  • Yes. But the data from "test1.csv" is not plotted as this requires set datafile separator ',' Whereas if I use set datafile separator ',' data from "test.dat" is not plotted. Commented Apr 14, 2020 at 15:14

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

plot "test.dat", "test.csv" is plotting something, but not correctly (gnuplot 5.2).
Thanks. Since I have rarely csv files, I will turn it around: plot "test.dat", "test.csv" u 1:2 "%lf,%lf,%lf". Thus there is really no need to use set datafile separator! That should be the first answer.
1

You can specify multiple characters to set datafile separator. If the columns are separated by either a space or comma, use

set datafile separator " ,"
plot "test.dat", "test1.csv"

Note, that this works only if the columns are separated by a single space. Still, it might be useful to have a simple variant.

3 Comments

But if the fields in the comma-separated file contain whitespace internally, that won't work.
... and it works only if test.dat has exactly one space as column separator. Multiple spaces won't work.
@theozh Indeed, I wasn't aware of that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.