Python Matplotlib修补程序包含_point()返回不一致的结果

Python Matplotlib修补程序包含_point()返回不一致的结果,python,matplotlib,Python,Matplotlib,为什么更改matplotlib.patches的打印参数会将该值更改为无意义的值?例如: import matplotlib.patches as mpatches print(mpatches.Circle((0,0), radius=1, ec='r').contains_point((0,1.5))) print(mpatches.Circle((0,0), radius=1).contains_point((0,1.5))) 第一行返回True,第二行返回False——显然,以(0,0

为什么更改
matplotlib.patches的打印参数会将该值更改为无意义的值?例如:

import matplotlib.patches as mpatches
print(mpatches.Circle((0,0), radius=1, ec='r').contains_point((0,1.5)))
print(mpatches.Circle((0,0), radius=1).contains_point((0,1.5)))

第一行返回
True
,第二行返回
False
——显然,以
(0,0)
为中心的单位圆不包含
(0,1.5)

包含点
功能中,您可以指定一个
半径
参数,以便在区域中也考虑面片边界和指定半径一半之间封闭的所有点。指定
edgecolor
时,默认
linewidth
设置为1,这将修改
contains\u point
函数中的
radius
参数。实际上,在这个函数的后面,您可以看到有一个对
\u process\u radius
函数的调用,该函数将
contains\u point
radius
参数修改为线宽。此行为记录在常规路径对象的
contains_point
函数中:

路径以半径/2的切线方向延伸;i、 e.如果要绘制线宽为半径的路径,则线上的所有点仍将被视为包含在该区域中。相反,负值会缩小该区域:虚线上的点将被视为该区域之外的点

从这一点,我们可以了解您正在试验的行为。要解决此问题,您可以:

  • 检查点后设置边缘颜色
  • 始终在
    contains\u point
    函数中指定
    radius=0

  • 无论如何,我认为这是一种不必要的行为,因为
    linewidth
    是在点中指定的,而不是在数据坐标中指定的。打开一个问题。

    我发现:两个圆的路径相同,即
    circle.get\u path()==circle\u red.get\u path()
    返回
    True
    ec
    参数似乎为圆添加了0.5边框,即点
    (0,1.6)
    retrurns
    False
    ,也在第一次打印中。当绘制第一个圆并在适当变换后检查点
    (0,1.5)
    时,它将正确返回
    False
    。因此,我的建议是通过
    set_edgecolor
    仅在绘图前设置edgecolor,并仅在纯图形上检查绘图前的点,而不检查绘图参数。
    ###### Solution 1 ######
    import matplotlib.patches as mpatches
    import matplotlib.pyplot as plt
    
    circle_ec = mpatches.Circle((0,0), radius=1)
    circle = mpatches.Circle((0,0), radius=1)
    point = (0,1.5)
    print(circle_ec.contains_point(point, radius = 0))         # prints False
    print(circle.contains_point(point))                        # prints False
    circle_ec.set_edgecolor('r')
    
    ###### Solution 2 #######
    import matplotlib.patches as mpatches
    import matplotlib.pyplot as plt
    
    circle_ec = mpatches.Circle((0,0), radius=1, ec='r')
    circle = mpatches.Circle((0,0), radius=1)
    point = (0,1.5)
    print(circle_ec.contains_point(point, radius = 0))         # prints False
    print(circle.contains_point(point))                        # prints False