hy0916
hy0916

Reputation: 11

Add constant x, y, z lines into 3D scatter plot

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()

Upvotes: 1

Views: 641

Answers (1)

Jack Haas
Jack Haas

Reputation: 76

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)

Upvotes: 0

Related Questions