Plotting in 3D in spherical coordinates
asked 2011-04-19 01:28:25 -0500
This post is a wiki. Anyone with karma >750 is welcome to improve it.
My problem is that I am trying to plot (in full 3D spherical coordinates) a set of values stored in a 2D lookup table or LUT. The LUT is actually stored as a numpy 1801*3601 2D array indexed by theta and phi respectively in 0.1 degree steps. The LUT in fact represents an antenna radiation pattern (i.e. antenna gain/ radiation intensity as a function of theta and phi). However, my problem generalises to any one of plotting a function in spherical coordinates.
My first attempt at plotting this in Sage was to use the 'spherical_plot3d()' function. First I defined a function called:
which returned a value from the lookup table (LUT) representing antenna gain (a positive number in decibels). Then I tried plotting this as follows:
Now this almost does what I want, but not quite. My LUT has a high resolution with 0.1 degree intervals. However, the 3D plot which the above command delivers (via Jmol) seems to smooth the pattern where I don't want it to be smoothed (because it has abrupt edges), and is too 'blocky' where I would like the pattern to be smooth. Is there any way I can have fine control of the step-size in phi and theta (u and v in Sage-speak), or must I leave it to Sage to control these?
I also tried a different approach, which is to use list_plot3d, but to transform the coordinates from spherical to rectangular when building up my list to plot. To discuss this case we can simplify the problem to say that we wish to plot the radiation pattern of an isotropic antenna, i.e. one which has equal gain in all directions. Thus what we are simply trying to do is to plot a sphere in 3 dimensions from a list of 3-tuples, where each tuple represents an (x,y,z) coordinate in Cartesian space. However, when generating the points to plot I transform from spherical to cartesian coordinates when setting up the list of points to plot, thus:
sage: import numpy as np
sage: r=1 # Representing the gain of an isotropic antenna
sage: listOfPointsOnSurfaceOfSphere = [ (r* sin(theta) * cos(phi), r* sin(theta)* sin(phi), r* getGain(phi,theta)* cos(theta)) for theta in np.arange(0.1,pi,0.1) for phi in np.arange(-pi,pi,0.1) ]
sage: myPlot1 = list_plot3d(listOfPointsOnSurfaceOfSphere).show()
Now what I find is that my plot is all 'spiky', whereas I was hoping to see a smooth sphere. Of course, what is happening is that I have multiple points with the same (or similar) (x,y) coordinates, but very different z coordinates, since every point on the sphere 'above the equator' (i.e. above the x,y plane) has effective neighbours as mirror images below. This seems to be screwing up the interpolation routine, which isn't able to identify that it ... (more)