Manas Dogra
Manas Dogra

Reputation: 317

How to plot a sphere in matplotlib using cartesian co-ordinates?

I want to plot a sphere of unit radius using matplotlib.Most of the examples and documentations do it by using polar co-ordinates,by my approach is to use Cartesian.When i wrote the code by using just the np.sqrt only the upper part was shown,so i defined a function sq only to receive an error message ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

import numpy as np
import matplotlib.pyplot as plt
def sq(x):
    if x>=0:
        return np.sqrt(x)
    else:
        return -np.sqrt(abs(x))
ax = plt.axes(projection="3d")
xlist=np.linspace(-1.0,1.0,50)
ylist=np.linspace(-1.0,1.0,50)
r=np.linspace(1.0,1.0,50)
X,Y= np.meshgrid(xlist,ylist)
Z=sq(r**2-X**2-Y**2)
cp=ax.plot_wireframe(X,Y,Z,color="r")
plt.title('The unit sphere')
plt.show()

How can i edit the program which will show the lower part also?

Upvotes: 2

Views: 3351

Answers (1)

BenT
BenT

Reputation: 3200

If you need the other half of the hemisphere you are creating, simply plot the same hemisphere but negative. Your methodology won't work because for a given x,y coordinate you need 2 values (i.e. +/- z). So even if you set the Z values for a given negative X to be negative you will still not achieve a sphere. If you want a smoother plot then you do need to use the polar coordinate calculation to get the correct sphere boundary values.

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d

ax = plt.axes(projection="3d")
xlist=np.linspace(-1.0,1.0,50)
ylist=np.linspace(-1.0,1.0,50)
r=np.linspace(1.0,1.0,50)
X,Y= np.meshgrid(xlist,ylist)

Z=np.sqrt(r**2-X**2-Y**2) #Use np.sqrt like you had before

cp=ax.plot_wireframe(X,Y,Z,color="r")
cp=ax.plot_wireframe(X,Y,-Z,color="r") # Now plot the bottom half


plt.title('2D Contour Plot of The unit sphere')
plt.show()

enter image description here

Note

If you want the sphere to look more like a sphere using this approach, you can increase the resolution and increase the rstride and cstride like so. You could also rotate your axis as well. For example:

xlist=np.linspace(-1.0,1.0,10000)
ylist=np.linspace(-1.0,1.0,10000)
X,Y= np.meshgrid(xlist,ylist)
Z=np.sqrt(1**2-X**2-Y**2) #Note your r is redundant, use 1.

ax = plt.axes(projection="3d")

cp=ax.plot_wireframe(X,Y,Z,color="r", rstride=1000, cstride=1000)
cp=ax.plot_wireframe(X,Y,-Z,color="r", rstride=1000, cstride=1000)


plt.title('2D Contour Plot of The unit sphere')
plt.show()

enter image description here

Upvotes: 1

Related Questions