I have two numpy arrays (plot_x and plot_y) of the same length where plot_y is calculated based off on the values of plot_x. I want to filter the first array plot_x such that 0.1<=plot_x<=10 and filter the second array plot_y such that it contains the values that corresponds to the values in plot_x so that I can plot them in a graph. How do I do that? Thank you in advance.
1 Answer
Filtering the data using Numpy-module is one of the basic operation, one of the simplest way is :
LOGIC :
1. Define Masking Matrix which we will use to filter the data.
condn = np.logical_and(X<5, X>0)
2. Using subscription operator [] we can access desired data with condition masking-matrix should follow Broadcasting-Properties.
X_u = X[condn]
Y_u = Y[condn]
CODE :
import numpy as np
X = np.array([-1, -3, 1,-5, 2, 3,-6, 4, 5,-9, 6, 7, 8])
Y = np.array([i**3 for i in X])
condn = np.logical_and(X<5, X>0)
# for shortcut you can use => condn = (X<5) & (X>0)
X_u = X[condn]
Y_u = Y[condn]
print(X_u)
print(Y_u)
OUTPUT :
[1 2 3 4]
[ 1 8 27 64]
plot_xand later calculateplot_yusing already filteredplot_x. OR you would have to keep it as pairs(x from plot_x, y from plot_y)to filter it aftre calculations.