gnuplot vector arrow length and streamlines - vector

I have already asked about vector fields in here. Now I want to know a bit more about it.
How can I make it so that each arrow has the same fixed length and define the magnitude of the value by color?
And is it still not possible to plot streamlines in gnuplot? If possible, how can I do that?
For now I have this and need to upgrade it.
set term pngcairo
set title 'Navier-Stokes Equation'
set terminal png size 1280,720
set output 'vec.png'
plot 'vec' u 1:2:($3/$5):($4/$5) w vec t 'Vector Field'
UPDATE
Thanks to #theozh I've got what I wanted. I want to share my result as it could be useful for someone else.
Now I use these instructions to plot my vector field.
reset session
set size square
set palette rgb 33, 15, 10
set term pngcairo
set title 'Navier-Stokes Equation'
set terminal png size 1280, 720
set output 'vec.png'
plot 'vec.dat' u 1:2:(0.08*$3):(0.08*$4):(sqrt($3**2+$4**2)) w vec lw 2 lc palette notitle

About the same length: simply normalize your vectors.
About the color:
you can add a "column" and the end. The last column will define the color according to a palette.
I don't know about streamlines (what exactly they are and how to possibly realize them).
With the example script:
Script: (Edit: define function for length L() to make plot command shorter and clearer.)
### plot with normalized/scaled vectors
reset session
set size square
set samples 25
set palette rgb 33,13,10
set key noautotitle
Scale = 0.5
L(colX,colY) = sqrt(column(colX)**2+column(colY)**2)
plot [-5:5] '++' u 1:2:(Scale*$1/L(1,2)):(Scale*$2/L(1,2)):(L(1,2)) w vec lc palette
### end of script

Related

How to create streamline like arrow lines in Gnuplot?

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):

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].

how histogram in Gnuplot works

I try to reproduce a simple histogram with Gnuplot with the simple macro:
reset
n=9 #number of intervals
width=1 #interval width
hist(x,width)=width*floor(x/width)
set terminal pngcairo size 800,500 enhanced font 'Verdana,14'
set output "test.png"
set boxwidth width
set style fill transparent solid 0.5 border #fillstyle
set xrange [*:*]
set yrange [0:2.]
set xlabel "x"
set ylabel "Freq."
plot "const.dat" u (hist($1,width)) smooth freq w boxes lc rgb "orange" notitle
whit the follow data:
1.1
1.1
1.1
1.1
1.1
1.1
1.1
1.1
Now I like to understand how the hist(x,width) works in the sense:
hist(x,width)=width*floor(x/width)
works with every numbers taking the width=1 and then:
hist(1.1,1)=1*floor(1.1/1)=1
and so on, right?
Now (hist($1,width)) take all the elements in the columns and applay the hist function to everyone.
And I can be able to make the follow plot with the macro above:!
Question:
If I use (hist($1,width)):(1.0) I Don't understand whit the plots change as all the elements stay in one single boxes (from 0.5 to 1.5) ?
In the first case you specify only a single column in the using statement. Since you need at least two (x and y-value), the specified value (your hist(...)) is used as y-value and the row number as x-value. The statement smooth frequency works such, that it takes all points with the same x-value and sums up the corresponding y-values. In your first example you have no equal x-values since the row number is used.
In the second example, you use the hist(...) value as x-value, which is 1 for all rows. The y-value is 1.0. So you get a single box at x=1 and y=8 (the number of rows).

gnuplot heat map color range

I have some X Y Z data in a file and I'm using gnuplot to display it.
I am creating a heat map, ie. a 2D plot where the Z value is presented using color.
Right now I'm using the following script:
set palette defined (0 "blue", 1 "red")
plot "xyz.dat" u $1:$2:$3 w image
My problem is, gnuplot ignores the 0 and 1 in the palette definition. It uses the colors specified, but rescales according to the minimal and maximal Z value in the file.
This makes it hard to visually compare different plots whose z range is different.
How do I tell gnuplot to stop rescaling the z color range?
The "set palette defined" command maps the grey values that you would get if you were plotting using greyscale onto a color palette; the scaling to min -> 0 and max -> 1 is how it's supposed to work. If you want to make a set of plots all with the same scaling of the data, you want to use the "set cbrange" command. For example,
set cbrange [0:0.5]
set palette defined (0 "blue", 1 "red")
splot '++' using 1:2:(sin($1)*cos($2)) w image
gives you an image plot with the maximum data value of 0.5 mapped to red and 0 mapped to blue.
Subsequent plots, like
splot '++' using 1:2:(0.5*sin($1)*cos($2)) w image
will use the same scaling, so they can be compared.

3D Mapped Graph with Gnuplot Not accurate

I am encountering problems while trying to create a 3D (2D mapped) graph.
The data I am generating should create a 3 dimensional normal distribution bump, or, when "mapped", it should look like a flattened 3D graph, with color used as the third dimension
The script I am using to generate the mapped graph is the following:
#!/usr/bin/gnuplot
reset
#set terminal png
set term postscript eps enhanced
set size square
set xlabel "X position"
set ylabel "Y position"
#set zlabel "Synaptic Strength"
#Have a gradient of colors from blue (low) to red (high)
set pm3d map
set palette rgbformulae 22,13,-31
#set xrange [0:110]
#set yrange [0:80]
#set zrange [0:1]
set style line 1 lw 1
#set title "Title"
#Don't want a key
unset key
#set the number of samples
set dgrid3d 51,51
set hidden3d
splot DataFile u 1:2:3
when I run it on the following DataFile (http://www.sendspace.com/file/ppibyw)
I get the following output
The legend indicates a z-range of 0-0.03, however, the datafile has far larger z-values, such as 0.1. Obviously I can't publish a graph that is so inaccurate. Furthermore, I need a better graph in order to gain a better insight as to what is wrong with my simulation.
Does anyone know why gnuplot handles 3d mapped graphs like this? I suspect it has to do with the number, and nature, of the samples.
You problem is in the set dgrid3d 51,51
Have a look at what happens if you write set dgrid3d 51,102 (much better) or set dgrid3d 51,500 (much worse)
The point is that (from the help)
The grid is equally spaced in x
(rows) and in y (columns); the z
values are computed as weighted
averages or spline interpolations of
the scattered points' z values. In
other words, a regularly spaced grid
is created and the a smooth
approximation to the raw data is
evaluated for all grid points. Only
this approximation is plotted, but
not the raw data.
You could try and improve the approximation if you want see the help (?dgrid3d), but I would rather just plot the data straight. You can do this by ditching the dgrid3d command altogether. You will have to modify your data file so that there is a blank line when the x coordinate changes. For example
3.10000000000000142109 4.15692193816530508599 0.00004084299890679580
3.10000000000000142109 4.33012701892219364908 0.00001123746243460237
3.15000000000000124345 0.08660254037844386521 0.00000816290100763514
3.15000000000000124345 0.25980762113533162339 0.00001935936190868058
Then with this simplified script
set terminal png![enter image description here][1]
#set size square
set xlabel "X position"
set ylabel "Y position"
#uncomment the next command to eliminate the mysterious glitch around x=3.4
set yrange [0.1:4.5]
set pm3d map
set output "grid_merged.png"
splot "grid_merged2.dat" u 1:2:3
set output
set term pop
I get
which is better than you get with the interpolated plot. I'm not sure what causes the glitch aroung 3.4, its not there on other (non-mapped) views - altering the yrange eliminates it - although I'm not sure it changing the y-range is cheating in terms of your simulation results....

Resources