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?
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.
5.9 Does gnuplot support multiple y-axes on a single plot? Yes.
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.
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.
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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With