3

I'm using matlab2tikz for exporting Matlab plots to tikz which works very well. I have now made a plot with grid lines, unfortunately the grid lines are in the foreground, i.e. in front of my plot.

In the tikz the only thing which has changed when including the grid lines is xmajorgrids and ymajorgrids added to the \begin{axis}[].

How can I move the grid lines to the background?

By the way, I have added some transparency (alpha) to the plots. Here is the tikz:

% This file was created by matlab2tikz.
%
\definecolor{mycolor1}{rgb}{0.00000,0.70000,0.70000}%
\definecolor{mycolor2}{rgb}{0.98040,0.50200,0.44710}%
%
\begin{tikzpicture}

\begin{axis}[%
width=0.951\figurewidth,
height=\figureheight,
at={(0\figurewidth,0\figureheight)},
scale only axis,
separate axis lines,
every outer x axis line/.append style={white!15!black},
every x tick label/.append style={font=\color{white!15!black}},
xmin=0,
xmax=1000,
xtick={0,100,200,300,400,500,600,700,800,900,1000},
xlabel={x},
xmajorgrids,
every outer y axis line/.append style={white!15!black},
every y tick label/.append style={font=\color{white!15!black}},
ymin=0,
ymax=8000,
ytick={0,1000,2000,3000,4000,5000,6000,7000,8000},
ylabel={y},
ymajorgrids,
axis background/.style={fill=white},
title style={font=\bfseries},
axis on top,
legend style={legend cell align=left,align=left,fill=none,draw=none},
title style={font=\titelfont},xlabel style={font=\axisfont},ylabel style={font=\axisfont},legend style={font=\legendfont},yticklabel style={font=\tickfont},xticklabel style={font=\tickfont},width=\figurewidth,height=\figureheight
]
\addplot[fill=mycolor1,fill opacity=0.3,draw=black,ybar interval,area legend] plot table[row sep=crcr] {%
x   y\\
0   7588\\
10  1209\\
20  388\\
30  196\\
40  126\\
50  78\\
60  56\\
70  40\\
80  43\\
90  28\\
100 17\\
110 16\\
120 9\\
130 13\\
140 12\\
150 17\\
160 6\\
170 7\\
180 5\\
190 17\\
200 8\\
210 8\\
220 8\\
230 2\\
240 6\\
250 4\\
260 4\\
270 3\\
280 6\\
290 3\\
300 3\\
310 5\\
320 0\\
330 2\\
340 0\\
350 1\\
360 4\\
370 0\\
380 2\\
390 0\\
400 1\\
410 2\\
420 1\\
430 1\\
440 2\\
450 2\\
460 1\\
470 2\\
480 3\\
490 0\\
500 0\\
510 0\\
520 3\\
530 0\\
540 0\\
550 2\\
560 1\\
570 0\\
580 0\\
590 0\\
600 0\\
610 2\\
620 1\\
630 1\\
640 0\\
650 0\\
660 0\\
670 0\\
680 1\\
690 0\\
700 2\\
710 1\\
720 2\\
730 1\\
740 0\\
750 2\\
760 0\\
770 0\\
780 0\\
790 0\\
800 1\\
810 0\\
820 1\\
830 0\\
840 0\\
850 1\\
860 0\\
870 0\\
880 0\\
890 0\\
900 0\\
910 0\\
920 1\\
930 1\\
940 0\\
950 1\\
960 1\\
};
\addlegendentry{test1};

\addplot[fill=mycolor2,fill opacity=0.3,draw=black,ybar interval,area legend] plot table[row sep=crcr] {%
x   y\\
30  206\\
60  1044\\
90  1256\\
120 1041\\
150 869\\
180 679\\
210 505\\
240 436\\
270 349\\
300 323\\
330 271\\
360 232\\
390 175\\
420 164\\
450 141\\
480 137\\
510 104\\
540 105\\
570 102\\
600 98\\
630 62\\
660 69\\
690 51\\
720 57\\
750 59\\
780 54\\
810 51\\
840 52\\
870 55\\
900 28\\
930 30\\
960 34\\
990 12\\
1020    12\\
};
\addlegendentry{test2};

\addplot[fill=red!20!lime,fill opacity=0.3,draw=black,ybar interval,area legend] plot table[row sep=crcr] {%
x   y\\
60  7\\
90  107\\
120 243\\
150 449\\
180 467\\
210 501\\
240 501\\
270 473\\
300 491\\
330 419\\
360 353\\
390 384\\
420 322\\
450 279\\
480 260\\
510 241\\
540 227\\
570 184\\
600 163\\
630 155\\
660 149\\
690 122\\
720 127\\
750 109\\
780 112\\
810 134\\
840 86\\
870 83\\
900 80\\
930 85\\
960 78\\
990 13\\
1020    13\\
};
\addlegendentry{test3};

\end{axis}
\end{tikzpicture}%
  • While code snippets are useful in explanations, it is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. This is especially important for tikz/pgfplots as there are numerous libraries. – Peter Grill Jul 22 '16 at 0:03
  • 3
    One of the effects of using the axis on top key is that grid lines are drawn on top of plot lines (this is not the standard behavior). – Paul Gessler Jul 22 '16 at 0:52
  • 1
    @PaulGessler Could you post an answer, please? – CarLaTeX Jun 3 '17 at 22:44
  • @CarLaTeX (finally) done! Thank you for the reminder. :-) – Paul Gessler May 17 '18 at 21:15
  • @PaulGessler Upvoted :) – CarLaTeX May 18 '18 at 1:51
2

This is a known difference between MATLAB plots and matlab2tikz output. See line 1023 of the source:

if ~any(hasGrid) && ~any(hasMinorGrid)
    % When specifying 'axis on top', the axes stay above all graphs (which is
    % default MATLAB behavior), but so do the grids (which is not default
    % behavior).
    %TODO: use proper grid ordering
    if m2t.args.strict
        options = opts_add(options, 'axis on top');
    end
    % FIXME: axis background, axis grid, main, axis ticks, axis lines, axis tick labels, axis descriptions, axis foreground
end

In your specific case, drawing the axis underneath the plots won't cause any harm, so you can use

matlab2tikz(...,'extraAxisOptions',{'axis on top=false'});

where ... is the existing list of arguments of your call to matlab2tikz.

Here's some example output, removing some data and styling not relevant to the problem at hand. It also shows some of the benefits of writing your own plot code, namely styles to reduce duplicated code and improve clarity.

I personally recommend exporting bare data files from MATLAB and let pgfplots import those. This means the style of the plots in your document stays inside your document. Only the critical outputs of your MATLAB code (the data itself) is shared between.

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{
  compat=1.15,
  my bar plot/.style={
    grid=major,
    fill=#1,
    fill opacity=0.3,
    draw=black,
    ybar interval,
    area legend,
  },
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[%
  xmin=0,
  xmajorgrids,
  ymin=0,
  ymajorgrids,
  %axis on top, % this is what causes grid lines to be drawn on top – commented out to prevent this
  legend style={legend cell align=left,align=left,fill=none,draw=none},
]
\addplot[my bar plot=teal] plot table[row sep=crcr] {%
x   y\\
0   2000\\
10  1209\\
20  388\\
30  196\\
40  126\\
50  78\\
60  56\\
70  40\\
80  43\\
90  28\\
100 17\\
};
\addlegendentry{test1};

\addplot[my bar plot=red] plot table[row sep=crcr] {%
x   y\\
30  206\\
60  1044\\
90  1256\\
120 1041\\
};
\addlegendentry{test2};

\addplot[my bar plot=red!20!lime] plot table[row sep=crcr] {%
x   y\\
60  7\\
90  107\\
120 243\\
};
\addlegendentry{test3};

\end{axis}
\end{tikzpicture}%
\end{document}

enter image description here

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.