python - Aign 二维散点图(并链接它们?)

标签 python r matplotlib ggplot2 seurat

我偶尔会看到一些图表,其中两个二维散点图在 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 中构建此类图像?

最佳答案

我没有在 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

关于python - Aign 二维散点图(并链接它们?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55584310/

相关文章:

python - 在没有 apt-get 的 Linux (Ubuntu) 上运行 OpenCV

python - Numpy - 将 2D 数组 reshape 并分区为 3D

r - 在 R 中没有重复组星座的子组中对多个个体进行采样

r - 在饼图上放置标签 ggplot2

python - 使用 plot_confusion_matrix 绘制多个混淆矩阵

python - 如何使 Matplotlib 散点图作为一个整体透明?

python - 程序立即终止,忽略守护线程中的 time.sleep()

r - 将日期(不同格式)转换为 r 中的日期

matplotlib使用相对坐标散点图?

python - 当python脚本失败时,如何停止执行bash脚本?