Plot points linked with edges using gnuplot - vector

I have a file of points (x, y) that I plot using gnuplot. If I have another file that shows which point is linked with which other point by an edge (e.g. (3.8, 6) linked to (4,7)), is it possible to visualise/plot this edges between points ?

depending on how your data is organized, you may want to look into plotting with vectors.
For example, if your datafile looks like:
#x1 y1 x2 y2
1 1 3 3
You can plot this using:
set style arrow 1 nohead
plot "my_arrows.dat" using 1:2:($3-$1):($4-$2) with vectors arrowstyle 1
EDIT
Assuming all the points in your datafile are repeated, you can do the following:
set style arrow 1 nohead
plot "my_arrows.dat" using 1:2:($3-$1):($4-$2) with vectors arrowstyle 1,\
"my_arrows.dat" using 1:2 w points
If they're not repeated, you can do:
set style arrow 1 nohead
plot "my_arrows.dat" using 1:2:($3-$1):($4-$2) with vectors arrowstyle 1,\
"my_arrows.dat" using 1:2 w points ls 1 lc rgb "red" pt 1,\
"my_arrows.dat" using 3:4 w points ls 1 lc rgb "red" pt 1
Note that you can play around with the linestyles (linecolor or lc, pointtype or pt, linewidth or lw etc. to make the points appear the same.)

You might not be able to read the line positions in without using a separate utility to generate your plotscript, but the command to draw a line from point to point is
set arrow [X] from first x1,y1 to first x2,y2 nohead
where X is an optional tag number for the arrow, and (x1,y1) and (x2,y2) are points in the graph's coordinate system.

Related

How to draw 3d vectors in gnuplot?

I want to draw several vectors in a 3D plot. The co-ordinates of the start and end point are columns in a data file. In 2D, I plot vectors by:
plot 'data.dat' u 1:2:($3-$1):($4-$2) with vectors
How can I do it in 3D?
Edit
I am trying to draw these vectors on top of a surface. When I try this:
set hidden3d
set xlabel "x"
set ylabel "y"
set zlabel "z"
set dgrid3d 10,10 qnorm 2
file = "surface.dat"
splot file u 1:2:3 with lines, file 1:2:3:($4-$1):($5-$2):($6-$3) with vectors
I get the surface, but not the vectors. (The vectors are out of the plane of the surface, like normals.)
Have you done any search at all? Have you checked the gnuplot homepage or gnuplot documentation?
From help vectors:
`The 2D vectors style draws a vector from (x,y) to (x+xdelta,y+ydelta). The 3D vectors style is similar, but requires six columns of basic data. In both cases, an additional input column (5th in 2D, 7th in 3D) may be used to provide variable (per-datapoint) color information. (see linecolor and rgbcolor variable). A small arrowhead is drawn at the end of each vector.
4 columns: x y xdelta ydelta
6 columns: x y z xdelta ydelta zdelta
... and further down...
Example:
plot 'file.dat' using 1:2:3:4 with vectors head filled lt 2
splot 'file.dat' using 1:2:3:(1):(1):(1) with vectors filled head lw 2
Hence, depending on your columns:
splot 'data.dat' u 1:2:3:($4-$1):($5-$2):($6-$3) with vectors

gnuplot - using line color as third variable does not generate the required output

Using gnuplot, I am trying to make a 2D plot with points where the point color is represented by the third column of a data file(file has 3 columns)
Here is the link to the file
I am using the following command to generate the graph:
pl "outPhaseDiff_b1_dScan.dat" u 1:2:3 w p pt 7 ps 2 lc variable
The desired output should contain 5 colors but it is only plotting 2 colors, which is really strange because I have been using this command for a long time and did not encounter such issue before. I guess it has to do something with the plotting algorithm but I have no clue.
Check your data, it contains many line pairs with the following pattern:
0.0000 0.0060 3
0.0000 0.0060 5
One line with x, y, color1, another line with identical x and y, but different color2. So the points from the second line hide the points from the first one.
If you plot it 3d with several layers, it looks like this:
z = 0
y = 0
splot "outPhaseDiff_b1_dScan.dat" \
u 1:2:($2 == y ? (z = z+1) : (z = 0, y=$2), z):3 \
w p pt 7 ps 2 lc variable
A 2d plot looks from top, only two colors are visible.

Lines Between Datapoints of different columns

I have these example-data saved in test.txt
1 2
2 3
3 5
4 5
5 6
I can easily plot them by
plot "test.txt" u 0:1,"" u 0:2
the result is
just the data plotted
but what i want is some lines between the datapoints to illustrate the difference and which points belong to the same x-value. (photoshopped scribble below)
same image with lines between datapoints
Is there any way i could achieve this with gnuplot?
Use the vectors style
plot "test.txt" u 0:1,"" u 0:2, "" u 0:1:(0):($2-$1) with vectors nohead
The vectors style expects 4 values which are the x and y coordinates, the x change (0 in this case), and the y change (the difference between your 2 columns). The nohead option removes the arrow head that is normally added.
You can style the vector using most line style specifications, and if necessary, can adjust coordinates to add some spacing. For example, setting the lines black and padding the lines by 0.1 in both directions by
plot "test.txt" u 0:1, "" u 0:2, "" u 0:($1+0.1):(0):($2-$1-0.2) with vectors nohead lc "black"
gives
I manually set ranges with set xrange and set yrange to match your images, as mine was using slightly different defaults.
See help vectors for more information on the vectors style.

Gnuplot 3D - plot scatter points and a surface *and* lines between the them

I've been unable to find a way of producing the plot described in the title, does anyone know how to do this if it's possible? I'd like to plot a surface from a function, and plot points from a file, and have lines drawn between the points and the surface. The link below is an image that contains an example of what I'd like to do, taken from a stackoverflow question for an identical task (but for a different plotting program).
You can do the calculations inside gnuplot's using statement. For the lines I use the vectors plotting style, and with arrowstyle variable (or linecolor variable) you can select different colors depending on the dz value:
set style arrow 1 linecolor rgb 'red' nohead
set style arrow 2 linecolor rgb 'green' nohead
splot f(x,y) with lines, \
'points.dat' using 1:2:(f($1,$2)):(0):(0):(dz=$3-f($1,$2)):(dz < 0 ? 1 : 2) with vectors arrowstyle variable,\
'' using 1:2:3 with points pt 7 ps 5
I'm not sure, which this gives problems with gnuplot 4.6. For your case you can also use linecolor variable or linecolor rgb variable to change the color of the vertical lines:
splot f(x,y) with lines, \
'points.dat' using 1:2:(f($1,$2)):(0):(0):(dz=$3-f($1,$2)):(dz < 0 ? 0xff0000 : 0x00ff00) with vectors nohead linecolor rgb variable lw 5,\
'' using 1:2:3 with points pt 7 ps 5

drawing a circle around many points with different radius

I can draw a circle around a given point (x,y) with a command like flowing (here for example point is at (X=5,Y=7) and the circle have a radius R=2.73):
set object 1 circle at 5,7 size first 2.73 fc rgb "navy"
Now, if I have many points (in a txt file where each line is "x y") and I want to draw a circle around each point with different specified radius. Should I repeat the command "set object i circle at Xi,Yi size first Ri fc rgb "navy"" for each point i ?!
It looks like you may want to use the with circles option. If you have a data file with three columns (x y radius), the following command will plot circles with radii from the file at each point:
plot 'datafile' u 1:2:3 with circles
http://gnuplot.sourceforge.net/demo/circles.html
You can create a loop in gnuplot for multiple circles.
do for[k=1:20]{
set object k circle front at k*3,0 size 1 fillcolor rgb "black" lw 1
}
This will create 20 circles at x points (3,6,9...) of radius 1. Please keep in mind to set the object count with the loop variable too ("object k").

Resources