I want to interpolate this to a 4 cell grid. Each cell would just have average values of all the points lying inside it.
The output then should look like this
Thus we have converted the entire data to a 2x2 matrix. Each cell of this matrix will have average x coordinate & average y coordinate values of all the points lying inside them.
A1= (3,-3) ; A2 = (3.5, 1.5)
A3= (-1,-3) ; A4= (-2,1)
=====WHAT IVE TRIED=====
avg = [[
(
( mat[row][col][0]
+ mat[row][col+1][0]
+ mat[row+1][col][0]
+ mat[row+1][col+1][0] ) / 4.0
,
( mat[row][col][1]
+ mat[row][col+1][1]
+ mat[row+1][col][1]
+ mat[row+1][col+1][1] ) / 4.0
)
for col in range(0, len(mat[0]), 2) ]
for row in range(0, len(mat), 2)
]
I'm not that good with numpy/scipy, i think this could be vastly improved in terms of elegancy and efficiency, but it works:
-> jupyter notebook with intermediate plots
Final code:
import numpy as np
import matplotlib.pyplot as plt
import math
data = np.random.uniform(low=-2.0, high=2.0, size=(2,100))
dataX = data[0]
dataY = data[1]
#plot the data
plt.plot(data[0], data[1], 'b+')
gridSize = 1.0
# grid coordinates are lower left point of grid rectangles
gridMaxX = math.floor(max(dataX) / gridSize)
gridMaxY = math.floor(max(dataY) / gridSize)
gridMinX = math.floor(min(dataX) / gridSize)
gridMinY = math.floor(min(dataY) / gridSize)
gridX = np.arange(gridMinX,gridMaxX + gridSize, gridSize)
gridY = np.arange(gridMinY,gridMaxY + gridSize, gridSize)
#plot the grid
for ix, x in enumerate(gridX):
plt.axvline(x=x)
for iy, y in enumerate(gridY):
plt.axhline(y=y)
#iterate the grid
for gridPosX in gridX:
for gridPosY in gridY:
inCell = lambda x,y: (gridPosX<x and x<gridPosX+gridSize
and gridPosY<y and y<gridPosY+gridSize)
pointsInCell = [ (x,y) for (x,y) in zip(dataX, dataY) if inCell(x,y)]
if len(pointsInCell) > 0:
xPos, yPos = zip(*pointsInCell)
plt.plot(np.mean(xPos), np.mean(yPos), 'ro')
plt.show()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.