将常数x,y,z线添加到3D散点图中

发布于 2025-01-30 06:33:28 字数 923 浏览 3 评论 0原文

我想将常数x,y,z线添加到python中的matplotlib 3D散点图中,该图从这个限制点延伸,我可以知道我该怎么做吗?

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

示例代码:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痞味浪人 2025-02-06 06:33:28

此代码应该解决问题。有几件重要的事情要注意。首先,创建的网格仅起作用,因为三个轴中的每个轴都具有相同的限制。其次,当x_limit和y_limit值以xy参数为z参数时,预计有望具有更高的维度(因此,为什么我使用full_like填充与x_1x_2的数组,并具有z限制)。

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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文