# Why does my code to plot a cone actually generate a roof?

per this post, this formula

$$Z = 1 - \sqrt{X^{2} + Y^{2}}$$

generates a cone where the point is at (0, 0, 1) and it spreads out below that. It meets the x-y plane at the unit circle

I am trying to reproduce with Python

ax = plt.figure().gca(projection='3d')
xx, yy = np.meshgrid(np.arange(-1,1.1,.1), np.arange(-1,1.1,.1))
zz = 1 - np.sqrt(xx**2, yy**2)
ax.plot_surface(xx, yy, zz, alpha=.5)


and get this "roof"

each of xx, yy, zz is a 21 by 21 matrix, even if I increase them to 210 by 210, nothing changes, what am I missing?

• Looks like your mesh is undersampled, in that it only has 6 vertices so its an approximate cone.. Can you increase the resolution some how ? Oct 15 '19 at 2:01
• @PaulHK each of xx, yy, zz is a 21 by 21 matrix, even if I increase them to 210 by 210, nothing changes Oct 15 '19 at 2:13
• What is np.sqrt(xx**2, yy**2)? I would expect something like np.sqrt(xx**2 + yy**2). Otherwise you're getting back an array of square roots, right? Oct 15 '19 at 2:21

You need to change the call to sqrt from:

np.sqrt(xx**2, yy**2)


to:

np.sqrt(xx**2 + yy**2)


Otherwise you're passing an array of values to the function and will be returned an array of values for the answer.

See here for details:

>>> np.sqrt([1,4,9])

array([ 1., 2., 3.])