How to plot a spherical segment in Python? - python

How can one plot a spherical segment, specifically a sphere "slice" in Python?
I know how to plot a sphere surface via
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)
x = 10 * np.outer(np.cos(u), np.sin(v))
y = 10 * np.outer(np.sin(u), np.sin(v))
z = 10 * np.outer(np.ones(np.size(u)), np.cos(v))
ax.plot_surface(x, y, z, rstride=4, cstride=4, color='b')
plt.show()
or some variation of that code, but I'm struggeling to plot only a part of the sphere, leading to an image like the following:
https://en.wikipedia.org/wiki/Spherical_segment#/media/File:LaoHaiKugelschicht1.png
If I vary the code I presented above by manipulating the definitions of u and v, e.g.:
u = np.linspace(0, 2 * np.pi, 20)
v = np.linspace(0, np.pi, 20)
the sphere is still presented as a whole, but with a very poor resolution.
Changing the starting point of the linspace range doens't seem to change anything.

I have figured out something that works for me. Here you go:
q = 0.5 # defines upper starting point of the spherical segment
p = 0.8 # defines ending point of the spherical segment as ratio
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(q, p * np.pi, p * 100)
x = r * np.outer(np.cos(u), np.sin(v)) + x_0
y = r * np.outer(np.sin(u), np.sin(v)) + y_0
z = r * np.outer(np.ones(np.size(u)), np.cos(v)) + z_0
ax.plot_surface(x, y, z, color='b')

Related

Plotting points from a function on a unit sphere using matplotlib

I am trying to plot the following function on a unit sphere, the points should be on the sphere and fill up the whole sphere however some of the points are falling off. Any suggestions why? I believe it is because the sphere is not spanning 1,1,1 3D grid but I am not sure how to edit my code to fix this.
from itertools import product, combinations
import matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
def d(kx,ky):
M = 1
B = 1
vf = 1
kxx,kyy = np.meshgrid(kx,ky)
x = (vf*kxx)/(np.sqrt(((((vf**2)*(kxx**2)))+((vf**2)*(kyy**2))+(M-B*(kxx**2+(kyy**2)))**2)))
y = (vf*kxx)/(np.sqrt(((((vf**2)*(kxx**2)))+((vf**2)*(kyy**2))+(M-B*(kxx**2+(kyy**2)))**2)))
z = (M-B*(kxx**2+(kyy**2)))/(np.sqrt(((((vf**2)*(kxx**2)))+((vf**2)*(kyy**2))+(M-B*(kxx**2+(kyy**2)))**2)))
return x,y,z
kx = np.linspace(-2, 2, 10)
ky = np.linspace(-2, 2, 10)
xi, yi, zi = d(kx,ky)
phi = np.linspace(0, np.pi, 100)
theta = np.linspace(0, 2*np.pi, 100)
phi, theta = np.meshgrid(phi, theta)
x = np.sin(phi) * np.cos(theta)
y = np.sin(phi) * np.sin(theta)
z = np.cos(phi)
fig = plt.figure(figsize=plt.figaspect(1.))
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(x, y, z, color="w", rstride=1, cstride=1)
ax.scatter(xi,yi,zi,color="k",s=20)
plt.show()
Thank you kindly,

Draw points connected by spiral on a sphere with Matplotlib

I would like to draw a sphere with points on its surface using Matplotlib. These points shall be connected by a spiral that spirals from one side of the sphere to the other.
To clarify this a little bit the plot should more or less look like this:
Has anyone a tip about how to do this?
Need to know parameters of spiral, formula or set of points.
However I post a code to plot a line with markers on a sphere for your start:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_aspect('equal')
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)
x = 1 * np.outer(np.cos(u), np.sin(v))
y = 1 * np.outer(np.sin(u), np.sin(v))
z = 1 * np.outer(np.ones(np.size(u)), np.cos(v))
elev = 10.
rot = 80. / 180. * np.pi
ax.plot_surface(x, y, z, rstride=1, cstride=1, color='y', linewidth=0, alpha=0.5)
# plot lines in spherical coordinates system
a = np.array([-np.sin(elev / 180 * np.pi), 0, np.cos(elev / 180 * np.pi)])
b = np.array([0, 1, 0])
b = b * np.cos(rot) + np.cross(a, b) * np.sin(rot) + a * np.dot(a, b) * (1 - np.cos(rot))
ax.plot(np.sin(u),np.cos(u),0,color='r', linestyle = '-', marker='o', linewidth=2.5)
ax.view_init(elev = elev, azim = 0)
plt.show()

Matplotlib 3dplot, order not correct

I am having problems with matplotlibs 3dplot. If I plot two 3d objects, the one that is supposed to be in front is sometimes in the back. Take for example the following code
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as pl
import numpy as np
fig = pl.figure()
ax = fig.add_subplot(111, projection='3d')
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)
X = 10 * np.outer(np.cos(u), np.sin(v))
Y = 10 * np.outer(np.sin(u), np.sin(v))
Z = 10 * np.outer(np.ones(np.size(u)), np.cos(v))
ax.plot_surface(X+10,Y,Z, rstride=4, cstride=4, color='b')
u=np.linspace(-2,2,100)
X = 10 * np.outer(np.ones(len(u)), u)
Y = 10 * np.outer(u, np.ones(len(u)))
Z = 10 * np.zeros((len(u), len(u)))
ax.plot_surface(Z,X,Y, rstride=4, cstride=4, color='b')
pl.show()
It is supposed to plot a plane, with a sphere in from of it, but the sphere appears to be behind the plane.

Using python, I am looking to plot a 3D spherical cap with a given radius r

I am looking to make a small program that will produce a 3D plot of a spherical cap given parameters h and a.
Any help would be greatly appreciated!
I've started with a matplotlib example to plot a sphere...
I want to enter a value for a and h, get the corresponding radius of the sphere, then plot a spherical cap of height h and base radius a. Ideally the z axis and x y axis on the 3d plot will correspond to my initial a and h entered (as long as it makes geometric sense i presume?)
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
def capRatio(r, a, h):
'''cap to sphere ratio'''
surface_cap = np.pi * (a**2 + h**2)
surface_sphere = 4.0 * np.pi * r**2
return surface_cap/surface_sphere
def findRadius(a, h):
"find radius if you have cap base radius a and height"
r = (a**2 + h**2) / (2*h)
return r
#choose a and h
a = 4
h = 3
r = findRadius(a,h)
p = capRatio(r, a, h) # Ratio of sphere to be plotted, could also be a function of a.
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, p*np.pi, 100)
x = r * np.outer(np.cos(u), np.sin(v))
y = r * np.outer(np.sin(u), np.sin(v))
z = r * np.outer(np.ones(np.size(u)), np.cos(v))
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(x, y, z, rstride=4, cstride=4, alpha=0.3 , cmap= cm.winter)
plt.show()
The next part comes in limiting the range of coordinates you will accept and this could be done in a few ways. Here is one way that appears to work:
p = 0.8 # Ratio of sphere to be plotted, could also be a function of a.
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, p*np.pi, p*100)

Plotting Ellipsoid with Matplotlib

Does anyone have sample code for plotting ellipsoids? There is one for sphere on matplotlib site, but nothing for ellipsoids. I am trying to plot
x**2 + 2*y**2 + 2*z**2 = c
where c is a constant (like 10) that defines an ellipsoid. I tried the meshgrid(x,y) route, reworked the equation so z is on one side, but the sqrt is a problem. The matplotlib sphere example works with angles, u,v, but I am not sure how to work that for ellipsoid.
Here is how you can do it via spherical coordinates:
# from mpl_toolkits.mplot3d import Axes3D # Not needed with Matplotlib 3.6.3
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(figsize=plt.figaspect(1)) # Square figure
ax = fig.add_subplot(111, projection='3d')
coefs = (1, 2, 2) # Coefficients in a0/c x**2 + a1/c y**2 + a2/c z**2 = 1
# Radii corresponding to the coefficients:
rx, ry, rz = 1/np.sqrt(coefs)
# Set of all spherical angles:
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)
# Cartesian coordinates that correspond to the spherical angles:
# (this is the equation of an ellipsoid):
x = rx * np.outer(np.cos(u), np.sin(v))
y = ry * np.outer(np.sin(u), np.sin(v))
z = rz * np.outer(np.ones_like(u), np.cos(v))
# Plot:
ax.plot_surface(x, y, z, rstride=4, cstride=4, color='b')
# Adjustment of the axes, so that they all have the same span:
max_radius = max(rx, ry, rz)
for axis in 'xyz':
getattr(ax, 'set_{}lim'.format(axis))((-max_radius, max_radius))
plt.show()
The resulting plot is similar to
The program above actually produces a nicer looking "square" graphics.
This solution is strongly inspired from the example in Matplotlib's gallery.
Building on EOL's answer. Sometimes you have an ellipsoid in matrix format:
A and c Where A is the ellipsoid matrix and c is a vector representing the centre of the ellipsoid.
import numpy as np
import numpy.linalg as linalg
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# your ellispsoid and center in matrix form
A = np.array([[1,0,0],[0,2,0],[0,0,2]])
center = [0,0,0]
# find the rotation matrix and radii of the axes
U, s, rotation = linalg.svd(A)
radii = 1.0/np.sqrt(s)
# now carry on with EOL's answer
u = np.linspace(0.0, 2.0 * np.pi, 100)
v = np.linspace(0.0, np.pi, 100)
x = radii[0] * np.outer(np.cos(u), np.sin(v))
y = radii[1] * np.outer(np.sin(u), np.sin(v))
z = radii[2] * np.outer(np.ones_like(u), np.cos(v))
for i in range(len(x)):
for j in range(len(x)):
[x[i,j],y[i,j],z[i,j]] = np.dot([x[i,j],y[i,j],z[i,j]], rotation) + center
# plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_wireframe(x, y, z, rstride=4, cstride=4, color='b', alpha=0.2)
plt.show()
plt.close(fig)
del fig
So, not too much new here, but helpful if you've got an ellipsoid in matrix form which is rotated and perhaps not centered at 0,0,0 and want to plot it.
If you have an ellipsoid specified by an arbitrary covariance matrix cov and offset bias, you do not need to figure out the intuitive parameters of the ellipsoid to get the shape. Specifically, you don't need the individual axes or rotations. The whole point of the matrix is that it transforms a unit sphere (represented by the identity matrix) into your ellipse.
Starting with
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)
Make a unit sphere
x = np.outer(np.cos(u), np.sin(v))
y = np.outer(np.sin(u), np.sin(v))
z = np.outer(np.ones_like(u), np.cos(v))
Now transform the sphere:
ellipsoid = (cov # np.stack((x, y, z), 0).reshape(3, -1) + bias).reshape(3, *x.shape)
You can plot the result pretty much as before:
ax.plot_surface(*ellipsoid, rstride=4, cstride=4, color='b', alpha=0.75)

Categories