Samyukta Ramnath
Samyukta Ramnath

Reputation: 383

Plot a 3D cone with vertex, height and angle information, along a given vector, matplotlib

How do I plot a cone in 3 dimensions when I know the coordinates of the vertex, the vector along which the cone lies (as a line segment whose end point coordinates are known), and the height of the cone, in matplotlib or any other python plotting module?

I guess what would be most helpful, would be some instruction on plotting implicit functions in matplotlib.

What I have tried, is to create a grid of points (x,y, and z), define an equation of the cone, and check whether each of those grid points satisfies the equation of the cone (since grid resolution may mean that it won't exactly satisfy the equation, I check whether it satisfies the equation upto some accuracy). However, I am not sure now how to plot this implicit data set in matplotlib. I have tried writing out the equation (which involves quadratic and linear terms in x,y and z) as a quadratic in z, solving for z and performing a surface plot with z in terms of x and y, but that gave me a plane instead of a cone.

Here is the code I used:

    x = np.linspace(0,100,100)
    y = np.linspace(0,100,100)


    z = (-4823.7 + np.sqrt(abs((4823.7**2) - (4*(-240.185)*((-5692*y) - (240.185*np.multiply(x,x)) - (240.185*np.multiply(y,y)) - 57607.26 )) ))/np.float(2*(-240.185)))
   #print z
   fig = plt.figure()
   ax = fig.add_subplot(111, projection='3d')
   x = np.arange(-50.0, 50, 1)
   y = np.arange(-50.0, 50, 1)
   X, Y = np.meshgrid(x, y)
   ax.plot_surface(X, Y, z)

   ax.set_xlabel('X Label')
   ax.set_ylabel('Y Label')
   ax.set_zlabel('Z Label')
   plt.show()

Upvotes: 1

Views: 3101

Answers (1)

eyllanesc
eyllanesc

Reputation: 244093

try with it:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = np.arange(-50.0, 50, 1)
y = np.arange(-50.0, 50, 1)
X, Y = np.meshgrid(x, y)
Z = (-4823.7 + np.sqrt(abs((4823.7 ** 2) - (4 * (-240.185) * ((-5692 * Y) - (240.185 * np.multiply(X, X))
                                                              - (240.185 * np.multiply(Y, Y)) - 57607.26)))) / np.float(
    2 * (-240.185)))

ax.plot_surface(X, Y, Z)

ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()

enter image description here

Upvotes: 1

Related Questions