对齐2D散点图(并链接它们?)

问题描述 投票:2回答:1

我偶尔会看到图表,其中两个二维散点图在3D空间中相互叠加,以便可以链接相应的点。通常这些采用两个网络重叠的网络形式。例如:

enter image description here参考:https://satijalab.org/seurat/v3.0/pbmc3k_tutorial.html

enter image description here参考:https://image.slidesharecdn.com/2007mauricioarango-end-to-endqosviaoverlaynetworksandbandwidthon-demand-091102230540-phpapp02/95/providing-endtoend-network-qos-via-overlay-networks-and-bandwidth-ondemand-mauricio-arango-2007-5-728.jpg?cb=1257203157

我知道我可以任意添加一个共同的第三维到二维图,以获得这样的情节:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

def randrange(n, vmin, vmax):
    return (vmax - vmin)*np.random.rand(n) + vmin

n = 100

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

xs = randrange(n, 23, 32)
ys = randrange(n, 0, 100)
zs = np.append(np.repeat(1, 50), np.repeat(2, 50))

for c, m in [('r', 'o'), ('b', '^')]:
     ax.scatter(xs, ys, zs, c=c, marker = m)

enter image description here

然后连接相关的点,但我认为可能有一种更直接的方式在R或python中构建这样的图像?

python r matplotlib ggplot2 visualization
1个回答
0
投票

我在matplotlib中找不到任何简单明了的东西。一种可能的解决方案是使用箭袋:

from mpl_toolkits.mplot3d import Axes3D  # keep it for projection='3d'
import matplotlib.pyplot as plt
import random


def calculate_vectors(x0, y0, z0, x1, y1, z1):
    u = []
    v = []
    w = []
    for i, x in enumerate(x0):
        dx = x1[i] - x
        dy = y1[i] - y0[i]
        dz = z1[i] - z0[i]
        u.append(dx)
        v.append(dy)
        w.append(dz)
    return u, v, w


def make_plot():
    n = 20
    x1 = [random.randrange(23, 32, 1) for _ in range(n)]
    y1 = [random.randrange(0, 100, 1) for _ in range(n)]
    z1 = [1.0 for _ in range(n)]

    x2 = [random.randrange(23, 32, 1) for _ in range(n)]
    y2 = [random.randrange(0, 100, 1) for _ in range(n)]
    z2 = [2.0 for _ in range(n)]

    u, v, w = calculate_vectors(x1, y1, z1, x2, y2, z2)

    fig = plt.figure()
    ax = fig.gca(projection='3d')
    ax.scatter(x1, y1, z1, c='b', marker='^')
    ax.scatter(x2, y2, z2, c='r', marker='o')
    ax.quiver(x1, y1, z1, u, v, w, arrow_length_ratio=0.0)


make_plot()
plt.show()

我没有使用numpy,因为刷新矢量和sin / cos计算更有趣。这是输出:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.