Shrinking ellipsoid in matplotlib - python

I am new to animations with matplotlib, and I am trying to animate a shrinking ellipsoid.
Specifically, I want to animate an ellipsoid that shrinks its axes proportionally. (Mathematically, I'm looking for a shrinking factor of e^(-t) multiplied to each axis, where t is time.)
I have made a function of time t that outputs a static ellipsoid with the code below:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from mpl_toolkits.mplot3d import Axes3D
def param_surface(t):
fig = plt.figure(figsize = (10, 10))
ax = fig.add_subplot(111, projection='3d')
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)
x = axis_a(4 * t / 50) * np.outer(np.cos(u), np.sin(v))
y = axis_a(4 * t / 50) * np.outer(np.sin(u), np.sin(v))
z = axis_b(4 * t / 50) * np.outer(np.ones(np.size(u)), np.cos(v))
return(ax.plot_surface(x, y, z, rstride = 4, cstride = 4))
I have seen animations (such as the one here: https://pythonmatplotlibtips.blogspot.com/2018/11/animation-3d-surface-plot-funcanimation-matplotlib.html) that allow you to animate 3D plots in which z is defined as a function of x, y. However, in the case of the shrinking ellipsoid, I need to use spherical coordinates, which complicates things.
Can someone explain what to add to my code to go from static to the desired shrinking animation?

Related

I have a problem with plotting sphere and a curve on it

I am trying to plot a curve on a sphere but I can not plot them at the same time. I identified some points with Euclidean norm 10 for my curve, and some other points to plot the sphere of radius 10, respectively as following.
Points for curve:
random_numbers=[]
basevalues=np.linspace(-0.9,0.9,100)
for i in range(len(basevalues)):
t=random.random()
random_numbers.append(t*10)
xvalues=[random_numbers[i]*np.cos(basevalues[i]) for i in range(len(basevalues))]
yvalues=[random_numbers[i]*np.sin(basevalues[i]) for i in range(len(basevalues))]
zvalues=[np.sqrt(100-xvalues[i]**2-yvalues[i]**2)for i in range(len(basevalues))]
Where xvalues, yvalues and zvalues are our points Euclidean components.
Points for sphere:
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))
Where x,y and z are Euclidean components of sphere points.
My problem:
When I try to plot the curve, without plotting sphere, it works. But when I plot them together, then it just return the sphere.
The whole code is the following:
import matplotlib.pyplot as plt
import numpy as np
import random
#Curve points
random_numbers=[]
basevalues=np.linspace(-0.9,0.9,100)
for i in range(len(basevalues)):
t=random.random()
random_numbers.append(t*10)
xvalues=[random_numbers[i]*np.cos(basevalues[i]) for i in range(len(basevalues))]
yvalues=[random_numbers[i]*np.sin(basevalues[i]) for i in range(len(basevalues))]
zvalues=[np.sqrt(100-xvalues[i]**2-yvalues[i]**2)for i in range(len(basevalues))]
# Sphere points
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))
# Plot the surface and curve
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
circ = ax.plot(xvalues,yvalues,zvalues, color='green',linewidth=1)
sphere=ax.plot_surface(x, y, z, color='r')
ax.set_zlim(-10, 10)
plt.xlabel("X axes")
plt.ylabel("Y axes")
plt.show()
What I want to occur:
I would like to plot the curve on the sphere, but it dose not happen in my code. I appreciate any hint.
If you use a "." option for plotting the points, like
circ = ax.plot(xvalues, yvalues,zvalues, '.', color='green', linewidth=1)
you will see the points on top of the sphere for certain viewing angles, but disappear sometimes even if they are in front of the sphere. This is a known bug explained in the matplotlib documentation:
My 3D plot doesn’t look right at certain viewing angles:
This is probably the most commonly reported issue with mplot3d. The problem is that – from some viewing angles – a 3D object would appear in front of another object, even though it is physically behind it. This can result in plots that do not look “physically correct.”
In the same doc, the developers recommend to use Mayavi for more advanced use of 3D plots in Python.
Using spherical coordinates, you can easily do that:
## plot a circle on the sphere using spherical coordinate.
import numpy as np
import matplotlib.pyplot as plt
# a complete sphere
R = 10
theta = np.linspace(0, 2 * np.pi, 1000)
phi = np.linspace(0, np.pi, 1000)
x_sphere = R * np.outer(np.cos(theta), np.sin(phi))
y_sphere = R * np.outer(np.sin(theta), np.sin(phi))
z_sphere = R * np.outer(np.ones(np.size(theta)), np.cos(phi))
# a complete circle on the sphere
x_circle = R * np.sin(theta)
y_circle = R * np.cos(theta)
# 3d plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(x_sphere, y_sphere, z_sphere, color='blue', alpha=0.2)
ax.plot(x_circle, y_circle, 0, color='green')
plt.show()

How to plot 3D sphere alongside 2D disk with matplotlib [duplicate]

This question already has answers here:
Trying to add a 3d subplot to a matplotlib figure
(3 answers)
Multiple 3D plots in one window
(1 answer)
Closed 3 years ago.
I have coordinates inside a sphere and I want to plot the whole 3D sphere as scattered points, but alongside I would also like to plot a 2D disk.
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(font_scale=1)
sns.set_style("whitegrid")
from matplotlib import rc
rc('text', usetex=True)
rc('font', family='serif')
from mpl_toolkits.mplot3d import Axes3D
fig, (ax1, ax3) = plt.subplots(1, 2, figsize=(900 / 106, 600 / 106), constrained_layout = False)
# 2D
radius = np.random.uniform(size=5000)
phi = np.random.uniform(size=5000) * 2 * np.pi
x = radius * np.cos(phi)
y = radius * np.sin(phi)
plot = ax1.scatter(x, y, s = 10, marker='.')
ax1.set_xlabel('$x$')
ax1.set_ylabel('$y$')
ax1.set_aspect('equal')
# 2D
radius = np.random.uniform(size=5000)
phi = np.random.uniform(size=5000) * 2 * np.pi
alfa = np.random.uniform(size=5000) * np.pi
x = radius * np.cos(phi) * np.sin(alfa)
y = radius * np.sin(phi) * np.sin(alfa)
z = radius * np.cos(alfa)
ax3 = Axes3D(fig)
plot = ax3.scatter(x, y, z, s = 1, marker='.')
ax3.set_xlabel('$x$')
ax3.set_ylabel('$y$')
ax3.set_zlabel('$z$')
#ax3.view_init(30, 240)
ax3.set_aspect('equal', 'box')
#fig.colorbar(plot, shrink = 0.9, ticks = np.linspace(0, 1, 6), ax = ax3)
fig.tight_layout()
Now the problem with this code above is that the output is not nearly as what I would expect (the 3D is plotted on top).
Any ideas how to do this properly?

Parametric 3D Surface Plot with color depending on (x,y,z)

I want to plot a quantity which is given on a parametric surface in 3d space (for example the temperature distribution on a sphere). I can plot a parametric 3D plot of the sphere (as a function of the two parameters phi and theta) but I don't know how to make the colors of the polygons making up the sphere depend on the parameters theta and phi (normally, the color of a polygon is simply determined by the z-Position of the polygon).
Here's a basic example which plots a torus with colormap:
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
angle = np.linspace(0, 2 * np.pi, 32)
theta, phi = np.meshgrid(angle, angle)
r, R = .25, 1.
X = (R + r * np.cos(phi)) * np.cos(theta)
Y = (R + r * np.cos(phi)) * np.sin(theta)
Z = r * np.sin(phi)
# Display the mesh
fig = plt.figure()
ax = fig.gca(projection = '3d')
ax.set_xlim3d(-1, 1)
ax.set_ylim3d(-1, 1)
ax.set_zlim3d(-1, 1)
ax.plot_surface(X, Y, Z, rstride = 1, cstride = 1,cmap="hot")
plt.show()
However, the colors of the files are given by the z position of the tile, I want the color to be given by a function f(x,y).
Does anyone know how I can achieve this dependency in Matplotlib?
Thanks very much!
Ok, if anyone else is looking for a solution to this problem here's a possible solution:
The colors of the individual faces making up the surface plot can be set using the keyword argument facecolors. The following code will use the function X**2+Y**2 for coloring the faces of the parametric surface:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.colors as mcolors
from matplotlib import cm
import matplotlib.pyplot as plt
import numpy as np
# Generate torus mesh
angle = np.linspace(0, 2 * np.pi, 32)
theta, phi = np.meshgrid(angle, angle)
r, R = .25, 1.
X = (R + r * np.cos(phi)) * np.cos(theta)
Y = (R + r * np.cos(phi)) * np.sin(theta)
Z = r * np.sin(phi)
colorfunction=(X**2+Y**2)
norm=mcolors.Normalize(colorfunction.min(),colorfunction.max())
# Display the mesh
fig = plt.figure(figsize=(7, 7))
ax = fig.add_subplot(projection='3d')
ax.set_xlim3d(-1, 1)
ax.set_ylim3d(-1, 1)
ax.set_zlim3d(-1, 1)
ax.plot_surface(X, Y, Z, rstride = 1, cstride = 1, facecolors=cm.jet(norm(colorfunction)))
plt.show()

Python 2D circular surface in 3D

I am trying to generate the top/bottom of a cylindrical surface. I was able to obtain the lateral surface here: Generating a Cylindrical Surface with np.outer. I would like to use np.outer again for consistency. I thought I understood the answers in the link however if I understood correctly then the following should work:
R = 5
h = 5
u = np.linspace(0, 2*np.pi, 100)
x = R * np.outer(np.ones(np.size(u)), np.cos(u))
y = R * np.outer(np.ones(np.size(u)), np.sin(u))
z = h * np.outer(np.ones(np.size(u)), np.ones(np.size(u)))
however in my plots, no surface is generated. Am I still not using np.outer correctly? Why is no surface generated?
There is no visible disk because all the points which you are creating have exactly the same distance to the center and surface which spans between "inner circle" and "outer circle" is infinitely thin. In order to see the disk, radius needs to vary between 0 and your desired value (5 in the example).
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
R = np.linspace(0, 5, 100)
h = 5
u = np.linspace(0, 2*np.pi, 100)
x = np.outer(R, np.cos(u))
y = np.outer(R, np.sin(u))
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(x,y,h) # z in case of disk which is parallel to XY plane is constant and you can directly use h
fig.show()

Making Random 3-D Shapes in Python

How can I use Python to generate a bunch of spheres and ellipses in one plot? Ideally it would just entail setting the endpoints (or radii/axes) of each object and a color, like how you can easily generate rectangles/circles using endpoints.
I was imagining using something like matplotlib's 3-D module, where you can rotate & play with the plot once it's outputted. I'm open to using other libraries though!
I could possibly plot the equations as surfaces by manipulating & graphing a bunch of ellipsoid equations, but is there an easier solution?
VPython might be the quickest path to getting some spheres and ellipsoids on the screen. Also, VPython is much more interactive than matplotlib (in the sense that you can rotate, zoom, etc), and it's very easy to get started. In the end, it depends on what you're looking for. There are lots of ways to get spheres and ellipsoids on the screen.
from visual import *
myell = ellipsoid(pos=(x0,y0,z0), length=L, height=H, width=W)
ball = sphere(pos=(1,2,1), radius=0.5)
Were you looking for functionality that isn't included in matplotlib's mpl_toolkits.mplot3d module? From the 3D Surface demo:
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()
I don't see any reason why you couldn't define another shape in the same field:
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
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))
x1 = 7 + 10 * np.outer(np.cos(u), np.sin(v))
y1 = 7 + 10 * np.outer(np.sin(u), np.sin(v))
z1 = 7 + 10 * np.outer(np.ones(np.size(u)), np.cos(v))
ax.plot_surface(x, y, z, rstride=4, cstride=4, color='b')
ax.plot_surface(x1, y1, z1, rstride=4, cstride=4, cmap=cm.coolwarm)
plt.show()

Categories

Resources