1

I am using matlab2tikz to produce colored surface plots. When I compile the TikZ file created, the colors are dark and washed out. I have looked at the posts

'parula' colormap in pgfplots

Custom colorbar not showing colors correctly

which seem to detail similar issues; however, I have been unsuccessful in adapting the 'fix' in these posts to my particular issue. Below is a snippet of my tikz file created with matlab2tikz as well as an image showing the output. How can I fix this?

\begin{tikzpicture}
\node[above] at (1.05\figurewidth,0.8\figureheight) {\color{white}x};
\begin{axis}[%
colormap access=piecewise const,
width=0.419\figurewidth,
height=0.419\figureheight,
at={(0\figurewidth,0.5\figureheight)},
scale only axis,
unbounded coords=jump,
xmin=-1.55,
xmax=1.55,
xtick={-1,0,1},
xticklabels={{},{},{}},
ymin=-1.55,
ymax=1.55,
ytick={-1,0,1},
ylabel={$y$},
y label style={at={(0.09,0.5)}},
axis background/.style={fill=white},
title style={font=\bfseries, at={(0.5,0.93)}},
title={$\psi_{2}(x,y,z)$},
xmajorgrids,
ymajorgrids,
legend style={legend cell align=left, align=left, draw=none}
]

\addplot[%
surf,
shader=faceted, colormap={mymap}{[1pt] rgb(0pt)=(0.2422,0.1504,0.6603); rgb(1pt)=(0.25039,0.164995,0.707614); rgb(2pt)=(0.257771,0.181781,0.751138); rgb(3pt)=(0.264729,0.197757,0.795214); rgb(4pt)=(0.270648,0.214676,0.836371); rgb(5pt)=(0.275114,0.234238,0.870986); rgb(6pt)=(0.2783,0.255871,0.899071); rgb(7pt)=(0.280333,0.278233,0.9221); rgb(8pt)=(0.281338,0.300595,0.941376); rgb(9pt)=(0.281014,0.322757,0.957886); rgb(10pt)=(0.279467,0.344671,0.971676); rgb(11pt)=(0.275971,0.366681,0.982905); rgb(12pt)=(0.269914,0.3892,0.9906); rgb(13pt)=(0.260243,0.412329,0.995157); rgb(14pt)=(0.244033,0.435833,0.998833); rgb(15pt)=(0.220643,0.460257,0.997286); rgb(16pt)=(0.196333,0.484719,0.989152); rgb(17pt)=(0.183405,0.507371,0.979795); rgb(18pt)=(0.178643,0.528857,0.968157); rgb(19pt)=(0.176438,0.549905,0.952019); rgb(20pt)=(0.168743,0.570262,0.935871); rgb(21pt)=(0.154,0.5902,0.9218); rgb(22pt)=(0.146029,0.609119,0.907857); rgb(23pt)=(0.138024,0.627629,0.89729); rgb(24pt)=(0.124814,0.645929,0.888343); rgb(25pt)=(0.111252,0.6635,0.876314); rgb(26pt)=(0.0952095,0.679829,0.859781); rgb(27pt)=(0.0688714,0.694771,0.839357); rgb(28pt)=(0.0296667,0.708167,0.816333); rgb(29pt)=(0.00357143,0.720267,0.7917); rgb(30pt)=(0.00665714,0.731214,0.766014); rgb(31pt)=(0.0433286,0.741095,0.73941); rgb(32pt)=(0.0963952,0.75,0.712038); rgb(33pt)=(0.140771,0.7584,0.684157); rgb(34pt)=(0.1717,0.766962,0.655443); rgb(35pt)=(0.193767,0.775767,0.6251); rgb(36pt)=(0.216086,0.7843,0.5923); rgb(37pt)=(0.246957,0.791795,0.556743); rgb(38pt)=(0.290614,0.79729,0.518829); rgb(39pt)=(0.340643,0.8008,0.478857); rgb(40pt)=(0.3909,0.802871,0.435448); rgb(41pt)=(0.445629,0.802419,0.390919); rgb(42pt)=(0.5044,0.7993,0.348); rgb(43pt)=(0.561562,0.794233,0.304481); rgb(44pt)=(0.617395,0.787619,0.261238); rgb(45pt)=(0.671986,0.779271,0.2227); rgb(46pt)=(0.7242,0.769843,0.191029); rgb(47pt)=(0.773833,0.759805,0.16461); rgb(48pt)=(0.820314,0.749814,0.153529); rgb(49pt)=(0.863433,0.7406,0.159633); rgb(50pt)=(0.903543,0.733029,0.177414); rgb(51pt)=(0.939257,0.728786,0.209957); rgb(52pt)=(0.972757,0.729771,0.239443); rgb(53pt)=(0.995648,0.743371,0.237148); rgb(54pt)=(0.996986,0.765857,0.219943); rgb(55pt)=(0.995205,0.789252,0.202762); rgb(56pt)=(0.9892,0.813567,0.188533); rgb(57pt)=(0.978629,0.838629,0.176557); rgb(58pt)=(0.967648,0.8639,0.16429); rgb(59pt)=(0.96101,0.889019,0.153676); rgb(60pt)=(0.959671,0.913457,0.142257); rgb(61pt)=(0.962795,0.937338,0.12651); rgb(62pt)=(0.969114,0.960629,0.106362); rgb(63pt)=(0.9769,0.9839,0.0805)}, mesh/rows=64]
table[point meta=\thisrow{c}] {%
art/eigenfunction_plots-1.tsv};

\end{axis}
\end{tikzpicture}

Here is the output of the TikZ file compared to hwo the plots look in MATLAB

enter image description here

  • As always, if you post code fragments which almost certainly cannot be made to compilable codes for most of the users, the waiting time for an answer can be long, or even infinite. While it it straightforward yet tedious to add the preamble and \end{document]. I'd assume that not too many have art/eigenfunction_plots-1.tsv, so not many can really use your code fragments. This is frustrating for those who are willing to help, and for you because you may not get an answer. –  Apr 09 '20 at 21:53
  • @Schrödinger'scat I understand that is frustrating and I really could use some help. My issue is that the .tsv file has a large number of points so I can't copy and paste it and SE does not allow me to upload a file either. I tried replicating this behavior using a small number of points; however, the problem disappears for less points. I am not sure what to do to help other help me on this problem. Any suggestions? – Aaron Hendrickson Apr 10 '20 at 11:54

2 Answers2

2

First and foremost remark

While I use matlab2tikz myself a lot, and appreciate this software very much, it must be reminded that some functions available in matlab are not in pgfplots and vice versa. Therefore, people must really pay attention when converting figures from matlab to latex using this tool, and it is not because matlab2tikz's developpers made some choices at some point that these choices necessarily reflect you desire out of the box, or that everything happening in matlab will look perfecly the same in pgfplots.

Regarding your problem itself

I believe this is the normal behavior with the shader=faceted so not really a problem. The shader=faceted options shows faces, with their borders, which are plotted as slightly darker lines. However, if you use a high density of grid points the size of each face reduces, and lines will tend to take more relative space and make the overall colors grayish.

Here is an example taken from the pgfplots manual (section 4.6.6 shader options) to illustrate this effect.

The first plot is computed using 20 grid points. You can well see the lines and the colors, which fully respect the colormap.

enter image description here

This second plot is now computed with 200 grid points (compile with lualatex if you want to reproduce, or you will overflow pdflatex's memory). This one clearly looks

dark and washed out

enter image description here

But actually, if you zoom in, you will see that the only gray areas are the lines.

enter image description here

So when you state that the problem disappears when you change the shader to flat, the problem disappears beacause you actually do not ask pgfplots to plot the lines.

enter image description here

Here is the code to reproduce if you want

% arara: lualatex
\documentclass[tikz]{standalone}   
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}
\begin{tikzpicture}
\begin{axis}[view={0}{90}]
\addplot3 [surf,shader=faceted,samples=200,domain=0:1] {x^2*y};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
    \begin{axis}[view={0}{90}]
    \addplot3 [surf,shader=faceted,samples=20,domain=0:1] {x^2*y};
    \end{axis}
    \end{tikzpicture}
    \begin{tikzpicture}
        \begin{axis}[view={0}{90}]
        \addplot3 [surf,shader=flat,samples=20,domain=0:1] {x^2*y};
        \end{axis}
        \end{tikzpicture}
\end{document}
BambOo
  • 8,801
  • 2
  • 20
  • 47
0

For anyone who ends up running into this, the problem was the shader=faceted option in \addplot[]. This was cause by the MATLAB command surf(xq,yq,vq,'EdgeColor','interp').

If instead we use the MATLAB command surf(xq,yq,vq,'EdgeColor','none'), the matlab2tikz output uses the shader=flat option in \addplot[] which fixed the problem.