1

I want to add constant x, y, z lines into a matplotlib 3D scatter plot in Python which extended from this limit point, may I know how could I do so?

x_limit = [-0.5]
y_limit = [151]
z_limit = [1090]

Example code:

import matplotlib.pyplot as plt        
from mpl_toolkits.mplot3d import Axes3D        
import numpy as np     
import pandas as pd   
from scipy.stats import multivariate_normal

fig = plt.figure(figsize=(8,8)) # size 4 inches X 4 inches     
ax = fig.add_subplot(111, projection='3d') 

np.random.seed(42)     
xs = np.random.random(100)*-0.8   
ys = np.random.random(100)*300   
zs = np.random.random(100)*10500   

plot = ax.scatter(xs,ys,zs)   

ax.set_title("3D plot with limit")    
ax.set_xlabel("x")   
ax.set_ylabel("y")    
ax.set_zlabel("z")    

x_limit = [-0.5]    
y_limit = [151]    
z_limit = [1090]

ax.scatter(x_limit, y_limit, z_limit, c = 'g', marker = "s", s = 50)    

plt.show()
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
hy0916
  • 11
  • 3

1 Answers1

0

This code should do the trick. There are a couple important things to note though. First, the mesh grid created only worked because each of the three axes share the same limits. Second, while the x_limit and y_limit values work as the X and Y arguments it appears that the Z argument is expected to be of a higher dimensionality (hence why I used full_like to fill an array of the same shape as x_1 and x_2 with the Z limit).

x_1, x_2 = np.meshgrid(np.arange(0, 1.1, 0.1), np.arange(0, 1.1, 0.1))
ax.plot_surface(x_limit, x_1, x_2, color='r', alpha=0.5)
ax.plot_surface(x_1, y_limit, x_2, color='g', alpha=0.5)
ax.plot_surface(x_1, x_2, np.full_like(x_1, z_limit), color='b', alpha=0.5)
Jack Haas
  • 66
  • 4
  • 1
    Thanks Jack for your answer! May I know what if the there axes in reality do no share the same limits? I edited my question to be more representable of my real life use case by changing the axis scale, could help to take a look again? thank you:) – hy0916 May 20 '22 at 00:20
  • @hy0916 You would then just need to create a 3 dimensional meshgrid and keep track of each of the specific dimensions like they do [here](https://stackoverflow.com/questions/1827489/numpy-meshgrid-in-3d). Then when plotting the three different surfaces you would need to use the two dimensions returned from the mesh grid along with the constant you are trying to plot. For example, with x, y, and z as the returns of the linked example you would want to do something like `ax.plot_surface( x, y, np.full_like(x, z_limit), color='b', alpha=0.5)` – Jack Haas May 20 '22 at 15:17