我有两个数组,我需要在考虑它们成员的情况下在散点图中使用它们.例如,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是否在Anp.isin(A, B)之间,但我有两个问题:

  1. 没有反映A的形状的网格(在右侧有一个额外的列)
  2. True值必须是带黑色边缘且大小和宽度与红色相同的实心x 'X'

enter image description here

你对如何做到这一点有什么 idea 吗?

推荐答案

按照 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)创建一个布尔数组,该数组可用于索引xy,以便在'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()

enter image description here

使用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()

enter image description here

go 掉了ax.scatter(x[idx_bool], y[idx_bool], facecolor='none', edgecolor='k', s=70, marker='s')个.

enter image description here

Python相关问答推荐

使文本输入中的文本与标签中的文本相同

如何使用Python中的clinicalTrials.gov API获取完整结果?

运行回文查找器代码时发生错误:[类型错误:builtin_index_or_system对象不可订阅]

为什么tkinter框架没有被隐藏?

SQLGory-file包FilField不允许提供自定义文件名,自动将文件保存为未命名

try 在树叶 map 上应用覆盖磁贴

如何使用数组的最小条目拆分数组

修复mypy错误-赋值中的类型不兼容(表达式具有类型xxx,变量具有类型yyy)

删除marplotlib条形图上的底边

如何保持服务器发送的事件连接活动?

Pandas GroupBy可以分成两个盒子吗?

isinstance()在使用dill.dump和dill.load后,对列表中包含的对象失败

在Python中计算连续天数

Flask Jinja2如果语句总是计算为false&

从列表中获取n个元素,其中list [i][0]== value''

在Python中从嵌套的for循环中获取插值

Discord.py -

如何在验证文本列表时使正则表达式无序?

高效地计算数字数组中三行上三个点之间的Angular

普洛特利express 发布的人口普查数据失败