按照 comments 是@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
,以便在's'
标记内插入'x'
标记以用于重叠值.
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
的倒数 Select 性地将facecolor
相加
fig, ax = plt.subplots()
# selectively plot red squares
ax.scatter(x[~idx_bool], y[~idx_bool], facecolor='r', edgecolor='k', s=70, marker='s')
# use idx_bool on x and y
ax.scatter(x[idx_bool], y[idx_bool], facecolor='none', edgecolor='k', s=70, marker='s') # remove this line if you don't want any squares on the True values
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()
go 掉了ax.scatter(x[idx_bool], y[idx_bool], facecolor='none', edgecolor='k', s=70, marker='s')
个.