python - Matplotlib path contains_point -
i've discovered matplotlib path functionality , i'm using path.contains_point check whether points found within region defined 2 bezier curves.
i'm getting unexpected behaviour contains_point returning true when have expected return false. specifically, if point tested left of region seems incorrect. on right ok.
defining paths number of straight lines rather curves seems work expected.
a failing test case follows:
import matplotlib import matplotlib.path mplpath import matplotlib.patches patches import matplotlib.pyplot plt import pylab import pandas 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 patch , of test points 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 false true print path.contains_point((20,120)) #this should false true print path.contains_point((20, 25)) #this should false , plt.show() thanks in advance can provide. python version 2.7, anaconda distro on linux mint 17.3
jim
you have open path (extra moveto command). once comment out, 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)) ]
Comments
Post a Comment