【问题标题】:How to plot lines between multiple nodes in 3D interface using Matplotlib如何使用 Matplotlib 在 3D 界面中的多个节点之间绘制线条
【发布时间】:2021-04-19 19:45:20
【问题描述】:

我想绘制像这个图这样的节点,这些节点随机分布在一个尺寸为 10×10×10 mm 的三维隔间区域中。 如何在 Python 中做到这一点?

更多信息:这里,(*) 节点将被称为目标节点,(·) 节点将被称为源节点。使用特定的SINRmin要求评估两个节点之间的可用通信链路,并用黑色虚线绘制,称为段。这里,SINRmin 的值为 8 dB。

这是我的代码,我做了什么:

import numpy as np
import matplotlib.pyplot as plt

n = 50

x1, x2 = 0.01, 0.1
y1, y2 = 0.01, 0.1    
z1, z2 = 0.01, 0.1

xs = (x2 - x1)*np.random.rand(n) + x1
ys = (y2 - y1)*np.random.rand(n) + y1
zs = (z2 - z1)*np.random.rand(n) + z1

fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
ax.plot(xs, ys, zs, "go")
ax.plot(xs, ys, zs, "k--")
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')

plt.show()

我的输出:

【问题讨论】:

  • 这篇文章不清楚。您的问题与您成功的范围内的绘图有关,您具体要求什么?绘制方向图?看看 FancyArrowPatch

标签: python matplotlib graph 3d


【解决方案1】:

根据您的代码,此代码将演示如何设置标记的形状、标记的大小和线型。图中From Nodes为绿色圆点,To Nodes为红色星形。

import numpy as np
import matplotlib.pyplot as plt

n = 50

x1, x2 = 0.01, 0.1
y1, y2 = 0.01, 0.1    
z1, z2 = 0.01, 0.1

# (origin) nodes
xs = (x2 - x1)*np.random.rand(n) + x1
ys = (y2 - y1)*np.random.rand(n) + y1
zs = (z2 - z1)*np.random.rand(n) + z1

fig = plt.figure(figsize=(9,9))
ax = fig.add_subplot(111,projection='3d')
#ax.plot(xs, ys, zs, "go")
#ax.plot(xs, ys, zs, "k--")
#ax.plot(xs, ys, zs, 'g.--', linewidth=0.8, markersize=8)
ax.plot(xs, ys, zs, 'g.', linewidth=0.8, markersize=8)

# (destination) nodes
x1,y1,z1 = 0.2, 0.2, 0.2
xs2 = (x2 - x1)*np.random.rand(n) + x1
ys2 = (y2 - y1)*np.random.rand(n) + y1
zs2 = (z2 - z1)*np.random.rand(n) + z1
#ax.plot(xs2, ys2, zs2, 'r*--', linewidth=0.8, markersize=8)
ax.plot(xs2, ys2, zs2, 'r*', linewidth=0.8, markersize=8)

#ax.plot(xs, ys, zs, color="k", marker=".")
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')

# Plot line between (origin) from-nodes to (destination) to-nodes
# Assuming the lines apply in succeeding order between 2 groups of data
from_pts = []
for ea in np.stack((xs, ys, zs), axis=-1):
    from_pts.append(ea)

dest_pts = []
for ea2 in np.stack((xs2, ys2, zs2), axis=-1):
    dest_pts.append(ea2)

for from_to in zip(from_pts, dest_pts):
    #print("From:", from_to[0])
    #print("To:", from_to[1])
    ax.plot([from_to[0][0],from_to[1][0]], 
            [from_to[0][1],from_to[1][1]], 
            [from_to[0][2],from_to[1][2]], "b--", linewidth=0.6)
    pass

plt.show()

输出:

【讨论】:

    猜你喜欢
    • 2016-12-20
    • 2019-06-26
    • 1970-01-01
    • 1970-01-01
    • 2020-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    相关资源
    最近更新 更多