[英]How can I convert from scatter size to data coordinates in matplotlib?
I would like to programmatically test whether two scatterplot glyphs will overlap in matplotlib. 我想以编程方式测试两个散点图字形是否会在matplotlib中重叠。 So given a pair of (x, y) coordinates and a size (which as i understand is the area of the circle, in points), I would like to plot
所以给出一对(x,y)坐标和一个大小(据我所知是圆的面积,以点为单位),我想绘制
plt.scatter(x, y, s=s)
and then have a function called points_overlap
that takes these parameters and returns True
if the points will overlap and False
otherwise. 然后有一个名为
points_overlap
的函数,它接受这些参数,如果点重叠则返回True
,否则返回False
。
def points_overlap(x, y, s):
if ...
return True
else:
return False
I know there are transformation matrices to take me between the different matplotlib coordinate systems , but I can't figure out the right steps for writing this function. 我知道在不同的matplotlib坐标系之间有转换矩阵,但是我无法找到编写这个函数的正确步骤。
This needs some testing, but it might work? 这需要一些测试,但它可能有用吗? These should all be in Display space
这些都应该在显示空间中
def overlap(x, y, sx, sy):
return np.linalg.norm(x - y) < np.linalg.norm(sx + sy)
test: 测试:
In [227]: X = np.array([[1, 1], [2, 1], [2.5, 1]])
In [228]: s = np.array([20, 10000, 10000])
In [229]: fig, ax = plt.subplots()
In [230]: ax.scatter(X[:, 0], X[:, 1], s=s)
Out[230]: <matplotlib.collections.PathCollection at 0x10c32f28>
In [231]: plt.draw()
Test every pair: 测试每一对:
Xt = ax.transData.transform(X)
st = np.sqrt(s)
pairs = product(Xt, Xt)
sizes = product(st, st)
for i, ((x, y), (sx, sy)) in enumerate(zip(pairs, sizes)):
h = i % 3
j = i // 3
if h != j and overlap(x, y, sx, sy):
print((i, h, j))
There's lots of room for improvement. 还有很大的改进空间。 It's probably easier to transform all your data and pass that into the
points_overlap
function instead of doing the transform inside. 转换所有数据并将其传递给
points_overlap
函数可能更容易,而不是在内部进行转换。 That'd be much better actually. 实际上这要好得多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.