Plotting an ellipsoid

Question:

I have three numbers representing lengths. Each number is the length of an object on the x, y, and z axes. The object I want to represent is (probably) a deformed sphere.

How can I 3D plot this object by specifying these 3 lengths ?

I would like to go from this:

enter image description here

To something like this, for example (I just dilated the picture):

enter image description here

I tried to take the following code (from Ellipsoid creation in Python) and play a bit with the definition of x, y, and z:

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

phi = np.linspace(0,2*np.pi, 256).reshape(256, 1) # the angle of the projection in the xy-plane
theta = np.linspace(0, np.pi, 256).reshape(-1, 256) # the angle from the polar axis, ie the polar angle
radius = 4

# Transformation formulae for a spherical coordinate system.
x = radius*np.sin(theta)*np.cos(phi)
y = radius*np.sin(theta)*np.sin(phi)
z = radius*np.cos(theta)

fig = plt.figure(figsize=plt.figaspect(1))  # Square figure
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(x, y, z, color='b')

But I can’t reach what I want. Could you give me a hand please ?

Asked By: JPFrancoia

||

Answers:

  1. You should use different radii for the x, y and z coordinates
  2. to actually see the effect, use ax.set_aspect(1.0) otherwise the plot view will be scaled back, to a sphere
  3. (to have the ellipsoid not axis aligned/zero centered: use a rotation and/or a translation on x, y and z)
Answered By: DomTomCat

Here is a modified version of your code. I added the ratio in data in x coordinate and specified the box aspect ratio.

import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator
import numpy as np

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

phi = np.linspace(0,2*np.pi, 256).reshape(256, 1) # the angle of the projection in the xy-plane
theta = np.linspace(0, np.pi, 256).reshape(-1, 256) # the angle from the polar axis, ie the polar angle
radius = 4

# Transformation formulae for a spherical coordinate system.
ratio=3
x = ratio*radius*np.sin(theta)*np.cos(phi)
y = radius*np.sin(theta)*np.sin(phi)
z = radius*np.cos(theta)

fig = plt.figure()  # Square figure
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(x, y, z, color='b')
ax.set_box_aspect([ratio,1,1])
Answered By: hojjat KAVEH
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.