drawing a circle around many points with different radius - plot

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").

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 erases circles when zoomed in

I am trying to plot a file of data along with 2 circles with gnuplot. The data file is here and the circle file is here. The circles are quite big compared to the dataset, and when I plot it on a large scale everything works fine. But when I try to zoom in, the circles disappear. Here are the gnuplot commands I use (v5.4.2):
plot 'data.txt' us (log($2)):(log($3)) w lp, 'circ.txt' us 1:2:3 w circles
Here is the result (looks ok):
Now if I try to zoom in a little (still ok):
set xrange [*:55]
replot
Trying to zoom in more (circles disappear!)
set xrange [*:8]
replot
Is this a bug in gnuplot, or is there a way to fix this and visualize the circles on a zoomed in view?
As #Ethan already explained, with the plotting style with circles if the center of a circle is not in the graph range anymore, the circle will not be plotted.
You can workaround this if you plot the circles yourself. Then you can zoom in as long as a datapoint of the circumference is still in the graph. For large zoom-in you could increase the number of samples, e.g. set samples 500.
However, in order to have the circle visually appear as a circle (not as an ellipse) you have to set size ratio -1 which will change the graph size.
Code:
### plot circles which "allow" zoom-in
reset session
set size ratio -1
$Circles <<EOD
50 0 50
100 0 100
220 0 220
EOD
CircX(x0,y0,r,t) = x0+r*cos(2*pi*t)
CircY(x0,y0,r,t) = y0+r*sin(2*pi*t)
x0(n) = word($Circles[n],1)
y0(n) = word($Circles[n],2)
r(n) = word($Circles[n],3)
set key noautotitle
set samples 100
set yrange[-100:100]
plot for [i=1:|$Circles|] [0:1] '+' u \
(CircX(x0(i),y0(i),r(i),$1)):(CircY(x0(i),y0(i),r(i),$1)) w l
### end of code
Result: (original plot)
(zoomed-in):
Gnuplot does not draw points that are out of range. You can think of a circle as a point defined by its center and radius. As soon as the center goes out of range, in this case xmin < xmax < center, the circle is no longer drawn.
Another solution using 'set object circle'
There is a way to display all circles defined in 'circ.txt' as objects using set object circle.
set table $DATA
plot "circ.txt" using 1:2:3 with table
unset table
do for [k=1:|$DATA|] {
line = $DATA[k]
xpos = word(line, 1)
ypos = word(line, 2)
r = word(line, 3)
set object circle at xpos, ypos radius r fill empty border lc black
}
set xrange [*:30]
set yrange [*:16]
plot "data.txt" using (log($2)):(log($3)) with lp

gnuplot: how to set the size of the points according to the grid

I have the following 'data.dat' file:
# x y z radius
-1.64905083 -1.14142799 -2657.88232 177.358566
-449.735321 416.586914 -2865.25366 10.0000000
178.955292 -256.291138 -2856.96069 89.9588394
-336.942322 184.932343 -2839.22876 90.6131058
-443.635315 -80.0183029 -2863.29077 70.7404404
236.385406 349.893188 -2901.33984 10.0000000
485.313416 -366.513947 -2868.35083 10.0000000
with the positions of the spheres and their radii.
My file.p reads:
set terminal png size 500,500
set output 'file.png'
set multiplot
set xrange [-1000:1000]
set yrange [-1000:1000]
set zrange [-3000:-2500]
splot "data.dat" using 1:2:3:4 ps variable pt 7
splot -(3000**2-x**2-y**2)**(0.5)
but the dots that gnuplot provides me are much bigger.
I understand that it is because ps yields points that are radius times bigger than the normal size.
Meaning that ps does not allow to set the radius of the dots, but rather how many times bigger it is than the normal points.
How can I set the radius of the points please ?
Use "with circles" rather than "with points pt 7".
From the manual:
gnuplot> help with circles
The `circles` style plots a circle with an explicit radius at each data point.
The radius is always interpreted in the units of the plot's horizontal axis
(x or x2). The scale on y and the aspect ratio of the plot are both ignored.
If the radius is not given in a separate column for each point it is taken from
`set style circle`. In this case the radius may use graph or screen coordinates.
Many combinations of per-point and previously set properties are possible.
For 2D plots these include
using x:y
using x:y:radius
using x:y:color
using x:y:radius:color
using x:y:radius:arc_begin:arc_end
using x:y:radius:arc_begin:arc_end:color
By default a full circle will be drawn. It is possible to instead plot arc
segments by specifying a start and end angle (in degrees) in columns 4 and 5.
A per-circle color may be provided in the last column of the using specifier.
In this case the plot command must include a corresponding variable color
term such as `lc variable` or `fillcolor rgb variable`.

Draw a translucent sphere using rgl in R

I have a set of data, looks like:
x y z
1 1 2 1
2 3 5 7
3 -3 2 4
4 -2 1 1
so each row record the dot coordinate in a 3-D space. I want to plot all the dot as points except for one, say no.15 as a translucent sphere, with radius I can set. Then I can see from the plot that which of those points in the data are included in the sphere. I'm using RGL package right now and did the following:
> open3d()
> plot3d(readin,col=3,type="p")
> plot3d(readin[15,],col=2,add=T,type="s",radius=0.1)
So the first plot command plotted the whole set as scatter plots and the second plot command picked the 15th row of the data and plot it as a sphere and add it to the previous canvas. I just wondering if I can make the sphere translucent so that I can see which dots a included in the sphere which means those dots are very near to the one I select.
Is there a way to do this by RGL Or you can provide me another ways to complete this task?
Thanks!
I think what you are looking for is the argument alpha.
Example
df <- data.frame(x=c(1,3,-3,-2), y=c(2,5,2,1),z=c(1,7,4,1))
library(rgl)
open3d()
plot3d(df,col=3,type="p", radius=0.5)
plot3d(df,col=rgb(1,0,0.3),alpha=0.5, add=T,type="s",radius=1)
You can plot transparent spheres using the alpha argument to spheres3d. You can rotate the plot to move the box line behind the sphere to prove it's transparent.
spheres3d(dat[4,],col=rgb(1,0,0), alpha=0.9) # transparent red.
(I tried to do it with the alpha argument to rgb but it failed.)
If you just want to find out which points are within a certain radius of point 15 then you can calculate the Euclidean distance from each point to point 15 and see which of those distances are less than the radius. No plotting needed (though you could plot those points as a different color to highlight them. The dist function is one way to compute the distances, or it is simple to program yourself.

Gnuplot: plot with circles of a defined radius

I know on gnuplot you can plot some data with circles as the plot points:
plot 'data.txt' using 1:2 ls 1 with circles
How do I then set the size of the circles? I want to plot several sets of data but with different size circles for each data set.
If you have a third column in your data, the third column specifies the size of the circles. In your case, you could have the third column have the same value for all the points in each data set. For example:
plot '-' with circles
1 1 0.2
e
will plot a circle at (1,1) with radius 0.2. Note that the radius is in the same units as the data. (The special file name '-' lets you input data directly; typing 'e' ends the input. Type help special at the gnuplot console for more info.)
You can look here for more ideas of how to use circles.
I used:
plot "file" using 1:2:($2*0+10) with circles
This will fake a the third column specifying the sizes - it is probably possible to write it simpler, but this worked for me.

Resources