2017-10-15 1 views
1

Ich möchte die Punkte auswählen, die in zwei willkürliche Kurven fallen, die jeweils durch ein Array von Punkten definiert sind. Unten ist ein Beispiel. In Wirklichkeit basieren die Kurven, die ich habe, nicht auf einer funktionalen Form, sie sind Arrays. Wie kann ich nur die Punkte auswählen, die in die rote und blaue Kurve fallen, und sagen, dass sie eine andere Farbe haben?Python: Wählen Sie Punkte aus, die in zwei beliebige Kurven fallen

import numpy as np 
import matplotlib.pyplot as plt 

# generate arrays from gaussian 
x = np.linspace(0.5, 3.5, 120) 
y = np.exp(-np.power(x - 2, 2.)/(2 * np.power(.8, 2.))) 
yy = .5*np.exp(-np.power(x - 2, 2.)/(2 * np.power(.4, 2.))) 

# generate random data points 
data_x = 4*np.random.rand(1000) 
data_y = np.random.rand(1000) 

fig = plt.figure() 
ax = plt.axes() 
ax.scatter(data_x, data_y, c='k', s=.1) 
ax.scatter(x,y, s=3) 
ax.scatter(x,yy, c='r', s=3) 
plt.show() 

enter image description here

+1

diese Kurven (scipy, einige Modell-Entscheidung über die Art der Interpolation benötigt, wahrscheinlich etwas monotone) interpolieren und dann boolean-Logik (die Teil von MPL fill_between API bereits -> arg wo). Theoretische Bemerkung: Sie werden * immer * irgendeine Art von A-priori-Annahmen/Regularisierungen mit unvollständigen Cuves benötigen; in diesem Fall Teil der Interpolation. – sascha

+0

Sind in Ihren tatsächlichen Daten die x-Werte für die beiden Kurven und die Daten identisch? ... ist (x0, x1, x2, x3, ...) für alle drei Datensätze gleich? – wwii

+0

Ja, ich dachte, ich muss interpolieren. Haben Sie Code für das, was Sie beschreiben? – user2483176

Antwort

0

Hier ist mein Versuch. Es implementiert numpy interpolierende Funktion, np.interp() wie in den Kommentaren erwähnt.

import numpy as np 
import matplotlib.pyplot as plt 

# generate arrays from gaussian 
x = np.linspace(0, 5, 120) 

# 2 sets of y's for given x 
# these can be any reasonable array of numbers 
y = np.exp(-np.power(x - 2, 2.)/(2 * np.power(.8, 2.))) 
yy = .5*np.exp(-np.power(x - 2, 2.)/(2 * np.power(.4, 2.))) 

fig = plt.figure() 
fig.set_size_inches(9, 7) 
ax = plt.axes() 

# plot curves using interpolating data 
numpnts = 60 
xs = np.linspace(0, 4, numpnts) 
ys1 = np.interp(xs, x, y) 
ys2 = np.interp(xs, x, yy) 

#ax.scatter(xs,ys1, c='b', s=8) # blue 
#ax.scatter(xs,ys2, c='r', s=8) # red 

# for the reference curves 
# better use plot than scatter 
ax.plot(xs, ys1, 'b^-', xs, ys2, 'ro-', markersize=4, linewidth=0.3) # blue 

# this function uses the interpolated data just created 
# and helps build color array for scatter plot 
def in_btw(x, y): 
    uppr = np.interp(x, xs, ys1) 
    lowr = np.interp(x, xs, ys2) 
    tf1 = lowr < y 
    tf2 = y < uppr 
    colr = 'c' 
    if tf1 and tf2: 
     colr = 'pink' 
    return colr 

# generate random data points 
data_x = 4*np.random.rand(1200) 
data_y = np.random.rand(1200) 

clrs = [] 
for ix,ea in enumerate(data_x): 
    #print (ea, in_btw(ea, data_y[ix])) 
    ret = in_btw(ea, data_y[ix]) 
    clrs.append(ret) 

# scatter plot of the data points with distinct colors 
# color: pink if location is between the 2 curves, else, cyan 
ax.scatter(data_x, data_y, c=clrs, s=4) 

plt.show() 

Das resultierende Bild:

enter image description here

2

Sie können numpy.interp verwenden, um die Punkte an den Positionen der Anordnungen zu interpolieren, dass die Kurven definieren.

c1 = data_y > np.interp(data_x, x,yy) 
c2 = data_y < np.interp(data_x, x,y) 

Stellen Sie dann die Farbe der Streuung als c=(c1&c2) und eine colormap der Wahl verwenden.

ax.scatter(data_x, data_y, c=(c1&c2), s=1, cmap="summer_r") 

Komplettes Beispiel:

import numpy as np 
import matplotlib.pyplot as plt 

# generate arrays from gaussian 
x = np.linspace(0.5, 3.5, 120) 
y = np.exp(-np.power(x - 2, 2.)/(2 * np.power(.8, 2.))) 
yy = .5*np.exp(-np.power(x - 2, 2.)/(2 * np.power(.4, 2.))) 

# generate random data points 
data_x = 4*np.random.rand(1000) 
data_y = np.random.rand(1000) 

c1 = data_y > np.interp(data_x, x,yy) 
c2 = data_y < np.interp(data_x, x,y) 

fig = plt.figure() 
ax = plt.axes() 
ax.scatter(data_x, data_y, c=(c1&c2), s=1, cmap="summer_r") 
ax.scatter(x,y, s=3) 
ax.scatter(x,yy, c='r', s=3) 
plt.show() 

enter image description here

Verwandte Themen