[Tex/LaTex] Drawing spherical 3D plot with PGFPlots

pgfplots

I want to draw some plots which looks like this:

enter image description here

I did not find anything related to this in the doc, and browse the Internet without success. I found ways to plot spherical values with a changing radius according to my function but I prefer mapping my function values on a sphere using a colormap.

Note that I have no explicit formula for the function values but I can put them into a file.

Best Answer

Here's an option using asymptote instead of pgfplots. Compile with pdflatex -shell-escape.

\documentclass{standalone}
\usepackage{asypictureB}

\begin{document}

\begin{asypicture}{name=spherical-harmonic-L-M-4-3}
import graph3;
import palette;

size3(200,200,200);
currentprojection=orthographic(4,1,1);


//Parametric function R^2 --> R^3 for drawing the sphere
real R=1;
triple f( pair t ) {
  return (
    R*sin(t.y)*cos(t.x),
    R*sin(t.y)*sin(t.x),
    R*cos(t.y)
    );
}

//Parametric function R^3 --> R^1 for coloring the sphere; normalization is not important.
int M = 3;
//int L = 4; Not actually used, but good for note-taking.
real PLM(real x){
    return -105*x*(1-x^2)^1.5;
}
real SphHarm(triple v){
    real r = v.x^2+v.y^2+v.z^2;
    real costheta = v.z/r;
    real cosphi = v.x/r;
    real sinphi = v.y/r;
    pair phase = (v.x + I*v.y)^M; //phase is complex number, phase.x is real part, phase.y is imaginary part
    return phase.x*PLM(costheta);
    }

//Create sphere surface
surface s=surface(f,(0,0),(2pi,2pi),100,Spline);
//Map colors onto surface
pen p1 = opacity(0.5)+red;
pen p2 = opacity(0.5)+white;
pen p3 = opacity(0.5)+blue;
s.colors(palette(s.map(SphHarm),Gradient(p1,p2,p3) )); //Gradient pallete interpolates between different colors
//Draw the surface
draw(s);

//Draw Axes
pen thickblack = gray+0.5;
real axislength = 1.25;
draw(L=Label("$x$",black, position=Relative(1.1), align=SW), O--axislength*X,thickblack, Arrow3); 
draw(L=Label("$y$",black, position=Relative(1.1), align=E), O--axislength*Y,thickblack, Arrow3); 
draw(L=Label("$z$",black, position=Relative(1.1), align=N), O--axislength*Z,thickblack, Arrow3); 

\end{asypicture}

\end{document}

enter image description here

Some notes:

  1. I colored the sphere's surface based on the real part of the spherical harmonic.
  2. I used an explicit function to do the coloring for me, rather than a data table To change the spherical harmonic, edit L and M, and PLM, which is the associated legendre polynomial. (Unfortunately, asymptote does not have a built-in library for these to my knowledge.)

If this is appealing to you and you want to learn more about asymptote, I recommend Charles Staats' asymptote tutorial

Related Question