2

Basically, I want to draw colored spheres via parametric splot (image below, top). However, I couldn't find a way to make them uniform in color but each sphere with a different color.

I found this post (Gnuplot, pm3d and surfaces) which tought me to achieve it by plotting the spheres to a datablock first and then plot the shifted datablock (image below, middle).

Now, I want to add some lines. But then the colors of the spheres unexpectedly change their color (image below, bottom). Why? How to avoid? How can I keep the originally intended colors?

My code:

### connected 3D-spheres with splot and pm3d
reset session
set obj 1 rect from screen 0,0,0 to screen 1,1,0 behind 
set obj 1 rect fc rgb "black" fs solid 1.0
set view equal xyz
set view 45,45 
unset border
unset tics
unset colorbox

set style fill solid 1.0 noborder
set pm3d depthorder noborder
set pm3d lighting specular 0.5

set isosamples 50,50
set parametric
set urange [-pi/2:pi/2]
set vrange [0:2*pi]
Radius = 1
set table $Sphere
    splot Radius*cos(u)*cos(v), Radius*cos(u)*sin(v), Radius*sin(u)
unset table
unset parametric

$Pos <<EOD
0 0 0
4 0 0
4 4 0
0 4 0
EOD

$Bonds <<EOD
0 0 0
4 0 0

4 0 0
4 4 0

4 4 0
0 4 0

0 4 0
0 0 0
EOD

PosX(i) = word($Pos[i],1)
PosY(i) = word($Pos[i],2)
PosZ(i) = word($Pos[i],3)

set palette defined (1 'red', 2 'green', 3 'blue', 4 'yellow')

set multiplot layout 3,1
    set parametric
        splot for [i=1:4] Radius*cos(u)*cos(v)+PosX(i), Radius*cos(u)*sin(v)+PosY(i), \
        Radius*sin(u)+PosZ(i) with pm3d not
    unset parametric
    unset obj 1 
    splot \
        for [i=1:4] $Sphere u ($1+PosX(i)):($2+PosY(i)):($3+PosZ(i)):(i) with pm3d not
    splot \
        for [i=1:4] $Sphere u ($1+PosX(i)):($2+PosY(i)):($3+PosZ(i)):(i) with pm3d not,\
        $Bonds u 1:2:3 w l lw 4 lc rgb "grey" not
unset multiplot
### end of code

The result:

enter image description here

theozh
  • 22,244
  • 5
  • 28
  • 72
  • 1
    Fencepost error, I suspect. Try `set cbrange [1:4]` before the final plot. – Ethan Apr 24 '19 at 21:45
  • Thank you, this works! Actually, I also can put `set cbrange [1:4]` before the _first_ plot. But I still don't understand why this happens only if I have a combined `splot`-command: `splot $Sphere .... , $Bonds ....` What do you mean by "Fence post" error? – theozh Apr 25 '19 at 09:03
  • 1
    To fence a length of N units requires N+1 fenceposts. You have defined a palette with four fixed colors. These are the "fenceposts". If you want the fixed colors to fall exactly on integer values of cb then total span of cb must be 3. The zrange of your final plot is [0:4] (all the "bonds" are at z=0). So the total span is 4. If you stretch a length 3 palette to span a length 4 range of cb, the fixed colors fall on non-integral values. You can fix this by pinning the total span of cb to match the span of your palette. – Ethan Apr 25 '19 at 17:55

1 Answers1

1

Here's a 2nd solution that doesn't involve setting up the palette. Note that this requires a feature from the development branch of gnuplot (version 5.3). It will appear in a future stable release but is not yet in version 5.2.6.

[preliminaries as above, followed by]

set palette defined (1 'red', 2 'green', 3 'blue', 4 'yellow')
set style line 1 lc 'red'
set style line 2 lc 'green'
set style line 3 lc 'blue'
set style line 4 lc 'yellow'

set multiplot layout 1,3
    set parametric
        splot for [i=1:4] Radius*cos(u)*cos(v)+PosX(i), Radius*cos(u)*sin(v)+PosY(i), \
        Radius*sin(u)+PosZ(i) with pm3d not
    unset parametric
    unset obj 1
    splot \
        for [i=1:4] $Sphere u ($1+PosX(i)):($2+PosY(i)):($3+PosZ(i)):(i) with pm3d not
    splot \
        for [i=1:4] $Sphere u ($1+PosX(i)):($2+PosY(i)):($3+PosZ(i)) with pm3d fc ls i not,\
        $Bonds u 1:2:3 w l lw 4 lc rgb "grey" not
unset multiplot

with pm3d fc lt i would also work (linetypes rather than linestyles)

enter image description here

Ethan
  • 13,715
  • 2
  • 12
  • 21
  • Unfortunately, `with pm3d fc ls i` or `with pm3d fc lt i` doesn't work for me. I get the error `unexpected or unrecognized token` pointing to `fc`. (using gnuplot 5.2.6, Win7,wxt terminal) – theozh Apr 25 '19 at 07:36
  • I will edit the answer to note that it requires a newer version of gnuplot. – Ethan Apr 25 '19 at 17:56