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.
Related
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
I want to create a streamline like arrow lines in Gnuplot,I already have the data points that I needed, so I think my problem is not the same as this post says and different from this post because I have already obtain the data needed for stramlines.
What I have done is like this:
So the red lines are vectors show flow field and green line is streamlines to guide the readers the direction of the flux. And all the large blue arrows are my aim to be plotted in GNUPLOT. I have kown how to plot middle arrows as this post has shown but what code I need to do if I want to plot more arrows along the lines?
To be more detailed, How can I plot like this:
I supply my data file here :
velocity.txt is for vector flow field data as "index,X,Y,vx,vy,particle-numbers"
line.txt is for streamline data as "X,Y"
and My gnu file is bleow:
set terminal postscript eps size 108,16 enhanced font "Arial-Bold,100"
set output 'vector.eps'
unset key
set tics
set colorbox
set border 0
set xtics 2
#set xlabel 'x'
#set ylabel 'y'
set xrange [0:108]
set yrange [0:16]
#set cbrange [0:40]
set nolabel
set style line 4 lt 2 lc rgb "green" lw 2
plot 'velcoity.txt' u 2:3:(250*$4):(250*$5) with vectors lc 1,'line.txt' u 1:2 ls 4
Thank you!
To plot arrows along a line you can again use the vectors plotting style like you do already for the stream field.
But to get a proper plot you must consider several points:
Usually gnuplot limits the size of the arrow heads to a fraction of the arrow length. So, if you want to plot a continuous line with arrows heads, the arrows themselves should have a very short length. To avoid downscaling of the arrow heads, use the size ... fixed option, which is available only since version 5.0
You have only the trajectory, x and y values, of the line. To extract the arrow direction, the simplest approach would be to use the difference between two neighbouring points (or at a distance of two or three points).
You can extract these differences in the using statement. As pseudo code, one could do the following:
if rownumber modulo 10 == 0:
save x and y values
else if rownumber modulo 10 == 1:
draw arrow from previous point to current point, only with a head
else
ignore the point.
Putting this pseudo-code in the using statement gives the following:
ev = 10
avg = 1
sc = 0.1
plot 'line.txt' u (prev_x = (int($0)%ev == 0 ? $1 : prev_x), prev_y = (int($0)%ev == 0 ? $2 : prev_y), int($0)%ev == avg ? $1 : 1/0):2:(sc*(prev_x-$1)):(sc*(prev_y-$2)) w vectors backhead size 2,20,90 fixed ls 4
To make things more flexible, I introduced some variables: ev tells you the difference count between two arrows heads, avg the distance between two points used to calculate the arrow direction, and sc the length of the arrow shaft.
As further improvement you can use the length of the stream field arrows to colour the stream field vectors. This gives the following script
reset
unset key
set tics
set colorbox
set border 0
set xtics 2
set autoscale xfix
set autoscale yfix
set autoscale cbfix
set style line 4 lt 2 lc rgb "green" lw 2
ev=30
avg=3
sc=0.1
field_scale=500
plot 'velcoity.txt' u 2:3:(field_scale*$4):(field_scale*$5):(sqrt($4**2+$5**2)) with vectors size 1,15,45 noborder lc palette,\
'line.txt' u 1:2 ls 4 w l,\
'' u (prev_x = (int($0)%ev == 0 ? $1 : prev_x), prev_y = (int($0)%ev == 0 ? $2 : prev_y), int($0)%ev == avg ? $1 : 1/0):2:(sc*(prev_x-$1)):(sc*(prev_y-$2)) w vectors backhead size 2,20,90 fixed ls 4
With the result (qt terminal):
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].
I have used gnuplot succesfully to plot boxplots. But now I would like to stick to gnuplot for all my plotting needs and looking to do something that eg. Prism can do:
http://www.graphpad.com/support/faqid/132/
I only have two columns of data (before and after) and want all pairs to be joined up with a line. If anyone has any idea, it would be great.
That is not possible out of the box, so it requires some fiddling.
The xtics are set manually, 0 is the x-value of 'Before', 1 for 'After'. These numerical values must be used explicitely later in the plots.
The lines are plotted as arrows without heads. Using lc variable (i.e. linecolor variable), we can use the last column of the using statement to select the color from the respective line type.
The 'Before' points are plotted first. Unfortunately, there is no option pointtype variable, so I use the plot for iteration to assign each point a different pointtype (pt).
I use the stats command to determine the number of points to plot. To get the total count, I must sum up the records, which are the inside points, and the outofrange points, because the classification is done based on the first column's value, which conflict with the 'manual' xtics settings for the 'Before' and 'After' labels.
These are the main points. Of course, there are many other possibilities (using line styles etc.), but should be a good starting point.
The script is:
reset
file='beforeafter.txt'
set xtics ('Before' 0, 'After' 1)
set xrange [-0.2:1.2]
set offset 0,0,0.2,0.2
stats file nooutput
cnt = int(STATS_records+STATS_outofrange)
plot for [i=0:cnt-1] file using (0):1 every ::i::i with points lc i+1 pt (6+i) ps 2 t '',\
for [i=0:cnt-1] file using (1):2 every ::i::i with points lc i+1 pt (6+i) ps 2 t '',\
file using (0):1:(1):($2-$1):($0+1) with vectors nohead lc variable t ''
With the test data beforeafter.txt:
1 5.5
2 0.3
3 3
And you get the result:
Using line styles
Another variant uses line styles to set the color, line type, and point type. For the iterations you must use explicitely ls (i+1), whereas for the vectors the as variable (arrowstyle variable) is used. With the lc variable it is not possible to set different dash patterns for the arrows.
So here is a, in my opinion, much more readable and flexible variant:
reset
set termoption dashed
file='beforeafter.txt'
set xtics ('Before' 0, 'After' 1)
set xrange [-0.2:1.2]
set offset 0,0,0.2,0.2
stats file nooutput
cnt = int(STATS_records+STATS_outofrange)
set style line 1 lt 1 pt 5 ps 2 lw 2 lc rgb '#AE1100'
set style line 2 lt 2 pt 7 ps 2 lw 2 lc rgb '#6EB043'
set style line 3 lt 3 pt 9 ps 2 lw 2 lc rgb '#7777ff'
set for [i=1:3] style arrow i ls i nohead
unset key
plot file using (0):1:(1):($2-$1):($0+1) with vectors as variable,\
for [i=0:cnt-1] file using (0):1 every ::i::i with points ls (i+1),\
for [i=0:cnt-1] file using (1):2 every ::i::i with points ls (i+1)
With the result:
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.