Interpolating within a grid in python





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I have a number of height values across a grid, specified in a series of lists:



[3, 1, -2, -3, -3] # x = 0m 
[2, -7, -14, -30, -39] # x = 10m
[46, 22, 5, -2, -8] # x = 20m


The example above shows a 40m x 20m grid in the form



[x=0y=0, x=0y=10...][x=10y=0, x=10y=10...] etc.


I need to calculate the height at a specific x,y coordinate. I have looked at various interpolate functions and example but can't make any sense out of them - please help!



An example would be the height at x = 5m, y = 5m in teh grid above, which would be in the middle of the values 3, 1 2 and -7 (-1ish?)



Thanks










share|improve this question























  • Is it what you need? docs.scipy.org/doc/scipy-0.14.0/reference/generated/…

    – dyukha
    Jan 3 at 12:34











  • You may want to check this post

    – Cedric Zoppolo
    Jan 3 at 17:49


















0















I have a number of height values across a grid, specified in a series of lists:



[3, 1, -2, -3, -3] # x = 0m 
[2, -7, -14, -30, -39] # x = 10m
[46, 22, 5, -2, -8] # x = 20m


The example above shows a 40m x 20m grid in the form



[x=0y=0, x=0y=10...][x=10y=0, x=10y=10...] etc.


I need to calculate the height at a specific x,y coordinate. I have looked at various interpolate functions and example but can't make any sense out of them - please help!



An example would be the height at x = 5m, y = 5m in teh grid above, which would be in the middle of the values 3, 1 2 and -7 (-1ish?)



Thanks










share|improve this question























  • Is it what you need? docs.scipy.org/doc/scipy-0.14.0/reference/generated/…

    – dyukha
    Jan 3 at 12:34











  • You may want to check this post

    – Cedric Zoppolo
    Jan 3 at 17:49














0












0








0








I have a number of height values across a grid, specified in a series of lists:



[3, 1, -2, -3, -3] # x = 0m 
[2, -7, -14, -30, -39] # x = 10m
[46, 22, 5, -2, -8] # x = 20m


The example above shows a 40m x 20m grid in the form



[x=0y=0, x=0y=10...][x=10y=0, x=10y=10...] etc.


I need to calculate the height at a specific x,y coordinate. I have looked at various interpolate functions and example but can't make any sense out of them - please help!



An example would be the height at x = 5m, y = 5m in teh grid above, which would be in the middle of the values 3, 1 2 and -7 (-1ish?)



Thanks










share|improve this question














I have a number of height values across a grid, specified in a series of lists:



[3, 1, -2, -3, -3] # x = 0m 
[2, -7, -14, -30, -39] # x = 10m
[46, 22, 5, -2, -8] # x = 20m


The example above shows a 40m x 20m grid in the form



[x=0y=0, x=0y=10...][x=10y=0, x=10y=10...] etc.


I need to calculate the height at a specific x,y coordinate. I have looked at various interpolate functions and example but can't make any sense out of them - please help!



An example would be the height at x = 5m, y = 5m in teh grid above, which would be in the middle of the values 3, 1 2 and -7 (-1ish?)



Thanks







python interpolation






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 3 at 12:26









tp503tp503

31




31













  • Is it what you need? docs.scipy.org/doc/scipy-0.14.0/reference/generated/…

    – dyukha
    Jan 3 at 12:34











  • You may want to check this post

    – Cedric Zoppolo
    Jan 3 at 17:49



















  • Is it what you need? docs.scipy.org/doc/scipy-0.14.0/reference/generated/…

    – dyukha
    Jan 3 at 12:34











  • You may want to check this post

    – Cedric Zoppolo
    Jan 3 at 17:49

















Is it what you need? docs.scipy.org/doc/scipy-0.14.0/reference/generated/…

– dyukha
Jan 3 at 12:34





Is it what you need? docs.scipy.org/doc/scipy-0.14.0/reference/generated/…

– dyukha
Jan 3 at 12:34













You may want to check this post

– Cedric Zoppolo
Jan 3 at 17:49





You may want to check this post

– Cedric Zoppolo
Jan 3 at 17:49












3 Answers
3






active

oldest

votes


















1














You can use scipy.interpolate.interp2d. The code below should do the job:



import numpy as np
from scipy.interpolate import interp2d

# Original data (e.g. measurements)
a = [3, 1, -2, -3, -3]
b = [2, -7, -14, -30, -39]
c = [46, 22, 5, -2, -8]

x = [0, 10, 20] # x-coordinates
y = [0, 10, 20, 30, 40] # y-coordinates

# Organise data in matrix
z = np.vstack([a, b, c]).T

# Create interpolation function
f_z = interp2d(x, y, z)

# Desired x/y values
x_interp = 5
y_interp = 5

# Collect interpolated z-value
z_interp = f_z(x_interp, y_interp)
print(z_interp) # (result: [-0.25])





share|improve this answer
























  • This works perfectly, thank you!

    – tp503
    Jan 3 at 16:20



















0














Solution: Transform into 2D array



import numpy as np

a = [3, 1, -2, -3, -3] # x = 0m
b = [2, -7, -14, -30, -39] # x = 10m
c = [46, 22, 5, -2, -8] # x = 20m

all_lists = [a,b,c]


grid = np.vstack(all_lists) # Edit: thanks @jochen for better code

>>print(grid.shape)
(3, 5)


Now you can access grid via x,y coordinates



grid[1,1] and etc



Based on this, you can create logic.



If x = 5, y = 5, then you can create square corners indexes by this:



indexes: x/5 +0, x/5+1, y/5,y/5+1 ---> by combining them you get [0,0],[0,1],[1,0],[1,1]



At the end you just feed grid with those indexes



SUM = grid[0,0] + grid[0,1] + grid[1,0] + grid[1,1]





share|improve this answer


























  • The construction of your grid is unnecessary complex. It could be grid = np.vstack([a, b, c]) Your code actually constructs and allocates memory for 3 grids, discarding the first two.

    – jochen
    Jan 3 at 14:10











  • thanks. I tried vstack and it just extended array, but with your code, it does it better

    – Martin
    Jan 3 at 14:46



















0














You can achieve this with interpolate.gridddata function from scipy.



With below code you can get the any interpolation you want from your grid.



import itertools
import numpy as np
from scipy.interpolate import griddata

dataX0 = [3, 1, -2, -3, -3] # x = 0m
dataX10 = [2, -7, -14, -30, -39] # x = 10m
dataX20 = [46, 22, 5, -2, -8] # x = 20m
data = dataX0 + dataX10 + dataX20
points = list(itertools.product(range(0,30,10),range(0,50,10)))

outputPoint = (5,5)
outputValue = griddata(points=points,values=data, xi=outputPoint, method="cubic")
print outputValue


The example above give you the interpolation at ouputPoint (5,5). And the output would give:



>>> 
-2.78976054957





share|improve this answer


























  • You may want to check this post

    – Cedric Zoppolo
    Jan 3 at 15:01






  • 1





    The post you linked applies to most answers given here. Perhaps you could add it as a comment to the original question, so that it is more likely to get noticed by other readers?

    – MPA
    Jan 3 at 15:19












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54022277%2finterpolating-within-a-grid-in-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You can use scipy.interpolate.interp2d. The code below should do the job:



import numpy as np
from scipy.interpolate import interp2d

# Original data (e.g. measurements)
a = [3, 1, -2, -3, -3]
b = [2, -7, -14, -30, -39]
c = [46, 22, 5, -2, -8]

x = [0, 10, 20] # x-coordinates
y = [0, 10, 20, 30, 40] # y-coordinates

# Organise data in matrix
z = np.vstack([a, b, c]).T

# Create interpolation function
f_z = interp2d(x, y, z)

# Desired x/y values
x_interp = 5
y_interp = 5

# Collect interpolated z-value
z_interp = f_z(x_interp, y_interp)
print(z_interp) # (result: [-0.25])





share|improve this answer
























  • This works perfectly, thank you!

    – tp503
    Jan 3 at 16:20
















1














You can use scipy.interpolate.interp2d. The code below should do the job:



import numpy as np
from scipy.interpolate import interp2d

# Original data (e.g. measurements)
a = [3, 1, -2, -3, -3]
b = [2, -7, -14, -30, -39]
c = [46, 22, 5, -2, -8]

x = [0, 10, 20] # x-coordinates
y = [0, 10, 20, 30, 40] # y-coordinates

# Organise data in matrix
z = np.vstack([a, b, c]).T

# Create interpolation function
f_z = interp2d(x, y, z)

# Desired x/y values
x_interp = 5
y_interp = 5

# Collect interpolated z-value
z_interp = f_z(x_interp, y_interp)
print(z_interp) # (result: [-0.25])





share|improve this answer
























  • This works perfectly, thank you!

    – tp503
    Jan 3 at 16:20














1












1








1







You can use scipy.interpolate.interp2d. The code below should do the job:



import numpy as np
from scipy.interpolate import interp2d

# Original data (e.g. measurements)
a = [3, 1, -2, -3, -3]
b = [2, -7, -14, -30, -39]
c = [46, 22, 5, -2, -8]

x = [0, 10, 20] # x-coordinates
y = [0, 10, 20, 30, 40] # y-coordinates

# Organise data in matrix
z = np.vstack([a, b, c]).T

# Create interpolation function
f_z = interp2d(x, y, z)

# Desired x/y values
x_interp = 5
y_interp = 5

# Collect interpolated z-value
z_interp = f_z(x_interp, y_interp)
print(z_interp) # (result: [-0.25])





share|improve this answer













You can use scipy.interpolate.interp2d. The code below should do the job:



import numpy as np
from scipy.interpolate import interp2d

# Original data (e.g. measurements)
a = [3, 1, -2, -3, -3]
b = [2, -7, -14, -30, -39]
c = [46, 22, 5, -2, -8]

x = [0, 10, 20] # x-coordinates
y = [0, 10, 20, 30, 40] # y-coordinates

# Organise data in matrix
z = np.vstack([a, b, c]).T

# Create interpolation function
f_z = interp2d(x, y, z)

# Desired x/y values
x_interp = 5
y_interp = 5

# Collect interpolated z-value
z_interp = f_z(x_interp, y_interp)
print(z_interp) # (result: [-0.25])






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 3 at 14:02









MPAMPA

84711429




84711429













  • This works perfectly, thank you!

    – tp503
    Jan 3 at 16:20



















  • This works perfectly, thank you!

    – tp503
    Jan 3 at 16:20

















This works perfectly, thank you!

– tp503
Jan 3 at 16:20





This works perfectly, thank you!

– tp503
Jan 3 at 16:20













0














Solution: Transform into 2D array



import numpy as np

a = [3, 1, -2, -3, -3] # x = 0m
b = [2, -7, -14, -30, -39] # x = 10m
c = [46, 22, 5, -2, -8] # x = 20m

all_lists = [a,b,c]


grid = np.vstack(all_lists) # Edit: thanks @jochen for better code

>>print(grid.shape)
(3, 5)


Now you can access grid via x,y coordinates



grid[1,1] and etc



Based on this, you can create logic.



If x = 5, y = 5, then you can create square corners indexes by this:



indexes: x/5 +0, x/5+1, y/5,y/5+1 ---> by combining them you get [0,0],[0,1],[1,0],[1,1]



At the end you just feed grid with those indexes



SUM = grid[0,0] + grid[0,1] + grid[1,0] + grid[1,1]





share|improve this answer


























  • The construction of your grid is unnecessary complex. It could be grid = np.vstack([a, b, c]) Your code actually constructs and allocates memory for 3 grids, discarding the first two.

    – jochen
    Jan 3 at 14:10











  • thanks. I tried vstack and it just extended array, but with your code, it does it better

    – Martin
    Jan 3 at 14:46
















0














Solution: Transform into 2D array



import numpy as np

a = [3, 1, -2, -3, -3] # x = 0m
b = [2, -7, -14, -30, -39] # x = 10m
c = [46, 22, 5, -2, -8] # x = 20m

all_lists = [a,b,c]


grid = np.vstack(all_lists) # Edit: thanks @jochen for better code

>>print(grid.shape)
(3, 5)


Now you can access grid via x,y coordinates



grid[1,1] and etc



Based on this, you can create logic.



If x = 5, y = 5, then you can create square corners indexes by this:



indexes: x/5 +0, x/5+1, y/5,y/5+1 ---> by combining them you get [0,0],[0,1],[1,0],[1,1]



At the end you just feed grid with those indexes



SUM = grid[0,0] + grid[0,1] + grid[1,0] + grid[1,1]





share|improve this answer


























  • The construction of your grid is unnecessary complex. It could be grid = np.vstack([a, b, c]) Your code actually constructs and allocates memory for 3 grids, discarding the first two.

    – jochen
    Jan 3 at 14:10











  • thanks. I tried vstack and it just extended array, but with your code, it does it better

    – Martin
    Jan 3 at 14:46














0












0








0







Solution: Transform into 2D array



import numpy as np

a = [3, 1, -2, -3, -3] # x = 0m
b = [2, -7, -14, -30, -39] # x = 10m
c = [46, 22, 5, -2, -8] # x = 20m

all_lists = [a,b,c]


grid = np.vstack(all_lists) # Edit: thanks @jochen for better code

>>print(grid.shape)
(3, 5)


Now you can access grid via x,y coordinates



grid[1,1] and etc



Based on this, you can create logic.



If x = 5, y = 5, then you can create square corners indexes by this:



indexes: x/5 +0, x/5+1, y/5,y/5+1 ---> by combining them you get [0,0],[0,1],[1,0],[1,1]



At the end you just feed grid with those indexes



SUM = grid[0,0] + grid[0,1] + grid[1,0] + grid[1,1]





share|improve this answer















Solution: Transform into 2D array



import numpy as np

a = [3, 1, -2, -3, -3] # x = 0m
b = [2, -7, -14, -30, -39] # x = 10m
c = [46, 22, 5, -2, -8] # x = 20m

all_lists = [a,b,c]


grid = np.vstack(all_lists) # Edit: thanks @jochen for better code

>>print(grid.shape)
(3, 5)


Now you can access grid via x,y coordinates



grid[1,1] and etc



Based on this, you can create logic.



If x = 5, y = 5, then you can create square corners indexes by this:



indexes: x/5 +0, x/5+1, y/5,y/5+1 ---> by combining them you get [0,0],[0,1],[1,0],[1,1]



At the end you just feed grid with those indexes



SUM = grid[0,0] + grid[0,1] + grid[1,0] + grid[1,1]






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 3 at 14:50

























answered Jan 3 at 12:44









Martin Martin

1,5081516




1,5081516













  • The construction of your grid is unnecessary complex. It could be grid = np.vstack([a, b, c]) Your code actually constructs and allocates memory for 3 grids, discarding the first two.

    – jochen
    Jan 3 at 14:10











  • thanks. I tried vstack and it just extended array, but with your code, it does it better

    – Martin
    Jan 3 at 14:46



















  • The construction of your grid is unnecessary complex. It could be grid = np.vstack([a, b, c]) Your code actually constructs and allocates memory for 3 grids, discarding the first two.

    – jochen
    Jan 3 at 14:10











  • thanks. I tried vstack and it just extended array, but with your code, it does it better

    – Martin
    Jan 3 at 14:46

















The construction of your grid is unnecessary complex. It could be grid = np.vstack([a, b, c]) Your code actually constructs and allocates memory for 3 grids, discarding the first two.

– jochen
Jan 3 at 14:10





The construction of your grid is unnecessary complex. It could be grid = np.vstack([a, b, c]) Your code actually constructs and allocates memory for 3 grids, discarding the first two.

– jochen
Jan 3 at 14:10













thanks. I tried vstack and it just extended array, but with your code, it does it better

– Martin
Jan 3 at 14:46





thanks. I tried vstack and it just extended array, but with your code, it does it better

– Martin
Jan 3 at 14:46











0














You can achieve this with interpolate.gridddata function from scipy.



With below code you can get the any interpolation you want from your grid.



import itertools
import numpy as np
from scipy.interpolate import griddata

dataX0 = [3, 1, -2, -3, -3] # x = 0m
dataX10 = [2, -7, -14, -30, -39] # x = 10m
dataX20 = [46, 22, 5, -2, -8] # x = 20m
data = dataX0 + dataX10 + dataX20
points = list(itertools.product(range(0,30,10),range(0,50,10)))

outputPoint = (5,5)
outputValue = griddata(points=points,values=data, xi=outputPoint, method="cubic")
print outputValue


The example above give you the interpolation at ouputPoint (5,5). And the output would give:



>>> 
-2.78976054957





share|improve this answer


























  • You may want to check this post

    – Cedric Zoppolo
    Jan 3 at 15:01






  • 1





    The post you linked applies to most answers given here. Perhaps you could add it as a comment to the original question, so that it is more likely to get noticed by other readers?

    – MPA
    Jan 3 at 15:19
















0














You can achieve this with interpolate.gridddata function from scipy.



With below code you can get the any interpolation you want from your grid.



import itertools
import numpy as np
from scipy.interpolate import griddata

dataX0 = [3, 1, -2, -3, -3] # x = 0m
dataX10 = [2, -7, -14, -30, -39] # x = 10m
dataX20 = [46, 22, 5, -2, -8] # x = 20m
data = dataX0 + dataX10 + dataX20
points = list(itertools.product(range(0,30,10),range(0,50,10)))

outputPoint = (5,5)
outputValue = griddata(points=points,values=data, xi=outputPoint, method="cubic")
print outputValue


The example above give you the interpolation at ouputPoint (5,5). And the output would give:



>>> 
-2.78976054957





share|improve this answer


























  • You may want to check this post

    – Cedric Zoppolo
    Jan 3 at 15:01






  • 1





    The post you linked applies to most answers given here. Perhaps you could add it as a comment to the original question, so that it is more likely to get noticed by other readers?

    – MPA
    Jan 3 at 15:19














0












0








0







You can achieve this with interpolate.gridddata function from scipy.



With below code you can get the any interpolation you want from your grid.



import itertools
import numpy as np
from scipy.interpolate import griddata

dataX0 = [3, 1, -2, -3, -3] # x = 0m
dataX10 = [2, -7, -14, -30, -39] # x = 10m
dataX20 = [46, 22, 5, -2, -8] # x = 20m
data = dataX0 + dataX10 + dataX20
points = list(itertools.product(range(0,30,10),range(0,50,10)))

outputPoint = (5,5)
outputValue = griddata(points=points,values=data, xi=outputPoint, method="cubic")
print outputValue


The example above give you the interpolation at ouputPoint (5,5). And the output would give:



>>> 
-2.78976054957





share|improve this answer















You can achieve this with interpolate.gridddata function from scipy.



With below code you can get the any interpolation you want from your grid.



import itertools
import numpy as np
from scipy.interpolate import griddata

dataX0 = [3, 1, -2, -3, -3] # x = 0m
dataX10 = [2, -7, -14, -30, -39] # x = 10m
dataX20 = [46, 22, 5, -2, -8] # x = 20m
data = dataX0 + dataX10 + dataX20
points = list(itertools.product(range(0,30,10),range(0,50,10)))

outputPoint = (5,5)
outputValue = griddata(points=points,values=data, xi=outputPoint, method="cubic")
print outputValue


The example above give you the interpolation at ouputPoint (5,5). And the output would give:



>>> 
-2.78976054957






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 3 at 15:03

























answered Jan 3 at 14:48









Cedric ZoppoloCedric Zoppolo

1,36211529




1,36211529













  • You may want to check this post

    – Cedric Zoppolo
    Jan 3 at 15:01






  • 1





    The post you linked applies to most answers given here. Perhaps you could add it as a comment to the original question, so that it is more likely to get noticed by other readers?

    – MPA
    Jan 3 at 15:19



















  • You may want to check this post

    – Cedric Zoppolo
    Jan 3 at 15:01






  • 1





    The post you linked applies to most answers given here. Perhaps you could add it as a comment to the original question, so that it is more likely to get noticed by other readers?

    – MPA
    Jan 3 at 15:19

















You may want to check this post

– Cedric Zoppolo
Jan 3 at 15:01





You may want to check this post

– Cedric Zoppolo
Jan 3 at 15:01




1




1





The post you linked applies to most answers given here. Perhaps you could add it as a comment to the original question, so that it is more likely to get noticed by other readers?

– MPA
Jan 3 at 15:19





The post you linked applies to most answers given here. Perhaps you could add it as a comment to the original question, so that it is more likely to get noticed by other readers?

– MPA
Jan 3 at 15:19


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54022277%2finterpolating-within-a-grid-in-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

'app-layout' is not a known element: how to share Component with different Modules

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

WPF add header to Image with URL pettitions [duplicate]