首页 > 解决方案 > 将点连接到 matplotlib 散点图中的中心

问题描述

我是 3 维框架中的散点;你怎么可能通过一个段(或向量)将这些点中的每一个连接到框架的中心(0,0,0)?这是使用的代码:

from numpy import random
import matplotlib as mpl
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d 

N = 10
coords = random.normal(loc = 0, scale = 1, size = (N, 3))

x = coords[:, 0]
y = coords[:, 1]
z = coords[:, 2]

fig = plt.figure(figsize=(12,12))
ax = plt.axes(projection ="3d") 

ax.scatter3D(x, y, z, color = "green", s = 300)

这是获得的情节:

在此处输入图像描述

我想得到的是:

在此处输入图像描述

标签: python-3.xmatplotlibscatter-plot

解决方案


一种选择是遍历每个点,并使用plot3D从 (0, 0, 0) 到该点绘制一条线。

from numpy import random
import matplotlib as mpl
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d 

N = 10
coords = random.normal(loc = 0, scale = 1, size = (N, 3))

x = coords[:, 0]
y = coords[:, 1]
z = coords[:, 2]

fig = plt.figure(figsize=(12,12))
ax = plt.axes(projection ="3d") 

for xx, yy, zz in zip(x, y, z):
    ax.plot3D([0, xx], [0, yy], [0, zz], color = "blue")

ax.scatter3D(x, y, z, color = "green", s = 300)

plt.show()

在此处输入图像描述


推荐阅读