gpt4 book ai didi

python - 为什么绘制 matplotlib 补丁会损坏 contains_point?

转载 作者:行者123 更新时间:2023-12-04 03:19:00 25 4
gpt4 key购买 nike

运行下面的代码片段后,

import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse

plotted_ellipse = Ellipse((0, 0), 10, 10)
unplotted_ellipse = Ellipse((0, 0), 10, 10)

fig = plt.figure()
ax = fig.add_subplot(111)

ax.add_patch(plotted_ellipse)

ax.set_aspect('equal')
ax.set_xlim(-10, 10)
ax.set_ylim(-10, 10)

我在使用 contains_point 时得到以下结果方法

In [6]: plotted_ellipse.contains_point((0, 0))
Out[6]: False

In [7]: unplotted_ellipse.contains_point((0, 0))
Out[7]: True

这对我来说毫无意义。这可能是一个错误吗?或者是否有测试补丁对象是否包含点的首选方法?

使用 matplotlib 版本 1.5.1

最佳答案

这是因为您正在检查数据坐标中绘制的椭圆中的点,而当您向 ax 添加路径时,其坐标会转换为显示坐标。根据docs :

The convention of checking against the transformed patch stems from the fact that this method is predominantly used to check if display coordinates (e.g. from mouse events) are within the patch. If you want to do the above check with data coordinates, you have to properly transform them first

因此,如果您想检查数据坐标中的一个点,您必须先将其转换为显示坐标:

point = plotted_ellipse.get_transform().transform((0,0))
print(plotted_ellipse.contains_point(point))
>>> True

有关坐标系的说明,请参阅 here .

关于python - 为什么绘制 matplotlib 补丁会损坏 contains_point?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39583674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com