Matplotlib路径contains_point [英] Matplotlib path contains_point

查看:28
本文介绍了Matplotlib路径contains_point的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚发现了matplotlib路径功能,并将其与 path.contains_point 一起使用,以检查是否在由2条贝塞尔曲线定义的区域内找到了点.

I've just discovered the matplotlib path functionality and I'm using it with path.contains_point to check whether points are found within a region defined by 2 bezier curves.

我遇到了一些意想不到的行为,其中 contains_point 返回 True,而我本来希望它返回 False.具体来说,如果要测试的点在该区域的左侧,那么它似乎是不正确的.右边没关系.

I'm getting some unexpected behaviour where contains_point is returning True when I would have expected it to return False. Specifically, if the point to be tested is to the left of the region then it seems to be incorrect. On the right is ok.

将我的路径定义为许多直线而不是曲线似乎按预期工作.

Defining my paths as a number of straight lines rather than curves seems to work as expected.

一个失败的测试用例如下:

A failing test case is as follows:

import matplotlib 
import matplotlib.path as mplPath
import matplotlib.patches as patches

import matplotlib.pyplot as plt
import pylab
import pandas as pd

print "MPL Version {}".format(matplotlib.__version__) #1.5.0
print "MPL NP Version {}".format(matplotlib.__version__numpy__) #1.6

path_data = [
    (mplPath.Path.MOVETO, (2, 10)),
    (mplPath.Path.CURVE4, (0, 100)),
    (mplPath.Path.CURVE4, (20, 100)),
    (mplPath.Path.CURVE4, (40, 150)),
    (mplPath.Path.MOVETO, (40, 150)),
    (mplPath.Path.CURVE4, (42, 45)),
    (mplPath.Path.CURVE4, (20, 30)),
    (mplPath.Path.CURVE4, (2, 10))
    ]

codes, verts = zip(*path_data)
path = mplPath.Path(verts, codes)
patch = patches.PathPatch(path, facecolor='r', alpha=0.5)

#Plot the patch and a some of the test points to visualise
fig = plt.figure()
ax = fig.add_subplot(111)
ax.add_patch(patch)
ax.set_xlim(0, 50)
ax.set_ylim(0, 200)

ax.scatter(1, 50)
ax.scatter(20, 120)
ax.scatter(20, 25)
print path.contains_point((1,50)) #This should be false but is actually true
print path.contains_point((20,120)) #This should be false but is actually true
print path.contains_point((20, 25)) #This should be false and it is

plt.show()

在此先感谢您提供的任何帮助.Python版本是2.7,Linux Mint 17.3上的Anaconda Distro

Thanks in advance for any help you can provide. Python version is 2.7, Anaconda Distro on Linux Mint 17.3

吉姆

推荐答案

您有一个开放路径(额外的 moveto 命令).一旦你把它注释掉,它就可以正常工作了.

You have an open path (extra moveto command). Once you comment it out, it works fine.

path_data = [
    (mplPath.Path.MOVETO, (2, 10)),
    (mplPath.Path.CURVE4, (0, 100)),
    (mplPath.Path.CURVE4, (20, 100)),
    (mplPath.Path.CURVE4, (40, 150)),
  #  (mplPath.Path.MOVETO, (40, 150)),
    (mplPath.Path.CURVE4, (42, 45)),
    (mplPath.Path.CURVE4, (20, 30)),
    (mplPath.Path.CURVE4, (2, 10))
    ]

这篇关于Matplotlib路径contains_point的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆