I'm having a bit of trouble. I've seen other questions answered on here about how to 3D plot data from .txt files of the format:

x y z

x y z

x y z

Which works well with the ListPointPlot function. However, using that methodology seems to confuse poor Mathematica when trying to do surface plotting of data that is in the format

` y y y y y y y y y y y y y y

z x x x x x x x x x x x x x x

z x x x x x x x x x x x x x x

z x x x x x x x x x x x x x x 

Where the ` is a place holder (just an empty space in my case). I've tried the manipulations that I know of or could find around the web, but those attempts end with errors being kicked back at me.

How would I get Mathematica to plot this .txt data?

  • $\begingroup$ Please include a proper minimal example that is indicative of your .txt file. As written, it is not clear how one would identify the different xs on each line. (I'm assuming they're different x's corresponding to the y's on top) $\endgroup$
    – rm -rf
    Nov 19, 2013 at 0:27
  • $\begingroup$ If the $y$'s and $z$'s are uniformly spaced, you can try ListPlot3D on the array of $x$'s. Use the DataRange option to specify the range of $y$ and $z$. $\endgroup$
    – user484
    Nov 19, 2013 at 0:34

1 Answer 1


You can probably read in the text file by importing it as a "Table":

d = ImportString["0 1 2 3\n4 2 1 0\n5 1 0 1\n6 0 1 2", "Table"]

0   1   2   3
4   2   1   0
5   1   0   1
6   0   1   2

Here I've used ImportString rather than Import (so I can use a string rather than a file).

Then you could split the values off:

x = Rest /@ Rest @ d;
y = Rest @ First @ d;
z = First /@ Rest @ d;

And reconstitute them in a list of {x, y, z} triples:

MapThread[Prepend, {Outer[List, y, z], x}, 2] ~Flatten~ 1
  • $\begingroup$ Let's say my file is relatively massive, though. As in, I'd be looking for a way to do this without manually typing in every number initially. $\endgroup$
    – Jak
    Nov 19, 2013 at 19:14
  • $\begingroup$ I probably wasn't clear in the answer. Import["C:\\yourfile.dat", "Table"] will read the file directly. I just used ImportString as a toy example. $\endgroup$
    – wxffles
    Nov 20, 2013 at 20:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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