Zero out all x-values for specific time values: t > t0

Viewed 28

I use a data set called: arr0 with dimensions: (x, y, t) = (151, 151, 600). To create a 2D image I took a specific y-coordinate with slicing i.e. arr0[:,0], now the dimensions are: (x, t) = (151, 600). Within the dataset arr0[:,0] there are x-values for a time greater than t = 0.00173 (below the blue line in the image and last line of code, both shown below). What I would like to do is zero out all x-values for t > 0.00173.

The problem is that I don't know how I should do this, could someone please help me?

Best,

FR

plt.figure(figsize = (10,10))
plt.subplots_adjust(wspace=0.3)

plt.subplot(121)
plt.imshow((np.rot90(arr0[:,0],3)),cmap='Greys',extent=[xr1,xr2,taxis[nt-1],taxis[0]],aspect='auto')
plt.xlabel("Xr location [m]")
plt.ylabel("Time [s]")
plt.title("Direct wave after extrap (arr0)")
plt.hlines(0.00173,-0.3,0.3)

2D graph with values I want to turn zero below the blue line

1 Answers

If I understand you correctly, we can regard x, y, t as index variables along the axes of your 3-dimensional array, which contains the actual data values (represented by grayscale pixels in the plot). Here is a simplified example:

import numpy as np
import matplotlib.pyplot as plt

arr0 = np.array([[[0, 2, 1, 1, 2, 2],
                  [0, 2, 1, 1, 2, 2]], 

                 [[0, 3, 1, 1, 2, 2], 
                  [0, 3, 1, 1, 2, 2]]])
arr0.shape
(2, 2, 6)

Now I suppose you don't want to set the x values to zero, but rather the actual data values. Since NumPy works with integer indices, you'll have to convert your t-threshold to the appropriate integer index, based on the range of t values and the length of the array in dimension t:

plt.figure(figsize = (10, 4))
plt.subplots_adjust(wspace=0.3)

# plot original data slice
plt.subplot(121)
plt.imshow((np.rot90(arr0[:,0],3)), cmap='Greys', aspect='auto')

# define t-threshold integer index based on t range
t_threshold = int(arr0.shape[2] * 0.00173 / 0.006) + 1

# clean up data by setting values above t-threshold to zero
arr0[:, 0, t_threshold:] = 0

# plot cleaned up data slice
plt.subplot(122)
plt.imshow((np.rot90(arr0[:,0],3)), cmap='Greys', aspect='auto');

plot of array slices

Note that assigning new values to the slice has changed the full array accordingly:

arr0
array([[[0, 2, 0, 0, 0, 0],
        [0, 2, 1, 1, 2, 2]],

       [[0, 3, 0, 0, 0, 0],
        [0, 3, 1, 1, 2, 2]]])
Related