'plot spheres in gnuplot from file
I have a file with 4 columns (radius,x,y,z) that contains on each line the coordinates of a sphere and its radius. Could you please help me to plot these spheres in gnuplot? ( If it is not possible in gnuplot, could you recommend another plotting tool?)
Solution 1:[1]
There is a with circles
style option in 2D. In 3D you can pass the radius from the file to the pointsize
option. Consider the following data:
# radius, x, y, z
1 0 0 0
2 1 2 2
3 3 4 5
1 2 5 7
1 1 3 4
2 2 0 1
Then you can plot it like this (ps
is short for pointsize
and pt
is short for pointtype
; pt 7
draws circles):
splot "data" u 2:3:4:1 ps variable pt 7
Solution 2:[2]
If you want to make nicer plots you have to plot parametric spheres. It is however not possible to have a parametric plot read parameters from a datafile directly.
consider the same following data:
# radius, x, y, z
1 0 0 0
2 1 2 2
3 3 4 5
1 2 5 7
1 1 3 4
2 2 0 1
The solution to this (in gp 5.1 or higher) is to first read the sphere coordinates from "data" into gnuplot arrays, then loop over the arrays for parametric plots:
# find the number of spheres
stats "data" using (0):(0) nooutput
spheres=STATS_records
# define arrays and fill with sphere coordinates
array sphereR[spheres]
array sphereX[spheres]
array sphereY[spheres]
array sphereZ[spheres]
stats "data" using ( sphereR[int($0)+1]=$1,sphereX[int($0)+1]=$2,sphereY[int($0)+1]=$3,sphereZ[int($0)+1]=$4,0):(0) nooutput
# plot spheres
set parametric
set isosamples 10,10
set view equal xyz
set urange[0:2*pi]
set vrange[-pi/2:pi/2]
splot for [i=1:spheres] sphereX[i]+sphereR[i]*cos(v)*sin(u),sphereY[i]+sphereR[i]*cos(v)*cos(u),sphereZ[i]+sphereR[i]*sin(v) with lines title sprintf("sphere %i",i)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | CorvusCorax |