Home > OS >  How to print two figures together in matplotlib.pyplot so that they form a complete figure
How to print two figures together in matplotlib.pyplot so that they form a complete figure

Time:03-25

I am trying to generate a 3d ellipsoid in matplotlib using the method matplotlib.pyplot.add_subplot(211,projection='3d').

Firstly, I generate the upper side of the ellipsoid using this code:

import matplotlib.pyplot as plt
fig1=plt.figure(figsize=(20,20))
ax=fig1.add_subplot(211,projection='3d')
import numpy as np
Xval=np.linspace(-5,5,50)
Yval=np.linspace(-5,5,50)
X,Y=np.meshgrid(Xval,Yval)
def height(x,y):
    return 100-(x**2/4 y**2/9)


Z=np.sqrt(height(X,Y))

ax.plot_wireframe(X,Y,Z)

Secondly, when I tried to generate the lower side of the ellipsoid, I could not show the two pictures together to form a complete ellipsoid and it messes up. The Z vector is as follows for the lower side.

Z=-np.sqrt(height(X,Y))

I would appreciate any tip. I know that this is possible in 2d figures.

CodePudding user response:

Your ellipsoid is way too large for the data ranges you define, that's why you get two "planes" when you plot the two sides together. With

Xval=np.linspace(-30,30,300)
Yval=np.linspace(-30,30,300)
...
ax.plot_wireframe(X,Y,np.sqrt(height(X,Y)))
ax.plot_wireframe(X,Y,-np.sqrt(height(X,Y)))

it shows as

enter image description here

Note that this still lacks the connections between the halves. That's because you would need to include the exact x, y coordinates which give z=0.

  • Related