Matplotlib - Flat plane and dots in 3D at the same time

I am trying to build both a plane and some points in 3D with Matplotlib at the same time. I have no errors since the dot will not appear. I can draw several glasses and planes at different times, but not at the same time. Part of the code is as follows:

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() 
+5
source share
2 answers

You will need to specify the axes that you want new plots to add to the current plots along the axes, rather than overwriting them. For this you will need to use axes.hold(True)

 # plot the surface plt3d = plt.figure().gca(projection='3d') plt3d.plot_surface(xx, yy, z, alpha=0.2) # Ensure that the next plot doesn't overwrite the first plot ax = plt.gca() ax.hold(True) ax.scatter(points2[0], point2[1], point2[2], color='green') 

enter image description here

UPDATE

As @tcaswell noted in the comments, they are considering discontinuing hold support. As a result, the best approach might be to use the axes directly to add additional graphs, as in @tom answer.

+7
source

To add @suever to the answer, you have no reason why you cannot create Axes , and then draw both the surface and the scatter points on it. Then there is 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') 
+6
source

Source: https://habr.com/ru/post/1245231/


All Articles