如何散点绘制不同形状数组的相交值

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

我有两个数组,我需要在散点图中使用它们,并考虑它们的成员资格。例如,

B
的第一行位于
A
的第二行,从第 2 列到第 3 列。

#A
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15],
       [16, 17, 18, 19]])

#B
array([[ 5,  6],
       [12, 13],
       [16, 17]])

我编写了以下代码:

import numpy as np
import matplotlib.pyplot as plt

A = np.arange(20).reshape(5, 4)
B = np.array([[5, 6], [12, 13], [16, 17]])
x, y = np.meshgrid(range(A.shape[0]), range(A.shape[1]))

fig, ax = plt.subplots()
ax.scatter(x, y, facecolor='none', edgecolor='k', s=70, marker='s')

for ix, iy, a in zip(x.ravel(), y.ravel(), A.ravel()):
    plt.annotate(a, (ix,iy), textcoords='offset points', xytext=(0,7), ha='center', fontsize=14)

plt.axis("off")
ax.invert_yaxis()

plt.show()

现在,我可以用

B
检查
A
是否在
np.isin(A, B)
中,但我有两个问题:

  1. 不反映
    A
    形状的网格(就像右边多了一个列)
  2. 真实值必须是填充的 x
    'X'
    ,具有黑色边缘,并且大小和宽度与红色相同

您对如何做到这一点有什么想法吗?

python numpy matplotlib scatter-plot intersection
2个回答
1
投票

根据@chrslg的评论,

x, y = np.meshgrid(range(A.shape[1]), range(A.shape[0]))
而不是
x, y = np.meshgrid(range(A.shape[0]), range(A.shape[1]))

np.isin(A, B)
创建一个布尔数组,可用于索引
x
y
以在
'x'
标记内插入
's'
标记,以实现重叠值。

np.isin(A, B)
array([[False, False, False, False],
       [False,  True,  True, False],
       [False, False, False, False],
       [ True,  True, False, False],
       [ True,  True, False, False]])
import numpy as np
import matplotlib.pyplot as plt

A = np.arange(20).reshape(5, 4)
B = np.array([[5, 6], [12, 13], [16, 17]])

# reversed 1 and 0 on this line
x, y = np.meshgrid(range(A.shape[1]), range(A.shape[0]))

# create a Boolean of overlapping values
idx_bool = np.isin(A, B)

fig, ax = plt.subplots()
ax.scatter(x, y, facecolor='r', edgecolor='k', s=70, marker='s')

# use idx_bool to on x and y
ax.scatter(x[idx_bool], y[idx_bool], facecolor='k', s=70, marker='x')

for ix, iy, a in zip(x.ravel(), y.ravel(), A.ravel()):
    plt.annotate(a, (ix,iy), textcoords='offset points', xytext=(0,7), ha='center', fontsize=14)

plt.axis("off")
ax.invert_yaxis()

plt.show()

使用

idx_bool
的逆来选择性地添加
facecolor

fig, ax = plt.subplots()
ax.scatter(x[~idx_bool], y[~idx_bool], facecolor='r', edgecolor='k', s=70, marker='s')

# use idx_bool to on x and y
ax.scatter(x[idx_bool], y[idx_bool], facecolor='none', edgecolor='k', s=70, marker='s')
ax.scatter(x[idx_bool], y[idx_bool], facecolor='k', s=70, marker='x')

for ix, iy, a in zip(x.ravel(), y.ravel(), A.ravel()):
    plt.annotate(a, (ix,iy), textcoords='offset points', xytext=(0,7), ha='center', fontsize=14)

plt.axis("off")
ax.invert_yaxis()

plt.show()


1
投票

我希望这能有所帮助:

import numpy as np
import matplotlib.pyplot as plt

A = np.arange(20).reshape(4, 5)

Ax = np.arange(4)
Ay = np.arange(5)
Bx = np.array([0, 1, 0, 1, 1, 2])
By = np.array([4, 4, 3, 3, 1, 1])

x, y = np.meshgrid(Ax, Ay)

fig, ax = plt.subplots()
ax.scatter(x, y, facecolor='r', edgecolor='k', s=70, marker='s')
ax.scatter(Bx, By, facecolor='k', edgecolor='k', s=70, marker='x')

for ix, iy, a in zip(x.ravel(), y.ravel(), A.ravel()):
    plt.annotate(a, (ix,iy), textcoords='offset points', xytext=(0,7), ha='center', fontsize=14)

plt.axis("off")
ax.invert_yaxis()
plt.show()

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