I'm trying to create a figure of particle distribution from a reference surface in python. I plan to get a distribution in python and then prettify it with tikz. I tried this:

xs = np.linspace(-1, 1, 10)
ys = np.linspace(-1, 1, 10)
X, Y = np.meshgrid(xs, ys)

Z = X
for i in range (0, 10):
  for j in range (0, 10):
    Z[i][j] = 0

xs = randomrange(10, -1, 1)
ys = randomrange(10, -1, 1)
zs = randomrange(10, 0, 10)
ax.plot_surface(X, Y, Z)
ax.scatter(xs, ys, zs)

However, I get a single line with an error message:

/usr/lib/python3.2/site-packages/mpl_toolkits/mplot3d/axes3d.py:1478: RuntimeWarning: nvalid value encountered in true_divide for n in normals])

I assume that this is due to division by 0 (trying to color the surface).

How can I generate a reference surface then?


I tried MRocklin's solution and replaced

Z = X 


Z = np.zeros_like(X)

Now I don't get any surface at all...


1 Answer 1


In the line

Z = X

You're just renaming X to Z, not making a copy. Your code then proceeds to zero out all elements of X. You might want to try replacing this with the following:

Z = X.copy()

Actually, the first bit of your code could be replaced with

X, Y = np.mgrid[-1:1:10j, -1:1:10j]
Z = np.zeros_like(X)

Here is the full Python code to produce a flat surface using the mpl toolkit mplot3d running python 2.7.2 from the EPD academic distribution 7.1-2

$ ipython --pylab
In [1]: from mpl_toolkits import mplot3d

In [2]: f = figure()

In [3]: ax = mplot3d.Axes3D(f)

In [4]: X, Y = mgrid[-1:1:10j, -1:1:10j]

In [5]: Z = zeros_like(X)

In [6]: ax.plot_surface(X,Y,Z)
/home/mrocklin/Software/epd/lib/python2.7/site-packages/mpl_toolkits/mplot3d/axes3d.py:777: RuntimeWarning: invalid value encountered in divide
  n = n / proj3d.mod(n)
Out[6]: <mpl_toolkits.mplot3d.art3d.Poly3DCollection at 0xaa13d2c>

In [7]: show()

A simple flat surface

  • $\begingroup$ Those two lines produce what I believe to be the original intent for the variables X,Y, and Z. I believe that this does cover the first two sections. The variables xs, ys, and zs are defined separately in the third section. $\endgroup$
    – MRocklin
    Jan 16, 2012 at 19:08
  • $\begingroup$ I tried this and I don't get any surface at all. $\endgroup$
    – Yotam
    Jan 16, 2012 at 20:32
  • $\begingroup$ @Yotam: When you tried it, what errors did you receive, if any? $\endgroup$
    – Paul
    Jan 16, 2012 at 22:14
  • $\begingroup$ I don't get any. I am trying to generate the surface with other particles though $\endgroup$
    – Yotam
    Jan 17, 2012 at 7:01

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.