Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

gnuplot

Assuming I have a file that looks like this (note the double newlines):

"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 two datasets in one plot with the titles given on the first line of each dataset?

like image 907
gTcV Avatar asked Oct 10 '12 12:10

gTcV


People also ask

How use gnuplot to plot data from a file?

To plot functions simply type: plot [function] at the gnuplot> prompt. Discrete data contained in a file can be displayed by specifying the name of the data file (enclosed in quotes) on the plot or splot command line. Data files should have the data arranged in columns of numbers.

Does gnuplot support multiple Y axes on a single plot?

5.9 Does gnuplot support multiple y-axes on a single plot? Yes.

What is gnuplot Splot?

splot is the command for drawing 3-d plots (well, actually projections on a 2-d surface, but you knew that). It can create a plot from functions or a data file in a manner very similar to the plot command. See plot (p. ) for features common to the plot (p. ) command; only differences are discussed in detail here.

How the plot generated by gnuplot may be saved in a file?

There are two ways to save your work in gnuplot: you can save the gnuplot commands used to generate a plot, so that you can regenerate the plot at a later time. Or you can export the graph to a file in a standard graphics file format, so that you can print it or include it in web pages, documents, or presentations.


2 Answers

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) 
like image 66
mgilson Avatar answered Oct 02 '22 19:10

mgilson


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 

Plot de

test.dat is

"p = 0.1" 1 1 3 3 4 1   "p = 0.2" 1 3 2 2 5 2 
like image 31
mkjeldsen Avatar answered Oct 02 '22 20:10

mkjeldsen