Discussion:
[Matplotlib-users] question on spherical coordinate plots
Romain Madar
2015-07-10 11:54:08 UTC
Permalink
Dear experts,

I am trying to plot spherical harmonics with matplotlib and I have some
troubles. I am starting from the example
http://matplotlib.org/examples/mplot3d/surface3d_demo2.html where I
change the factor 10 in a function of r=f(theta,phi) (or r=f(u,v) as
they are named in the example). I observe very strange behaviours:

(1) (x,y,z) = (r cos(phi) sin(theta) , r sin(phi) sin(theta) , r
cos(theta)). But np.outer(a,b) is not commutative while the
multiplication is. So how to choose the order in the np.outer() product?
In fact, different order gives very different results.

(2) It's seem impossible to reproduce the well known Ylm(theta,phi)
plots. Using for example this document
http://www.cs.dartmouth.edu/~wjarosz/publications/dissertation/appendixB.pdf
:





I don't know if I am doing something wrong or so, but I don't understand
... My full code is bellow.

Thanks a lot in advance !
Cheers,
Romain


PS:

import math
import numpy as np
import pylab as p
from mpl_toolkits.mplot3d import Axes3D

def f(theta,phi):
return np.sin(phi)*np.cos(phi)*np.sin(theta)**2

fig = p.figure()
ax = fig.add_subplot(111, projection='3d')

theta = np.linspace(0, np.pi, 500)
phi = np.linspace(0, 2*np.pi, 500)

r = f(theta,phi)
x = r**2 * np.outer( np.cos(phi) , np.sin(theta) )
y = r**2 * np.outer( np.sin(phi) , np.sin(theta) )
z = r**2 * np.outer(np.ones(phi.shape), np.cos(theta))

#x = r**2 * np.outer( np.sin(theta) , np.cos(phi) )
#y = r**2 * np.outer( np.sin(theta) , np.sin(phi) )
#z = r**2 * np.outer( np.cos(theta), np.ones(theta.shape) )

ax.plot_surface(x,y,z)
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_zlabel("Z")

p.show()
--
=========================================================
Romain Madar

Laboratoire de Physique Corpusculaire de Clermont-Ferrand
Campus Universitaire des Cézeaux
4 avenue Blaise Pascal
TSA 60026, CS 60026
63178 AubiÚre cedex, FRANCE

Email: ***@cern.ch
Tel. : +33 (0)4 73 40 71 57
Off. : 8204-8205
=========================================================
Benjamin Root
2015-07-10 16:47:16 UTC
Permalink
Your theta and phi were essentially 1D rather than 2D, so it didn't allow
for 2 degrees of freedom. And you don't need np.outer() for this:

theta = np.linspace(0, np.pi, 500)[:, None]
phi = np.linspace(0, 2*np.pi, 500)[None, :]

r = f(theta, phi)
x = r**2 * np.cos(phi) * np.sin(theta)
y = r**2 * np.sin(phi) * np.sin(theta)
z = r**2 * np.cos(theta)

The use of np.outer() in the original example acted a bit like a creating a
grid of u/v values in a 2D grid. However, your formulation required
computing a 2D grid of radius values in order to work correctly.

Cheers!
Ben Root
Post by Romain Madar
Dear experts,
I am trying to plot spherical harmonics with matplotlib and I have some
troubles. I am starting from the example
http://matplotlib.org/examples/mplot3d/surface3d_demo2.html where I
change the factor 10 in a function of r=f(theta,phi) (or r=f(u,v) as they
(1) (x,y,z) = (r cos(phi) sin(theta) , r sin(phi) sin(theta) , r
cos(theta)). But np.outer(a,b) is not commutative while the multiplication
is. So how to choose the order in the np.outer() product? In fact,
different order gives very different results.
(2) It's seem impossible to reproduce the well known Ylm(theta,phi) plots.
Using for example this document
http://www.cs.dartmouth.edu/~wjarosz/publications/dissertation/appendixB.pdf
I don't know if I am doing something wrong or so, but I don't understand
... My full code is bellow.
Thanks a lot in advance !
Cheers,
Romain
import math
import numpy as np
import pylab as p
from mpl_toolkits.mplot3d import Axes3D
return np.sin(phi)*np.cos(phi)*np.sin(theta)**2
fig = p.figure()
ax = fig.add_subplot(111, projection='3d')
theta = np.linspace(0, np.pi, 500)
phi = np.linspace(0, 2*np.pi, 500)
r = f(theta,phi)
x = r**2 * np.outer( np.cos(phi) , np.sin(theta) )
y = r**2 * np.outer( np.sin(phi) , np.sin(theta) )
z = r**2 * np.outer(np.ones(phi.shape), np.cos(theta))
#x = r**2 * np.outer( np.sin(theta) , np.cos(phi)
)
#y = r**2 * np.outer( np.sin(theta) , np.sin(phi) )
#z = r**2 * np.outer( np.cos(theta), np.ones(theta.shape) )
ax.plot_surface(x,y,z)
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_zlabel("Z")
p.show()
--
=========================================================
Romain Madar
Laboratoire de Physique Corpusculaire de Clermont-Ferrand
Campus Universitaire des Cézeaux
4 avenue Blaise Pascal
TSA 60026, CS 60026
63178 AubiÚre cedex, FRANCE
Tel. : +33 (0)4 73 40 71 57
Off. : 8204-8205
=========================================================
------------------------------------------------------------------------------
Don't Limit Your Business. Reach for the Cloud.
GigeNET's Cloud Solutions provide you with the tools and support that
you need to offload your IT needs and focus on growing your business.
Configured For All Businesses. Start Your Cloud Today.
https://www.gigenetcloud.com/
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Loading...