How to draw 3d vectors in gnuplot? - plot

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

Related

Size of parametric plane in gnuplot

I want to plot a series of points, a plane and the intersection points with that plane using gnuplot. In order to do this, I created the following gnuplot script:
set title 'Homogeneous field with plane intersection'
set xlabel 'x'
set ylabel 'y'
set zlabel 'z'
set parametric
splot 'points.txt' with linespoints, 'intersectionPoints.txt' with points
pointtype 7 lc rgb 'red',
60 + 0 * u, u, v
The script produces the following plot:
Everything works fine, however the size of the parametric plane is way too small. How can I get gnuplot to automatically adjust the size of the plane to roughly match the dimensions of the rest of the plot?
You have to set the ranges for the two parameters u and v, for example
set urange [-50:250]
set vrange [-20:20]
From help parametric: " Currently the default range for these parametric variables is [-5:5]. Setting the ranges to something more meaningful is expected.".

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 line plot from data points, z interpolated color?

I have a data file with x, y, and z datums--basically x,y locations with
z representing attenuation at that location.
The answer to a question like Line plot in GnuPlot where line color is a third column in my data file? using
palette defined (with palette z) is very
close, except that each line segment is set to a single color along its length.
Is there a
way to have the Z value interpolated (linear is fine) along each segment, so
the attenuation values are a smooth gradient rather than jumping values
at each segment boundary?
You can use set dgrid3d to interpolate a given data set. Consider the data file test.dat with the content
1 2 1
2 3 2
1 1 2
Plot this with
set dgrid3d 30,30 splines
set ticslevel 0
set hidden3d
splot 'test.dat' matrix w l lc palette lw 3
to get
If this works also in your case depends on several other factors, like number of data points or if you don't want to create a new grid, but retain the original grid, and only smooth the colors. In the latter case you must write an external script to prepare your data in an appropriate way.

gnuplot: How to plot with variable linewidth?

I have some issues with a plot i would like to make.
Let say I have a data file with 5 columns like: x,y,x+dx,y+dy, a .
I would like to plot a vector field with a arrow thickness proportional to a, but I don't know how to do it. Suppose I scale a in a way that it belongs to [0:100], should I have to define a linetype for each interval [0:5],[5:10] etc... ?
I have tried with column function, but it is not working.
c1=12
plot 'data' u 3:4:($5-$3):($6-$4) w vectors lw column(c1)
(Note: the a term is in the twelve column)
And i tried this command:
plot 'data' u 3:4:($5-$3):($6-$4):12 w vectors nohead arrowstyle variable
Your last approach with the variable arrow style should work fine. Consider the following example:
set samples 11
set xrange [0:100]
set for [i=1:101] style arrow i lw i/10.0 nohead
unset key
plot '+' using 1:1:(2):(10):($1+1) with vectors arrowstyle variable
Here I defined 100 arrow styles which differ only in their linewidth. The result with version 4.6.5 is
In your case it should be enough to use
set for [i=1:101] style arrow i lw i/10.0 nohead
plot 'data' u 3:4:($5-$3):($6-$4):12 w vectors nohead arrowstyle variable
Of course you must make sure, that column 12 is in the range [1:101]. You could also use stats to determine the limits of the values in column 12 and write a function map(x) which maps the values of this column to the required range [1:101].

Plot points linked with edges using gnuplot

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.

Resources