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

时间:2019-04-09 02:35:28

标签: python r matplotlib ggplot2 visualization

我偶尔会看到一个图,其中两个二维散点图在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

我知道我可以在二维图上任意添加一个公共的3维,以获得这样的图:

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中构造此类图像?

1 个答案:

答案 0 :(得分: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