Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading