Reputation: 5619
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:
To something like this, for example (I just dilated the picture):
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 ?
Upvotes: 2
Views: 4463
Reputation: 11
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])
Upvotes: 0
Reputation: 8569
x
, y
and z
coordinatesax.set_aspect(1.0)
otherwise the plot view will be scaled back, to a spherex
, y
and z
)Upvotes: 5