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].
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
X Y Z
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
file
should be used instead oftable
- 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! :) – Kjetil Sonerud Oct 24 '14 at 13:26 -
1Thanks for the update. In fact,
file
andtable
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
. – Christian Feuersänger Oct 24 '14 at 21:23 -
1Thanks for the clarification! For some reason, this does not seem to work for me - using
table
instead offile
with a.dat
-file as source for the plotting data throws an error. I may very well have done something wrong here, but at leastfile
works like a charm. I must add thatpgfplots
in general is amazing; thanks for that! :) – Kjetil Sonerud Oct 27 '14 at 9:41