3D vector field in gnuplot with specified colors - gnuplot

I would like to plot a 3D vector field in gnuplot where every vector is a specified color. My input file looks like this:
x
y
z
dx
dy
dz
color
x1
y1
z1
dx1
dy1
dz1
color1 (hex code)
and so forth. I tried the following code, piggybacking off the solution*
scale = 1
splot 'file.dat' u 1:2:3:($4*scale):($5*scale):($6*scale):7 w vectors arrowstyle 1 lc rgb var
but this gave me a color bar on the side of the graph. Also, every vector was colored gray, instead of the color in the input file. Could someone suggest a fix for this?
* Vector field 3D plot in gnuplot with contour of vectors (bottom)

Your command is very nearly correct. The problem is that apparently the line color is considered part of the arrow style, so the program produces an error message duplicated or contradicting arguments in plot options if you give both an arrow style and a line color. If you move the lc rgb variable to the style definition, it is accepted. This works for me:
set style arrow 1 filled head lc rgb variable
plot DATA using 1:2:3:4:5:6:7 with vectors arrowstyle 1

Related

Gnuplot: fill area bounded by curves left/right?

I have a dataset that defines two curves, and I want to fill the area between them. However, contrary to the standard situation, the abscissa is to be plotted on the vertical axis and the ordinates on the horizontal one; the abscissa indicates depth, this is a common plotting format in geophysics. In other words, I want something like
plot 's.dat' u 1:2:3 w filledcurves
but with swapped axes so that the filled area is bounded not at the top and bottom but to the left and right by the curves as seen in
plot 's.dat' u 2:1,'s.dat' u 3:1
My dataset is like this:
0. -1.776 -0.880
160. -1.775 -0.882
160. -1.692 -0.799
320. -1.692 -0.800
320. -1.531 -0.634
480. -1.534 -0.637
480. -1.286 -0.394
Is this possible in Gnuplot?
Thomas
This is a totally different solution using 3D plot style "with zerror".
You will need current gnuplot (version 5.2) for this. The plot style was really not designed for this so there are some difficulties (e.g. x tic marks invisible because drawn perpendicular to the plane of the plot, all tic labels requiring an offset for readability).
#
# [mis]use 3D plot style "with zerror" to create a plot of the xz
# plane with area fill between two sets of data points with
# equal coordinates on the vertical axis (x) but contrasting
# values on the horizontal axis (z).
#
set view 270, 0
set view azimuth -90
set xyplane at 0
unset ytics
set ztics offset 4, -2 out
set xtics offset 4
splot 's.dat' using 1:(0):(0.5*($2+$3)):2:3 with zerror notitle
If there is some value of x which is guaranteed to lie between the two curves then you can plot in two halves. For the data you show, x=-1 would be a suitable value and the plot command would be:
plot 's.dat' u 2:1 with filledcurve x=-1 lt 3, \
's.dat' u 3:1 with filledcurve x=-1 lt 3
If the requirement for a constant intermediate x value can only be
satisfied piece-wise, e.g.
x=-1 for (0<y<500), x=0 for (500<y<1000)
then it may nevertheless be possible to construct a graph by stacking
the piecewise sections.
A simple way would be to define a closed line and fill it. For this, you take column 2 and add the reversed column 3. You probably need gnuplot >=5.2 for this.
Code:
### fill between vertical curves
reset session
$Data <<EOD
0. -1.776 -0.880
160. -1.775 -0.882
160. -1.692 -0.799
320. -1.692 -0.800
320. -1.531 -0.634
480. -1.534 -0.637
480. -1.286 -0.394
EOD
set print $Outline
do for [i=1:|$Data|] {
print sprintf("%s %s", word($Data[i],2), word($Data[i],1))
}
do for [i=|$Data|:1:-1] {
print sprintf("%s %s", word($Data[i],3), word($Data[i],1))
}
set print
plot $Outline w filledcurve lc rgb "green"
### end of code
Result:

gnuplot fill area under curve alternatively

I am having some difficulty generating a plot of a data set that is oscillating between negative and positive values (line a sin or cos). My goal is to fill the area under the curve with alternating colour: negative region with blue and positive with red. To be more precise I want to fill the area between the curve and the x axis. So far i managed to plot the curve with alternating colours (blue for negative, red for positive) using:
set palette model RGB defined ( 0 'red', 1 'blue' )
unset colorbox
plot 'data.set' u 1:2:( $2 < 0.0 ? 1 : 0 ) w lines lt 1 lw 4 palette
Unfortunately if I replace w lines with filledcurves I don't get an alternate fill. How can one accomplish this?
Cheers
If I understood the question correctly, you can try this:
plot '+' using 1:(0):(sin($1)) w filledc below, \
'+' using 1:(0):(sin($1)) w filledc above
which is telling gnuplot to fill the area between two curves (sin(x) and 0), using the above and below positions. There is another solution as well:
plot '+' using 1:(sin($1) > 0 ? sin($1):0) w filledcurves y1, \
'+' using 1:(sin($1) < 0 ? sin($1):0) w filledcurves y2
and the result would be:
The important part refers to the options part of filledcurves. See more details here and here.

Gnuplot histogram gap does nothing

I have a gnuplot script which plots a histogram. I used the following syntax:
set style data histogram
set style histogram cluster gap 2
set style fill solid
set logscale y
rgb(r,g,b) = int(r)*65536 + int(g)*256 + int(b)
plot 'histogram_data' using (column(0)):2:(0.5):(rgb($3,$4,$5)):xticlabels(1) w boxes notitle lc rgb variable
What the last line does is: using column 1 as x labels, column 2 as the height of the histogram bars, 0.5 as box width, and columns 3, 4 and 5 as the rgb values to colour the bars.
Now, the problem is that modifying the gap parameter in line 2 does not change in any way the spacing between bars, even though as far as I understand that is the correct way to adjust such spacing. I am using gnuplot 4.6 patchlevel 4.
I found a way to do this with boxes, though I do not consider it very clean:
plot 'histogram_data' u (column(0)*2+1):2 w boxes notitle lc rgb 'white',\
'histogram_data' u (column(0)*2):2:(rgb($3,$4,$5)):xticlabels(1) w boxes notitle lc rgb variable;
This command is plotting all the data of the main plot on even slots and a white box on odd slots. So the first line in the plot command is plotting the gaps between every box of the plot (the width of these gaps can be specified using the boxwidth property I think but I haven't tested this), while the second line is drawing the actual plot.
I could not find a way to do this with the histogram plotting style, keeping the variable colours specified in the data file.

How do I use gnuplot to plot a simple 2d vector arrow?

This is my first time trying to use gnuplot, and I can't find any instructions on how to accomplish this. The closest I found was this:
http://gnuplot.sourceforge.net/docs_4.2/node259.html
plot 'file.dat' using 1:2:3:4 with vectors head filled lt 2
but I can't find any explanation about "file.dat".
So can somebody give a simple example of how to draw a simple 2d vector arrow? Thanks.
gnuplot has a very good help/documentation build in. Just type help plot or help vector to learn more on how to plot vectors in gnuplot.
The 2D vectors style draws a vector from (x,y) to (x+xdelta,y+ydelta).
A small arrowhead is drawn at the end of each vector.
4 columns: x y xdelta ydelta
This means, your input file should have 4 columns, where the first two columns define the starting (x,y)-position of the vector/arrow and the last two its (x,y) direction:
# file.dat
0 0 .5 .5
0 1 -.5 .5
1 1 1 0
Now typing the following command
plot "file.dat" using 1:2:3:4 with vectors filled head lw 3
gives the following output:
Drawing vectors with the set arrow command
Consider using the set arrow command, if you only need to draw a few vectors/arrows (e.g. to highlight some points in the plot).
set arrow 1 from pi/2,1 to pi/2,0
set arrow 2 from pi*3/2,-1 to pi*3/2,0
plot[0:2*pi] sin(x)
You can create 'file.dat' in a spreadsheet save it as text and put it in the path of gnuplot by using the cd command to point gnuplot to its location. If that does not agree with you, look at the examples using '+' and '++' and '-' in the gnuplot manual. These are a "virtual data file." Note that the first two are for one and two column data points i.e. (x) or (x,y). You will have to use $1 and $2 as variables for calculating dx and dy. It is obligatory to set the xrange and yrange variables and the isosamples for density for this to work.
Something like....
set isosamples 30
set samples 30
set xrange [-10:10]
set yrange [-10:10]
plot '++' using 1:2:(0.1*sin($1)):(0.1*cos($2)) with vectors

Gnuplot transform x axis data with in the plot

I have data in this format
apple 1
bananna 3
ornage 5
fig 4
I want the x axis data to appear in the plot and not in the axis as such. Moreover they are words and not numbers.
How can I do it please?
The question is a bit unclear. Where do you want the labels to show up? Here's an example where I'll put the label "apple" at the point (0,1) and the label "banana" at the point (1,3), etc. (each label is moved 1 unit further down the x axis):
plot 'data.dat' using (column(0)):2:1 with labels
You can also put points associated with it:
plot 'data.dat' using (column(0)):2 w p,\
'' using (column(0)):2:1 w labels
Here the labels sit directly on top of the points. That may not be what you want -- You can add an offset:
plot 'test.dat' u (column(0)):2:1 w labels offset character 0,1,\
'' u (column(0)):2 w p
From the documentation (help labels), it appears that the appearance of your labels should be able to be modified by pretty much any option that you can pass to set label.
The font, color, rotation angle and other properties of the printed text
may be specified as additional command options (see `set label`).

Resources