首页 > 解决方案 > Matplotlib 意外 contains_point() 导致嵌套圆环图

问题描述

我在尝试让Patch.contains_point()方法在嵌套圆环图中工作时遇到问题。这是我的代码:

fig, ax = plt.subplots()
ax.axis('equal')
width = 0.3

pie, _ = ax.pie([120, 120], radius=1)
plt.setp(pie, width=width, edgecolor='white')

pie2, _ = ax.pie([60, 60, 37, 40, 29, 10], radius=1 - width)
plt.setp(pie2, width=width, edgecolor='white')

def button_release(event):
    if event.inaxes == ax:
        pos = [event.x, event.y]
        for i, p in enumerate([pie, pie2]):
            for j, w in enumerate(p):
                if w.contains_point(pos):
                    print(i, j)

fig.canvas.mpl_connect("button_release_event", button_release)
plt.show()

结果是这个图表:

在此处输入图像描述

打印的“坐标”完全符合预期,直到外环只包含一个值,图表如下所示:

在此处输入图像描述

在这种情况下,单击任何楔形总是会在外圈上产生积极的结果。所以当我点击外圈时,我得到了(0, 0),没关系。但是当我点击内圈的第一个楔子时,我得到了(0, 0)and (1, 0)。与内圈上的其他楔块相同。

我还注意到,在这种情况下,单击图表内的空圆圈会导致误报(0, 0),而在第一个图表上并非如此。

contains_point()是假设的工作方式吗?

标签: pythonmatplotlib

解决方案


推荐阅读