2016-04-19 2 views
2

Ich habe gerade die Pfad-Funktionalität von Matplotlib entdeckt und verwende sie mit path.contains_point, um zu überprüfen, ob Punkte innerhalb einer durch 2 Bezier-Kurven definierten Region gefunden werden.Matplotlib-Pfad contains_point

Ich erhalte ein unerwartetes Verhalten, wobei contains_pointTrue zurückgibt, wenn ich erwartet hätte, dass es False zurückgibt. Genauer gesagt, wenn der zu testende Punkt links von der Region liegt, scheint er falsch zu sein. Auf der rechten Seite ist in Ordnung.

Definieren meiner Pfade als eine Anzahl von geraden Linien anstelle von Kurven scheint zu funktionieren, wie erwartet.

Ein ausbleibende Testfall ist wie folgt:

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() 

Vielen Dank im Voraus für jede Hilfe, die Sie anbieten können. Python-Version ist 2.7, Anaconda Distro auf Linux Mint 17.3

Jim

Antwort

0

Sie haben einen offenen Pfad (extra moveto Befehl). Sobald Sie es kommentieren, funktioniert es gut.

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)) 
    ]