2

I am trying to plot 3d data as a surface plot. However the color of the surface is wrong. I tried different shaders but none of the shaders seem to work. I think the color should be the same for the same values of Z.

\documentclass{article} 
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}

\begin{axis}[%
width=2.018in,
height=1.698in,
at={(0.339in,0.229in)},
scale only axis,
point meta min=0,
point meta max=1.8,
xmin=0,
xmax=0.2,
tick align=outside,
xlabel style={font=\color{white!15!black}},
xlabel={$X$},
ymin=0,
ymax=1.6,
ylabel style={font=\color{white!15!black}},
ylabel={$Y$},
zmin=0,
zmax=2,
zlabel style={font=\color{white!15!black}},
zlabel={$Z$},
view={-45}{45},
axis background/.style={fill=white},
axis x line*=bottom,
axis y line*=left,
axis z line*=left,
xmajorgrids,
ymajorgrids,
zmajorgrids
]

\addplot3[%
surf,
shader=interp, draw=black, z buffer=sort, colormap/jet, mesh/rows=10]
table[row sep=crcr, point meta=\thisrow{c}] {%
%
x   y   z   c\\
0.02    0.1 0.000836933014342034    0.000836933014342034\\
0.02    0.2 0.00334773205736814 0.00334773205736814\\
0.02    0.3 0.00753239711883609 0.00753239711883609\\
0.02    0.4 0.013390928208828   0.013390928208828\\
0.02    0.5 0.0209233253274806  0.0209233253274806\\
0.02    0.6 0.030129588474806   0.030129588474806\\
0.02    0.7 0.0410097176508092  0.0410097176508092\\
0.02    0.8 0.0535637128554926  0.0535637128554926\\
0.02    0.9 0.0677915740888575  0.0677915740888575\\
0.02    1   0.0836933013509049  0.0836933013509049\\
0.02    1.1 0.101268894633832   0.101268894633832\\
0.02    1.2 0.1205183539637 0.1205183539637\\
0.02    1.3 0.141441679271941   0.141441679271941\\
0.02    1.4 0.16403887060397    0.16403887060397\\
0.02    1.5 0.188309927963666   0.188309927963666\\
0.02    1.6 0.214254851351224   0.214254851351224\\
0.04    0.1 0.00156389526495684 0.00156389526495684\\
0.04    0.2 0.00625558105982738 0.00625558105982738\\
0.04    0.3 0.0140750573743694  0.0140750573743694\\
0.04    0.4 0.025022324218665   0.025022324218665\\
0.04    0.5 0.0390973815928508  0.0390973815928508\\
0.04    0.6 0.0563002294969392  0.0563002294969392\\
0.04    0.7 0.0766308679309348  0.0766308679309348\\
0.04    0.8 0.10008929689484    0.10008929689484\\
0.04    0.9 0.126675516388657   0.126675516388657\\
0.04    1   0.156389526412386   0.156389526412386\\
0.04    1.1 0.189231326958224   0.189231326958224\\
0.04    1.2 0.225200918052232   0.225200918052232\\
0.04    1.3 0.264298299625844   0.264298299625844\\
0.04    1.4 0.306523471724472   0.306523471724472\\
0.04    1.5 0.351876434351998   0.351876434351998\\
0.04    1.6 0.400357187508615   0.400357187508615\\
0.06    0.1 0.00229665158772114 0.00229665158772114\\
0.06    0.2 0.00918660635088458 0.00918660635088458\\
0.06    0.3 0.0206698642792481  0.0206698642792481\\
0.06    0.4 0.0367464253828938  0.0367464253828938\\
0.06    0.5 0.0574162896619585  0.0574162896619585\\
0.06    0.6 0.0826794571164541  0.0826794571164541\\
0.06    0.7 0.112535927746386   0.112535927746386\\
0.06    0.8 0.146985701551756   0.146985701551756\\
0.06    0.9 0.186028778532566   0.186028778532566\\
0.06    1   0.229665158688816   0.229665158688816\\
0.06    1.1 0.277894842012705   0.277894842012705\\
0.06    1.2 0.330717828530292   0.330717828530292\\
0.06    1.3 0.388134118173011   0.388134118173011\\
0.06    1.4 0.450143710986276   0.450143710986276\\
0.06    1.5 0.516746606973967   0.516746606973967\\
0.06    1.6 0.587942806136277   0.587942806136277\\
0.08    0.1 0.00303220449594347 0.00303220449594347\\
0.08    0.2 0.0121288179837739  0.0121288179837739\\
0.08    0.3 0.027289840453249   0.027289840453249\\
0.08    0.4 0.048515271914451   0.048515271914451\\
0.08    0.5 0.0758051123675164  0.0758051123675164\\
0.08    0.6 0.109159361812458   0.109159361812458\\
0.08    0.7 0.148578020249279   0.148578020249279\\
0.08    0.8 0.194061087677984   0.194061087677984\\
0.08    0.9 0.245608564098574   0.245608564098574\\
0.08    1   0.303220449511048   0.303220449511048\\
0.08    1.1 0.366896743907606   0.366896743907606\\
0.08    1.2 0.436637447314307   0.436637447314307\\
0.08    1.3 0.512442559662584   0.512442559662584\\
0.08    1.4 0.594312080997851   0.594312080997851\\
0.08    1.5 0.682246011323989   0.682246011323989\\
0.08    1.6 0.776244350641191   0.776244350641191\\
0.1 0.1 0.00377055252190335 0.00377055252190335\\
0.1 0.2 0.0150822100876134  0.0150822100876134\\
0.1 0.3 0.0339349726868879  0.0339349726868879\\
0.1 0.4 0.0603288403298091  0.0603288403298091\\
0.1 0.5 0.0942638130165135  0.0942638130165135\\
0.1 0.6 0.135739890747013   0.135739890747013\\
0.1 0.7 0.184757073521314   0.184757073521314\\
0.1 0.8 0.241315361339417   0.241315361339417\\
0.1 0.9 0.305414754201324   0.305414754201324\\
0.1 1   0.377055252107037   0.377055252107037\\
0.1 1.1 0.456236855048751   0.456236855048751\\
0.1 1.2 0.542959563052529   0.542959563052529\\
0.1 1.3 0.637223376049803   0.637223376049803\\
0.1 1.4 0.739028294085988   0.739028294085988\\
0.1 1.5 0.848374317164961   0.848374317164961\\
0.1 1.6 0.965261445286921   0.965261445286921\\
0.12    0.1 0.00451243489258604 0.00451243489258604\\
0.12    0.2 0.0180497395703441  0.0180497395703441\\
0.12    0.3 0.0406119140230322  0.0406119140230322\\
0.12    0.4 0.072198958260732   0.072198958260732\\
0.12    0.5 0.112810872283581   0.112810872283581\\
0.12    0.6 0.16244765609159    0.16244765609159\\
0.12    0.7 0.221109309684766   0.221109309684766\\
0.12    0.8 0.288795833063109   0.288795833063109\\
0.12    0.9 0.365507226226622   0.365507226226622\\
0.12    1   0.451243489175306   0.451243489175306\\
0.12    1.1 0.546004621901357   0.546004621901357\\
0.12    1.2 0.649790624430837   0.649790624430837\\
0.12    1.3 0.762601496695178   0.762601496695178\\
0.12    1.4 0.884437238739795   0.884437238739795\\
0.12    1.5 1.01529785056857    1.01529785056857\\
0.12    1.6 1.15518333218169    1.15518333218169\\
0.14    0.1 0.00525969968187338 0.00525969968187338\\
0.14    0.2 0.0210387987274935  0.0210387987274935\\
0.14    0.3 0.0473372971266183  0.0473372971266183\\
0.14    0.4 0.0841551948893296  0.0841551948893296\\
0.14    0.5 0.131492492015764   0.131492492015764\\
0.14    0.6 0.189349188505935   0.189349188505935\\
0.14    0.7 0.257725284359845   0.257725284359845\\
0.14    0.8 0.336620779577499   0.336620779577499\\
0.14    0.9 0.426035674158897   0.426035674158897\\
0.14    1   0.52596996810404    0.52596996810404\\
0.14    1.1 0.636423661405126   0.636423661405126\\
0.14    1.2 0.757396754088215   0.757396754088215\\
0.14    1.3 0.88888924608474    0.88888924608474\\
0.14    1.4 1.03090113744011    1.03090113744011\\
0.14    1.5 1.18343242815822    1.18343242815822\\
0.14    1.6 1.34648311823925    1.34648311823925\\
0.16    0.1 0.00601956490652337 0.00601956490652337\\
0.16    0.2 0.0240782596260935  0.0240782596260935\\
0.16    0.3 0.0541760841484682  0.0541760841484682\\
0.16    0.4 0.0963130384837294  0.0963130384837294\\
0.16    0.5 0.150489122632014   0.150489122632014\\
0.16    0.6 0.216704336593334   0.216704336593334\\
0.16    0.7 0.294958680367695   0.294958680367695\\
0.16    0.8 0.385252153955098   0.385252153955098\\
0.16    0.9 0.487584757355546   0.487584757355546\\
0.16    1   0.601956490569039   0.601956490569039\\
0.16    1.1 0.728367353587774   0.728367353587774\\
0.16    1.2 0.866817346437813   0.866817346437813\\
0.16    1.3 1.01730646905059    1.01730646905059\\
0.16    1.4 1.17983472147151    1.17983472147151\\
0.16    1.5 1.35440210370447    1.35440210370447\\
0.16    1.6 1.54100861574965    1.54100861574965\\
0.18    0.1 0.00684707188551121 0.00684707188551121\\
0.18    0.2 0.0273882875420448  0.0273882875420448\\
0.18    0.3 0.0616236469593587  0.0616236469593587\\
0.18    0.4 0.109553150147535   0.109553150147535\\
0.18    0.5 0.17117679710671    0.17117679710671\\
0.18    0.6 0.246494587836897   0.246494587836897\\
0.18    0.7 0.335506522338099   0.335506522338099\\
0.18    0.8 0.43821260061032    0.43821260061032\\
0.18    0.9 0.554612822653561   0.554612822653561\\
0.18    1   0.684707188467822   0.684707188467822\\
0.18    1.1 0.828495698045302   0.828495698045302\\
0.18    1.2 0.985978351412062   0.985978351412062\\
0.18    1.3 1.15715514849953    1.15715514849953\\
0.18    1.4 1.34202608935313    1.34202608935313\\
0.18    1.5 1.54059117397673    1.54059117397673\\
0.18    1.6 1.75285040237053    1.75285040237053\\
0.2 0.1 200 200\\
0.2 0.2 200 200\\
0.2 0.3 200 200\\
0.2 0.4 200 200\\
0.2 0.5 200 200\\
0.2 0.6 200 200\\
0.2 0.7 200 200\\
0.2 0.8 200 200\\
0.2 0.9 200 200\\
0.2 1   200 200\\
0.2 1.1 200 200\\
0.2 1.2 200 200\\
0.2 1.3 200 200\\
0.2 1.4 200 200\\
0.2 1.5 200 200\\
0.2 1.6 200 200\\
};
\end{axis}
\end{tikzpicture}%

\end{document}

What it looks like (pgfplots ouput):

enter image description here

What it should look like (Matlab output):

enter image description here

23
  • Welcome ! What are the points at z=200 for ? – BambOo Mar 20 '20 at 12:27
  • Thank you for the fast repy. I have to be honest I generated this plot with matlab2tikz. Because these values are normaly much higher, I had to limit the Z-values. Higher values threw errors in Latex. – Flo Mar 20 '20 at 12:31
  • Do you mean that you actually do not need these ponts ? – BambOo Mar 20 '20 at 12:32
  • 1
    When you plot the figure in Matlab itself, do you get different colors? If yes, then could you add the Matlab output to your question? If not, then maybe the problem is on the Matlab side? – Marijn Mar 20 '20 at 12:32
  • 1
    @Schrödinger'scat just checked the example, datas are actually the same between c and z. – BambOo Mar 20 '20 at 12:41
2

Based on Schrödinger'scat's great comments, you can use z expr={min(\thisrow{z}, 1.8) to truncate your z values. I've removed the c column, as it just duplicates z values.

Note: the output may be incorrect with some PDF viewers, so if you don't see the same as below, try another viewer. ImageMagick's convert tool produced the image below and Okular displays it fine, at least.

\documentclass[tikz, border=2mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
  width=2.018in,
  height=1.698in,
  at={(0.339in,0.229in)},
  scale only axis,
  xmin=0,
  xmax=0.2,
  tick align=outside,
  xlabel style={font=\color{white!15!black}},
  xlabel={$X$},
  ymin=0,
  ymax=1.6,
  ylabel style={font=\color{white!15!black}},
  ylabel={$Y$},
  zlabel style={font=\color{white!15!black}},
  zlabel={$Z$},
  view={-45}{45},
  axis background/.style={fill=white},
  axis x line*=bottom,
  axis y line*=left,
  axis z line*=left,
  xmajorgrids,
  ymajorgrids,
  zmajorgrids
]

\addplot3[
  surf, shader=interp, draw=black, z buffer=sort, colormap/jet, mesh/rows=10,
  ] table[row sep=crcr, z expr={min(\thisrow{z}, 1.8)}] {
%
x       y   z\\
0.02    0.1 0.000836933014342034\\
0.02    0.2 0.00334773205736814\\
0.02    0.3 0.00753239711883609\\
0.02    0.4 0.013390928208828\\
0.02    0.5 0.0209233253274806\\
0.02    0.6 0.030129588474806\\
0.02    0.7 0.0410097176508092\\
0.02    0.8 0.0535637128554926\\
0.02    0.9 0.0677915740888575\\
0.02    1   0.0836933013509049\\
0.02    1.1 0.101268894633832\\
0.02    1.2 0.1205183539637\\
0.02    1.3 0.141441679271941\\
0.02    1.4 0.16403887060397\\
0.02    1.5 0.188309927963666\\
0.02    1.6 0.214254851351224\\
0.04    0.1 0.00156389526495684\\
0.04    0.2 0.00625558105982738\\
0.04    0.3 0.0140750573743694\\
0.04    0.4 0.025022324218665\\
0.04    0.5 0.0390973815928508\\
0.04    0.6 0.0563002294969392\\
0.04    0.7 0.0766308679309348\\
0.04    0.8 0.10008929689484\\
0.04    0.9 0.126675516388657\\
0.04    1   0.156389526412386\\
0.04    1.1 0.189231326958224\\
0.04    1.2 0.225200918052232\\
0.04    1.3 0.264298299625844\\
0.04    1.4 0.306523471724472\\
0.04    1.5 0.351876434351998\\
0.04    1.6 0.400357187508615\\
0.06    0.1 0.00229665158772114\\
0.06    0.2 0.00918660635088458\\
0.06    0.3 0.0206698642792481\\
0.06    0.4 0.0367464253828938\\
0.06    0.5 0.0574162896619585\\
0.06    0.6 0.0826794571164541\\
0.06    0.7 0.112535927746386\\
0.06    0.8 0.146985701551756\\
0.06    0.9 0.186028778532566\\
0.06    1   0.229665158688816\\
0.06    1.1 0.277894842012705\\
0.06    1.2 0.330717828530292\\
0.06    1.3 0.388134118173011\\
0.06    1.4 0.450143710986276\\
0.06    1.5 0.516746606973967\\
0.06    1.6 0.587942806136277\\
0.08    0.1 0.00303220449594347\\
0.08    0.2 0.0121288179837739\\
0.08    0.3 0.027289840453249\\
0.08    0.4 0.048515271914451\\
0.08    0.5 0.0758051123675164\\
0.08    0.6 0.109159361812458\\
0.08    0.7 0.148578020249279\\
0.08    0.8 0.194061087677984\\
0.08    0.9 0.245608564098574\\
0.08    1   0.303220449511048\\
0.08    1.1 0.366896743907606\\
0.08    1.2 0.436637447314307\\
0.08    1.3 0.512442559662584\\
0.08    1.4 0.594312080997851\\
0.08    1.5 0.682246011323989\\
0.08    1.6 0.776244350641191\\
0.1 0.1 0.00377055252190335\\
0.1 0.2 0.0150822100876134\\
0.1 0.3 0.0339349726868879\\
0.1 0.4 0.0603288403298091\\
0.1 0.5 0.0942638130165135\\
0.1 0.6 0.135739890747013\\
0.1 0.7 0.184757073521314\\
0.1 0.8 0.241315361339417\\
0.1 0.9 0.305414754201324\\
0.1 1   0.377055252107037\\
0.1 1.1 0.456236855048751\\
0.1 1.2 0.542959563052529\\
0.1 1.3 0.637223376049803\\
0.1 1.4 0.739028294085988\\
0.1 1.5 0.848374317164961\\
0.1 1.6 0.965261445286921\\
0.12    0.1 0.00451243489258604\\
0.12    0.2 0.0180497395703441\\
0.12    0.3 0.0406119140230322\\
0.12    0.4 0.072198958260732\\
0.12    0.5 0.112810872283581\\
0.12    0.6 0.16244765609159\\
0.12    0.7 0.221109309684766\\
0.12    0.8 0.288795833063109\\
0.12    0.9 0.365507226226622\\
0.12    1   0.451243489175306\\
0.12    1.1 0.546004621901357\\
0.12    1.2 0.649790624430837\\
0.12    1.3 0.762601496695178\\
0.12    1.4 0.884437238739795\\
0.12    1.5 1.01529785056857\\
0.12    1.6 1.15518333218169\\
0.14    0.1 0.00525969968187338\\
0.14    0.2 0.0210387987274935\\
0.14    0.3 0.0473372971266183\\
0.14    0.4 0.0841551948893296\\
0.14    0.5 0.131492492015764\\
0.14    0.6 0.189349188505935\\
0.14    0.7 0.257725284359845\\
0.14    0.8 0.336620779577499\\
0.14    0.9 0.426035674158897\\
0.14    1   0.52596996810404\\
0.14    1.1 0.636423661405126\\
0.14    1.2 0.757396754088215\\
0.14    1.3 0.88888924608474\\
0.14    1.4 1.03090113744011\\
0.14    1.5 1.18343242815822\\
0.14    1.6 1.34648311823925\\
0.16    0.1 0.00601956490652337\\
0.16    0.2 0.0240782596260935\\
0.16    0.3 0.0541760841484682\\
0.16    0.4 0.0963130384837294\\
0.16    0.5 0.150489122632014\\
0.16    0.6 0.216704336593334\\
0.16    0.7 0.294958680367695\\
0.16    0.8 0.385252153955098\\
0.16    0.9 0.487584757355546\\
0.16    1   0.601956490569039\\
0.16    1.1 0.728367353587774\\
0.16    1.2 0.866817346437813\\
0.16    1.3 1.01730646905059\\
0.16    1.4 1.17983472147151\\
0.16    1.5 1.35440210370447\\
0.16    1.6 1.54100861574965\\
0.18    0.1 0.00684707188551121\\
0.18    0.2 0.0273882875420448\\
0.18    0.3 0.0616236469593587\\
0.18    0.4 0.109553150147535\\
0.18    0.5 0.17117679710671\\
0.18    0.6 0.246494587836897\\
0.18    0.7 0.335506522338099\\
0.18    0.8 0.43821260061032\\
0.18    0.9 0.554612822653561\\
0.18    1   0.684707188467822\\
0.18    1.1 0.828495698045302\\
0.18    1.2 0.985978351412062\\
0.18    1.3 1.15715514849953\\
0.18    1.4 1.34202608935313\\
0.18    1.5 1.54059117397673\\
0.18    1.6 1.75285040237053\\
0.2 0.1 200\\
0.2 0.2 200\\
0.2 0.3 200\\
0.2 0.4 200\\
0.2 0.5 200\\
0.2 0.6 200\\
0.2 0.7 200\\
0.2 0.8 200\\
0.2 0.9 200\\
0.2 1   200\\
0.2 1.1 200\\
0.2 1.2 200\\
0.2 1.3 200\\
0.2 1.4 200\\
0.2 1.5 200\\
0.2 1.6 200\\
};
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

This answer is "Community wiki" but so far legitimately belongs to Schrödinger's cat; I'm posting it so that one can see the outcome and can improve it if necessary (Schrödinger's cat initially had a viewer problem).

13
  • Looks good! (I still do not know what this f(x) in point meta=f(x) is.) – user194703 Mar 20 '20 at 14:18
  • @Schrödinger'scat As I understand it, it's y in 2D and z in 3D plots... – frougon Mar 20 '20 at 14:19
  • Thanks! (I still do not see which key I set wrong... ;-) (Actually, when I compile your code I get the output of this post. Strange.) – user194703 Mar 20 '20 at 14:22
  • @Schrödinger'scat Quote from the manual for point meta=f(x) (User Input Format for Point Meta, p. 231 for pgfplots 1.16): “This will use the last available coordinate, in other words: it is the same as y for two dimensional plots and z for three dimensional ones.” For the rest, use diff or M-x ediff-buffers. ;-) – frougon Mar 20 '20 at 14:24
  • Yes, thanks. Actually this is not even necessary, you can also use point meta=\thisrow{z}, say. What is necessary is to use the right viewer.... – user194703 Mar 20 '20 at 14:31

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.