SPHERE3D
SPHERE3D
a Spherical Surface
J M De Freitas
QinetiQ Ltd, Winfrith Technology Centre
Winfrith, Dorchester
Dorset DT2 8XJ
United Kingdom
28 September 2005
1. This function is made available to Matlab® users under the terms and conditions set out in the Matlab
Exchange by The Mathworks, Inc.
2. Where the use of SPHERE3D is to be cited, the following is recommended:
J M De Freitas. ‘SPHERE3D: A Matlab® Function to Plot 3-Dimensional Data on a Spherical Surface’.
QinetiQ Ltd, Winfrith Technology Centre, Winfrith, Dorchester DT2 8XJ. UK. 28 September 2005.
3. No offer of warranty is made or implied by the author and use of this work means that the user has agreed
to take full responsibility for its use.
2
3D PLOT ON A SPHERICAL SURFACE
6.84
4.41
1.98
-0.45
-2.88
-5.31
-7.74
6.84
6
4.41
4
1.98
2
-0.45
0
-2.88
-2
-5.31
-4
-7.74
-6
SURFCONTOUR
HOT MAP
CONTOUR / MESH
3
z
ϕ
y
θ
Coordinate System
Fig.1 shows the coordinate system on which SPHERE3D is based. Note that in the function,
φ is constrained to vary over π , i.e. the difference between φmax (phi_max) and φmin (phi_min)
can be no more than π . In a similar manner, θ is constrained over 2π and as such the
difference between θ max (theta_max) and θ min (theta_min) can be no more than 2π .
Mesh Plot
The mesh plot shown above was produced using:
P = peaks(20);
sphere3d(-P,-pi,pi,-pi/2,pi/2,20,.4,'mesh','spline');
In this case, the angular range for θ covers -180o to 180o, while φ takes up -90o to +90o, with
sphere radius 20. The spline interpolation method was used to increase the dimension of the
input matrix from 20x20 to 50x50 using a mesh scale value of 0.4. Since the Zscale factor is
not specified, it defaults to 1.
Contour Plot
The contour plot shown above was produced using:
P = peaks(20);
sphere3d(-P,-pi/2,pi,-pi/3,pi/3,20,.4,'contour','spline');
The angular range and radius are unchanged from the mesh example above. Note further
that the colour scheme of the contour plot is unaffected by the colormap command. See
Surf Plot below. Moreover, when the angular range of θ is 360o, a vertical line on the
spherical surface is used to show the reference or start point.
Surf Plot
The surf plot is slightly different to the standard Matlab surf plot. In SPHERE3D the colour
intensity varies radially from inside (lowest value) to outside (highest value). This allows the
user to easily spot peaks and troughs on the surface.
4
The surf plots produced by this function accept the colormap command. For example, the
hot scale map shown above was produced using:
P = peaks(20);
sphere3d(-P,-pi,pi,-pi/2,pi/2,20,.4,'surf','spline');
colormap hot
Any standard colour mapping could be used. Note that there is no reference line in the surf
plot as in the contour plot. The output (x,y,z,c) can be used to produce other colour plots with
colour mapping c. Standard properties, e.g. axes, axis labels, title, etc. for the plot can then
be manipulated to suit the user. See Contour/Mesh Plot below.
P = peaks(20);
sphere3d(-P,-pi/2,pi,-pi/4,pi/4,8,.4,'surf','spline',.001);
P = peaks(20);
R = 20; % contour radius
mR = 0.995*R; % mesh radius
[x,y,z,c] = sphere3d(P,-pi,pi,-pi/2,pi/2,mR,.75,'off','spline',0);
mesh(x,y,z)
colormap ([.8 .8 .8]);
hold on
sphere3d(-P,-pi,pi,-pi/2,pi/2,R,.25,'contour','spline',0);
s = colorbar;
delete(s)
hold off
Note that Zscale is 0, and that the radius R for the contour plot is slightly larger than the
radius mR of the mesh plot; this avoids pixel colour writing conflicts. It is recommended that
the mesh radius should be 0.5% smaller than the contour radius i.e., mR = 0.995*R. Note
further that the contour with mesh plot is no longer transparent as when using the contour
function only.
The example above allows you to get smoother contours by specifying the smaller meshscale
value of 0.25 for the contour plot, but specifying a coarser mesh of scale value 0.75 for the
mesh plot i.e., the mesh plot sets the spacing between longitudes and latitudes. The colormap
for the mesh plot is now gray. There are other ways of combining these plots.
P = peaks(20);
[x1,y1,z1,c] = sphere3d(-P,pi/2,2*pi,- pi/4,pi/4,8,.4,'off','spline',.001);
[x2,y2,z2,c] = sphere3d(-P,pi/2,2*pi,-pi/4,pi/4,15,.4,'off','spline',.001);
[x3,y3,z3,c] = sphere3d(-P,pi/2,2*pi,-pi/4,pi/4,25,.4,'off','spline',.001);
surf(x1,y1,z1,c); hold on
surf(x2,y2,z2,c); hold on
5
surf(x3,y3,z3,c);
axis off;
grid off;
set(gca,'DataAspectRatio',[1 1 1])
set(gca,'XDir','rev','YDir','rev');
hold off
produces three concentric surf/filled contour plots. There are other ways of setting out the
script e.g. by using loops. This type of plot could be useful in visualizing spatio-temporal
evolutions of wavefronts e.g. sonar, astronomy etc.
P = peaks(20);
[x1,y1,z1,c] = sphere3d(-P,-pi/8,pi/8,- pi/8,pi/8,8,.4,'off','spline',1);
[x2,y2,z2,c] = sphere3d(-P,-pi/8,pi/8,-pi/8,pi/8,15,.4,'off','spline',1);
[x3,y3,z3,c] = sphere3d(-P,-pi/8,pi/8,-pi/8,pi/8,25,.4,'off','spline',1);
colormap([0 0 0]);
mesh(x1,y1,z1); hold on
mesh(x2,y2,z2); hold on
mesh(x3,y3,z3);
axis off;
grid off;
set(gca,'DataAspectRatio',[1 1 1])
set(gca,'XDir','rev','YDir','rev');
hold off
Function Description
A detailed description of the function can be found from the standard help command – help
sphere3d – once installed, or the header information in the file sphere3d.m.