如何在3D空间中连接散点? [英] How can I connect scatter points in a 3D space?

查看:84
本文介绍了如何在3D空间中连接散点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在matplotlib中将所有弧的起点和终点连接在一起.红色弧是所有四个周围绿色弧的中心.在下面,您将找到绘制平行圆弧的代码.

I want to connect the starting point and ending point of all arcs together in matplotlib. Red arc is the centre of all four surrounding green arcs. Below you'll find a code to draw parallel arcs.

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import interactive
interactive(True)


def cart2sphere(x, y, z):
    r = np.sqrt(x**2 + y**2 + z**2)
    theta = np.arccos(z, r)
    phi = np.arctan2(y, x)
    return(r, theta, phi)

def sphere2cart(r, theta, phi):
    theta = theta - np.pi/2
    x = r * np.sin(theta)* np.cos(phi)
    y = r * np.sin(theta)* np.sin(phi)
    z = r * np.cos(theta)
    return(x, y, z)

def pol2cart(rho, phi):
    x = rho * np.cos(phi)
    y = rho * np.sin(phi)
    return(x, y)

# define values 
theta = np.pi/2 # arclength in radians
radius = 10 # raduis of circle
k = 1/radius # if you want to use k instead of radius
phi = np.pi/6 # angle of circle in xy plane

radius1 = 7.5
radius2 = 12.5

# discretize for plotting
arcIndex = np.linspace(0, theta, num = 100)


X, Y, Z, = sphere2cart(radius, arcIndex, phi)

X1, Y1, Z1, = sphere2cart(radius2, arcIndex, phi)
X2, Y2, Z2, = sphere2cart(radius1, arcIndex, phi)
X3, Y3, Z3, = sphere2cart(radius1, arcIndex, phi)
X4, Y4, Z4, = sphere2cart(radius2, arcIndex, phi)


# move center or arc to xy plane
# =============================================================================
x1, y1 = pol2cart(radius, phi) #arc
X += x1
Y += y1
x2, y2 = pol2cart(radius, phi) #L1 
X1 += x2
Y1 += 1.5+y2
x3, y3 = pol2cart(radius, phi) #L2 
X2 += x3
Y2 += y3-1.5
x4, y4 = pol2cart(radius, phi) #L3 
X3 += x4
Y3 += 1.5+y4
x5, y5 = pol2cart(radius, phi) #L4 
X4 += x5
Y4 += y5-1.5
# =============================================================================

fig = plt.figure()
ax = fig.gca(projection='3d')

# plot arc
ax.plot(X, Y, Z, c= "red", label='arc')
ax.plot(X1, Y1, Z1, c= "Green", label='L1')
ax.plot(X2, Y2, Z2, c= "Green", label='L2')
ax.plot(X3, Y3, Z3, c= "Green", label='L3')
ax.plot(X4, Y4, Z4, c= "Green", label='L4')


# plot axes
ax.plot(np.zeros(100), np.zeros(100), np.linspace(-np.max(np.abs(Z)), np.max(np.abs(Z)), 100), c= "black", alpha = 0.15)
ax.plot(np.zeros(100), np.linspace(-np.max(np.abs(Z)), np.max(np.abs(Z)), 100), np.zeros(100),  c= "black", alpha = 0.15)
ax.plot(np.linspace(-np.max(np.abs(Z)), np.max(np.abs(Z)), 100), np.zeros(100), np.zeros(100),  c= "black", alpha = 0.15)

# plot center of circle
ax.scatter(np.array([x1]), np.array([y1]), np.array([0]), c = 'Green', label = "center of circle", s=10)

# plot endpoint
# =============================================================================
ax.scatter(X[0], Y[0], Z[0], color="red", label= "origin", s=30)
ax.scatter(X[-1], Y[-1], Z[-1], color= 'red', label = "endpoint", s=30)
ax.scatter(X1[-1], Y1[-1], Z1[-1], color= 'red', s=30)
ax.scatter(X2[-1], Y2[-1], Z2[-1], color= 'red', s=30)
ax.scatter(X3[-1], Y3[-1], Z3[-1], color= 'red', s=30)
ax.scatter(X4[-1], Y4[-1], Z4[-1], color= 'red', s=30)
ax.scatter(X1[1], Y1[1], Z1[1], color= 'red', s=30)
ax.scatter(X2[1], Y2[1], Z2[1], color= 'red', s=30)
ax.scatter(X3[1], Y3[1], Z3[1], color= 'red', s=30)
ax.scatter(X4[1], Y4[1], Z4[1], color= 'red', s=30)
# =============================================================================

# plot projection on each axis
ax.plot(X, np.zeros(len(X)), np.zeros(len(X)), color = "blue", label = "X projection")
ax.plot(np.zeros(len(X)), Y, np.zeros(len(X)), color = "green", label = "Y projection")
ax.plot(np.zeros(len(X)), np.zeros(len(X)), Z, color = "brown", label = "Z projection")

ax.legend()

我从下面的代码中得到的结果,

Result that I got from this code shown below,

我正在尝试将所有这些红色散点连接在一起,以便它们可以在圆弧的起点和终点形成一个正方形.我想要的东西类似于下图所示,

I'm trying to connect all these red scatter points together such that they can make a square at the starting and ending of the arc. What I want is something just like the figure shown below,

如何将空间中的每个点与下一个点连接起来,以使其成为平滑的线形图?

How do I connect each point in the space with the next one to make it a smooth line graph?

推荐答案

我建议完全不要使用Matplotlib进行3d绘图.您迟早会碰壁.即使周围有漂亮的图,它也不能很好地工作,尤其是当您要可视化具有多个数据点的散点图时.如果要绘制彩色的三角形网格或复杂的曲面,将无法确定.

I recommend to not use Matplotlib for 3d plotting at all. You will hit a wall sooner or later. Even though there are cool looking plots around, it does not work well especially if you want to visualize scatter plots with many data points. And it will fail for sure if you want to plot colorized triangular meshes or complex surfaces.

相反,请使用以下工具箱之一:

Instead use one of the following toolkits:

  • https://docs.enthought.com/mayavi/mayavi/ (3d, fast, nice)
  • http://www.pyqtgraph.org/ (2d/3d, very fast)

可以找到一个很好的示例,展示了用于多个实时图的不同方法,

A very good example showing different approaches for multiple real time plots can be found here

https://plot.ly/python/3d-surface-plots /(2d/3d,很好)

https://plot.ly/python/3d-surface-plots/ (2d/3d, nice)

http://vispy.org/documentation.html (2d/3d,非常快,很好,需要OpenGL)

http://vispy.org/documentation.html (2d/3d, very fast, nice, requires OpenGL)

可能是未来,请检查高级功能 vispy.plot() vispy.scene(),我想他们取得了一些进展)

Might be the future, check the high-level functions vispy.plot() and vispy.scene(), I guess they made some progress)

有关示例,请参见此处,寻找带有场景".似乎也有一些界面可以使用Vispy来渲染Matplotlib图.

For examples see here, look for the ones with "scene". There also seems to be some interface to use Vispy to render Matplotlib plots.

这篇关于如何在3D空间中连接散点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆