【问题标题】:Connect points to center in matplotlib scatter plot将点连接到 matplotlib 散点图中的中心
【发布时间】:2020-09-24 10:22:12
【问题描述】:

我在 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.x matplotlib scatter-plot


    【解决方案1】:

    一种选择是遍历每个点,并使用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()
    

    【讨论】:

      猜你喜欢
      • 2017-01-31
      • 2013-12-06
      • 1970-01-01
      • 2021-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-13
      相关资源
      最近更新 更多