I have some data that represents small-scale surface topography. The data is planar but not flat because it's impractical to level this microscope to a tolerance of microns. It looks like this:

An example of 3D profilometric data with a nonzero planar slope I'd like to postprocess that slope out, such that the major axis is colinear with the long side of the blue rectangle and the vertical axis can then be rescaled to show meaningful topographic variation instead of the (really large) planar slope.

One approach here might be to do an orthogonal transformation via (basically) principal component analysis, eg. find the three eigenvalue/eigenvector pairs, and then treat the eigenvectors corresponding to the largest two eigenvalues as the new X and Y axes. I'd be very interested to see how this (or some better way) might be done efficiently in Mathematica (this data is generally in the millions of rows).

An example of my data is here.

and this is the code I've written so far (which depends on version 9+ for Downsample[]):

    pathToDatafile = "path\\to\\file\\6000s.xyz";
    data = Import[pathToDatafile,"Table","HeaderLines"->15];
    data = DeleteCases[data,{__,_String,_String}]; (*remove the "no data" lines that the instrument creates*)
    data=Delete[data,-1]; (*throw away the instrument's pound sign at the end of the file*)
    data = Downsample[data,{100,1}];(*downsample by a lot because we have way too many datapoints*)

2 Answers 2


This solution is giving some artifacts, I believe, due to the downsampling, but the problem can be solved by using RotationTransform similar to this

ListPlot3D[RotationTransform[-2.2 Degree, {1, 0, 0}, data[[1]]]@data]

Mathematica graphics

Mathematica graphics

You can make an interactive leveler. This requires a severely downsampled dataset in order to work smoothly (at least on my computer)

subdata = Downsample[data, {100, 1}];
  d = RotationTransform[x Degree, {1, 0, 0}, subdata[[1]]]@subdata;
  d = RotationTransform[y Degree, {0, 1, 0}, d[[1]]]@d;
  ], {{x, 0}, -5, 5}, {{y, 0}, -5, 5}]

Mathematica graphics

And then use those results on the full set of data

 d = RotationTransform[-2.16 Degree, {1, 0, 0}, data[[1]]]@data;
 d = RotationTransform[0.12 Degree, {0, 1, 0}, d[[1]]]@d;

Which gets you a slightly tweaked version of the image I have above, so I won't repost it.

  • $\begingroup$ The interactivity is particularly handy because I'll have to batch process many of these plots and they'll all have a slightly different slope. It's much faster to do by eye than by trial/error/replot. Thanks! $\endgroup$
    – avn2109
    Nov 5, 2013 at 17:05

You can use LinearModelFit to create a linear-fit of the model, and use that to find an appropriate rotation.

model = LinearModelFit[
   {x, y},
   {x, y}];
(* We have: z = c + a x + b y *)
{c, a, b} = model["BestFitParameters"];

rt = Composition[
   {1, 0, a},
   {1, 0, 0}},
  {0, 0, c}],
   {0, 1, b},
   {0, 1, 0}},
  {0, 0, c}]];
 corrected = rt@data;


(Note: As I don't have Mathematica v9 I downsampled by data = data[[;; ;;100, All]] which I assume is more-or-less the same in this case)

Edit: Answer used to be just corrected = data; corrected[[All, 3]] -= model["Function"] @@@ corrected[[All, ;; 2]]; but as @bobthechemist points out that will squish together the plane.

  • $\begingroup$ I think one has to be careful with a linear subtraction, as this will change the size of the surface. Note that EuclideanDistance[data[[1]],data[[-1]]] does not equal EuclideanDistance[corrected[[1]],data[[-1]]] $\endgroup$ Nov 5, 2013 at 1:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.