Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib - Plot a plane and points in 3D simultaneously

I m trying to plot simultaneously a plane and some points in 3D with Matplotlib. I have no errors just the point will not appear. I can plot at different times some points and planes but never at same time. The part of the code looks like :

import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D  point  = np.array([1, 2, 3]) normal = np.array([1, 1, 2])  point2 = np.array([10, 50, 50])  # a plane is a*x+b*y+c*z+d=0 # [a,b,c] is the normal. Thus, we have to calculate # d and we're set d = -point.dot(normal)  # create x,y xx, yy = np.meshgrid(range(10), range(10))  # calculate corresponding z z = (-normal[0] * xx - normal[1] * yy - d) * 1. /normal[2]  # plot the surface plt3d = plt.figure().gca(projection='3d') plt3d.plot_surface(xx, yy, z, alpha=0.2)   #and i would like to plot this point :  ax.scatter(point2[0] , point2[1] , point2[2],  color='green')  plt.show() 
like image 431
user3601754 Avatar asked Mar 17 '16 12:03

user3601754


People also ask

Can matplotlib Pyplot be used to display 3D plots?

Matplotlib was introduced keeping in mind, only two-dimensional plotting. But at the time when the release of 1.0 occurred, the 3d utilities were developed upon the 2d and thus, we have 3d implementation of data available today! The 3d plots are enabled by importing the mplot3d toolkit.


1 Answers

Just to add to @suever's answer, you there's no reason why you can't create the Axes and then plot both the surface and the scatter points on it. Then there's no need to use ax.hold():

# Create the figure fig = plt.figure()  # Add an axes ax = fig.add_subplot(111,projection='3d')  # plot the surface ax.plot_surface(xx, yy, z, alpha=0.2)  # and plot the point  ax.scatter(point2[0] , point2[1] , point2[2],  color='green') 
like image 175
tmdavison Avatar answered Sep 20 '22 03:09

tmdavison