0

While plotting an ellipsoid using axes3D, I met with an error

TypeError: unbound method plot() must be called with Axes3D instance as first argument (got ndarray instance instead)

I need to plot the ellipsoid with random number of points inside. SO i used random module. But I couldn't identify the reason for such an error. The program is given below.

import random
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import *

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
n = 1000000
a = input("Enter the value of semi major axis: \n")
b = input("Enter the value of semi minor axis: \n")
c = input("Enter the value of c \n")
x = random.uniform(-a, a, n)
y = random.uniform(-b, b, n)
z = random.uniform(-c, c, n)
r = (x ** 2 / a ** 2) + (y ** 2 / b ** 2) + (z ** 2 / c ** 2)
rd = r[:] <= 1
xd = x[rd]
yd = y[rd]
zd = z[rd]
Axes3D.plot3D(xd, yd, zd, "*")
plot.show()

May be there some errors. I am a beginner and please help me.

2
  • please post your actual code, what you posted fails on random.uniform(-a, a, n) Commented May 18, 2016 at 10:01
  • The error is exactly telling you what to do (not that this is the only problem). Just use ax as first param in plot3d! Commented May 18, 2016 at 15:09

1 Answer 1

1

Your import random cannot work, leading to problems even before you try to plot your cloud of points, because random.uniform has a different signature from what you use.

I suppose that the real statement you use is from numpy import random ...

Further, the standard way (and there is a reason) to import the 3d capabilities is from mpl_toolkits.mplot3d import Axes3D — this modifies the definition of the axes object, and that's all you need.

Finally, you use the .plot() method when what you really need is .scatter(), that doesn't draw the lines connecting the individual points.

My version of what you're trying to accomplish is as follows (note that I use the modified ax object, not Axes3D ...)

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from numpy.random import uniform

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

n, a, b, c = 2000, 10.0, 6.0, 20.0
x, y, z = [uniform(-d, d, n) for d in (a, b, c)]
inside =  ((x/a)**2 + (y/b)**2 + (z/c)**2) <= 1.0

ax.scatter(x[inside], y[inside], z[inside])

plt.show()

example plot

Sign up to request clarification or add additional context in comments.

2 Comments

@AthulRT Glad to hear your positive feedback — I have to wonder if it would be possible that you formally accept my answer...
sorry for being late.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.