Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gnuplot is connecting every single point together

Tags:

gnuplot

I am plotting data in gnuplot like so:

set terminal pdf
set output "Planck.pdf"
plot "CalculatedValues.dat" u 1:2 t "Dawn" pt 1 ps .1 with lines

But my output ends up looking like a yarn sculpture,

enter image description here

I want the output to look like, but a line graph instead of a scatter plot.

enter image description here

What am I doing wrong?

Here is some data:

13.4904 3.13714e+07     3.91106e+07
11.3872 4.64475e+07     5.96647e+07
18.0928 1.40999e+07     1.69117e+07
13.3284 3.23223e+07     4.03737e+07
1.3264  3309.46 24012.2
0.323113        5.16869e-25     1.764e-21
10.6252 5.35423e+07     6.97629e+07

It's tab separated and new line separated

like image 329
Cam Avatar asked Apr 11 '16 23:04

Cam


People also ask

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.

Is gnuplot suitable for scripting?

1.4 Is gnuplot suitable for scripting? Yes. Gnuplot can read in files containing additional commands during an interactive session, or it can be run in batch mode by piping a pre-existing file or a stream of commands to stdin.

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.


1 Answers

The problem is with the order of your points. Gnuplot will connect consecutive points in the file with lines. If the points are in order based on the x-axis value (the first column in this case), you will get what you are after. If they are not, you will get strange results like you are seeing.

Unsorted Data

13.4904   3.13714e+07   3.91106e+07
11.3872   4.64475e+07   5.96647e+07
18.0928   1.40999e+07   1.69117e+07
13.3284   3.23223e+07   4.03737e+07
1.3264    3309.46       24012.2
0.323113  5.16869e-25   1.764e-21
10.6252   5.35423e+07   6.97629e+07

plot datafile u 1:2 w linespoints pt 7 produces the following

enter image description here

Here the points are numbered to show the order they are plotted in. We can see that points that occur consecutively in the datafile are connected.

Sorted Data

0.323113   5.16869e-25   1.764e-21
1.3264     3309.46       24012.2
10.6252    5.35423e+07   6.97629e+07
11.3872    4.64475e+07   5.96647e+07
13.3284    3.23223e+07   4.03737e+07
13.4904    3.13714e+07   3.91106e+07
18.0928    1.40999e+07   1.69117e+07

plot datafile u 1:2 w linespoints pt 7 produces the following

enter image description here

Here we see that the same points are plotted, but in a different order. Again, consecutive points are connected, but are done in an increasing order, as the data is sorted in such a matter.


The solution is to sort your data first. Either have the program generating the data sort it during production, or use an external program to sort it before plotting.

If the values in the first column are unique, one of the smoothing options can be used with the original unsorted data. For example,

plot datafile u 1:2 smooth unique w linespoints pt 7

will produce the same results as plotting with the sorted data. This is because the smooth unique option first sorts the data by the x column. The y values are replaced with the average of all y values with the corresponding x value. If the x values are unique, this means that the original data is preserved, just in sorted order.


The plot command shown will only draw the lines. To get the numerical labels as well, we use

plot datafile u 1:2 w linespoints pt 7, \
           "" u 1:2:(sprintf("%d",$0+1)) w labels offset 0,graph 0.05

which plots a label at each point coordinate moved upward by 5% of the graph range (using the graph coordinate system). As the 0 pseudocolumn (the line number) is 0-based, we add one in order to generate the label, which we will give 1-based labels.

If we wanted to label the points here, we would have to use set table to capture the smoothed data, and then plot data. Doing

set table "tempfile"
plot datafile u 1:2 smooth unique
unset table
plot "tempfile" u 1:2 w linespoints pt 7, \
             "" u 1:2:(sprintf("%d",$0+1)) w labels offset 0,graph 0.05

will produce exactly the sorted graph above. The data is captured to a temporary file and the first plot command just generates the points. The following plot command plots the now sorted data along with the labels. This allows gnuplot to do all of the sorting itself, but again, this will only work if the values in the first column are unique.

like image 197
Matthew Avatar answered Sep 19 '22 00:09

Matthew