Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Fitting 2D rectangular data

I am writing a program using python 2.7 and am having difficulting pinpointing the best way to interpolate/fit some rectangular data.

I have a bunch of known data points of the format z=f(x,y) where x and y are evenly spaced on a grid. My x points range from 0 to 100 in increments of 0.1. My y points range from 0 to 100 in increments of 2.

My issue in interpolating or fitting this data is the large spacing between my y data coordinates. I am looking for the best way to fit this data so that I can evaluate the height Z for any (X,Y) coordinate pair. I want the resulting surface to be as smooth as possible. The conventional methods using griddata, or rectbivariatespline do no produce smooth surfaces - seemingly because the space between my y points is much greater than the space between my x points.

One option I considered was to use polyfit (or something similar) to fit a polynomial to each slice (the Y vs Z curve at each X) then to interpolate these slices together to produce a smoother surface. This seems like kind of a roundabout way of doing things. Is that my best option?

example of fitting a slice to a polynomial: fitting data

If fit to a polynomial, each of my slices (Y vs Z at a fixed X) is roughly gaussian or bimodal. They are fairly simple distributions.

My goal is to be able to produce a smooth, closely fit surface for display in 3D or on a pcolor style chart. I need to avoid swings past Zmax and Zmin. I know this is an issue when using splines.

my goal:

goal surface

Thank you for any help you can provide. I will watch this thread carefully and respond quickly if you need any elaboration.

EDIT:

From what I have read, it seems like what I am looking for is a shape preserving interpolation method in python for 2d data. I need something along the lines of pchip for matlab, but for a surface. I have looking through all the documentation and can't find anything shape preserving that will give me a smooth surface.

like image 728
user1764386 Avatar asked Feb 05 '26 03:02

user1764386


1 Answers

conventional methods using griddata, or rectbivariatespline do no produce smooth surfaces - seemingly because the space between my y points is much greater than the space between my x points"

You should try scaling your y-coordinates appropriately --- at least griddata assumes an euclidean metric i.e. that the length scales for all dimensions are similar.

If you on the other want shape-preserving interpolation, see my answer here However, such an interpolation will not do any smoothing, so maybe this is not what you want after all.

like image 53
pv. Avatar answered Feb 06 '26 20:02

pv.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!