简体   繁体   English

如何在matplotlib中将散点大小转换为数据坐标?

[英]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.

相关问题 matplotlib:如何将XYZ散射转换为像素图像? - matplotlib: how can I convert a XYZ scatter to a pixel image? 从数据坐标转换为 matplotlib 中的轴坐标 - Convert from data coordinates to axes coordinates in matplotlib 如何防止数据在 matplotlib 散点图中挤在一起? - How can I keep the data from being crowded together in matplotlib scatter plot? 如何使用Matplotlib绘制极坐标中轮廓密度线的散点图? - How can I draw a scatter plot with contour density lines in polar coordinates using Matplotlib? 如何将 x、y 坐标从一个小比例的图形转换为一个更大尺寸的新 matplotlib 图形? - How do I convert x, y coordinates from one graph with a small scale to a new matplotlib graph of a much greater size? Matplotlib 动画散点图如何在不叠加数据的情况下完成? - How animation scatter plot with Matplotlib can be done with not superimposed data? 如何在 matplotlib 散点图中标准化颜色图? - How can I normalize colormap in matplotlib scatter plot? 如何使用 matplotlib.pyplot 连接散点 - How can I connect scatter points using matplotlib.pyplot 如何在 matplotlib 中制作按密度着色的散点图? - How can I make a scatter plot colored by density in matplotlib? 如何从文本创建变量并转换坐标 - How Can I Create Variables From Text and Convert Coordinates
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM