I need to plot a surface plot using TikZ. I have the (x,y) coordinate and the corresponding function value in a table form. I have not done 3D plots before in TikZ and hence I would appreciate any help. I would want a surf plot like this, but I do not have a closed form expression for my function. Instead, I have a table of values as shown below.The (X,Y) values are a two dimensional uniform grid on the domain [0,4] * [0,4].

enter image description here

  • 12
    Please provide the data in a plain text rather than an image. Aug 1, 2012 at 19:48

1 Answer 1


TikZ cannot do this with builtin methods. pgfplots can do it - in your case with \addplot3[surf, mesh/ordering=x varies] table {myfile.dat}; .

It supports custom colormaps, color bars, draws an appropriate axis, chooses suitable scales, ticks, and ticklabels etc.

See http://pgfplots.sourceforge.net/pgfplots.pdf for details and examples.

By default, pgfplots assumes numerical input (i.e. 0.29 instead of 29/10). If your data file really looks like

0 0 29/10

you need to write \addplot3.... table[z expr=\thisrow{Z}] {myfile.dat}; in order to activate math expression parsing for that column.

  • As far as I can tell, if the data supplied is in a file (.dat here), the command fileshould be used instead of table- at least that did the trick for me. I suspect that this is syntax that may have changed since the post was written, and just wanted to add this in case it is helpful to someone else coming across this post. Otherwise, great answer! :) Oct 24, 2014 at 13:26
  • 1
    Thanks for the update. In fact, file and table are almost the same and both do the job. \addplot3 table is more powerful and allows a huge degree of customization; but its default is actually the same as \addplot3 file. Oct 24, 2014 at 21:23
  • 1
    Thanks for the clarification! For some reason, this does not seem to work for me - using table instead of file with a .dat-file as source for the plotting data throws an error. I may very well have done something wrong here, but at least file works like a charm. I must add that pgfplots in general is amazing; thanks for that! :) Oct 27, 2014 at 9:41

You must log in to answer this question.