Search code examples
plotgnuplotheatmap

gnuplot: Plotting a function on the surface of a sphere


I have a function, say f(theta, phi) = sqrt(1 - (sin(theta)*sin(phi))**2) + 5 * sqrt(1 - (sin(theta)*cos(phi))**2) that I want to plot as a color plot on the surface of a sphere. However, I can't figure out how I have to feed this function into splot in order to achieve this without first generating a file with the appropriate values in a table.

How can I get gnuplot to do this?


Solution

  • Instead of generating a file, you can use the special filename "++", see help special. I think the last example on the gnuplot demo page has your use case. Simplified with minor modifications:

    xx(u, v) = cos(v) * cos(u)
    yy(u, v) = cos(v) * sin(u)
    zz(u, v) = sin(v)
    f(theta, phi) = sqrt(1 - (sin(theta)*sin(phi))**2) + 5 * sqrt(1 - (sin(theta)*cos(phi))**2)
    
    set parametric
    set isosamples 121, 61
    set samples 121, 61
    set urange [-pi:pi]
    set vrange [-pi/2:pi/2]
    
    set border 4095
    set view equal xyz
    set xyplane 0
    
    splot "++" using (xx($1,$2)):(yy($1,$2)):(zz($1,$2)):(f($1,$2)) with pm3d notitle
    

    This is the result:

    colored sphere

    Please double check whether the definitions of the spherical coordinates match.