Reputation: 1
when I calculate the mean over a 2D moving grid window in Python, but there is a white line just in the top, not on the bottom, right or left, but horizontal, after which there's just white in the plotter contour, so somehow invalid values. The bigger I chose the selected moving interval window, the bigger the corresponding top white area with invalid values, because there should be values. Over edge values cannot be summed, therefore the grid is extended with np.pad -> constant 'edge' values.
Parts of my code:
#Function
def apply_mean_filter(grid, window_size): # window size was chosen 500 points - about 1m
pad_size = window_size // 2
padded_grid = np.pad(grid, pad_size, mode='edge')
filtered_grid = np.zeros_like(grid)
rows, cols = grid.shape
for i in range(rows):
for j in range(cols):
window = padded_grid[i:i + window_size, j:j + window_size]
filtered_grid[i, j] = np.mean(window)
return filtered_grid
...
gradient_reg = griddata((coordinates_mvm[0], coordinates_mvm[1]), coordinates_mvm[3], (grid_x, grid_y), method='cubic')
mean_window = ``your text``apply_mean_filter(gradient_reg, 500)
...
c1 = axs[0].contourf(grid_x, grid_y, mean_window, levels=1000, cmap=cmap, norm=norm)
# Here the rest of the plotting code
...```
Normally plotting gradient_reg is working well, but if I plot just the mean I get something unexpected like this: https://i.sstatic.net/YGjewnx7.png
What's wrong?
I appreciate any help!
Regards :)
Some more code :) Above there's sufficient description :)
grid_x, grid_y = np.meshgrid(np.linspace(x_min, x_max, num_points), np.linspace(y_min, y_max, int((y_max / x_max) * num_points)))
gradient_reg = griddata((coordinates_mvm[0], coordinates_mvm[1]), coordinates_mvm[3], (grid_x, grid_y), method='cubic')
mean_window = apply_mean_filter(gradient_reg, 500)
vmin = min(np.nanmin(mean_window), np.nanmin(mean_window))
vmax = max(np.nanmax(mean_window), np.nanmax(mean_window))
fig, axs = plt.subplots(1, 2, figsize=(1, 1)) # Smaller figure size
cmap = "seismic"
# Create a TwoSlopeNorm where the color white is at 0
norm = mcolors.TwoSlopeNorm(vmin=vmin, vcenter=0, vmax=vmax)
# Plot for grid_gradient1
c1 = axs[0].contourf(grid_x, grid_y, mean_window, levels=1000, cmap=cmap, norm=norm)
axs[0].set_xlabel('X Distance (m)')
axs[0].set_ylabel('Y Distance (m)')
axs[0].set_title('') # 1D Moving Average of the vertical gradient with buried objects
axs[0].set_aspect(4/3)
...```
Upvotes: 0
Views: 31