【问题标题】:Aign 2D scatter plots (and link them?)对齐二维散点图(并链接它们?)
【发布时间】:2019-08-30 05:53:09
【问题描述】:

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

参考:https://satijalab.org/seurat/v3.0/pbmc3k_tutorial.html

参考: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)

然后连接相关点,但我认为可能有更直接的方法可以在 R 或 python 中构建此类图像?

【问题讨论】:

  • 如何运行您的脚本?是同一个函数调用“random.randrange(start, stop[, step])”吗?那么 start 大于 stop。
  • @AlexanderLopatin 我已在randrange() 函数中添加。

标签: python r matplotlib ggplot2 seurat


【解决方案1】:

我没有在 matplotlib 中找到任何简单的东西。一种可能的解决方案是使用 quiver:

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 计算更有趣。这是输出:

【讨论】:

    猜你喜欢
    • 2015-08-02
    • 1970-01-01
    • 2017-01-21
    • 1970-01-01
    • 2015-01-17
    • 2020-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多