7

When I draw some measured data with the tikzpicture and axis environment of pgfplots I have the problem that the lines and markers are not on the same layer. E.g. line 2 is drawn over line 1 but the marker of line 1 is drawn above line 2 but below the marker of line 2. Finally, I also want to draw two nodes in the figure. But these lines are getting below the markers, even if I plot them at the end. Here is my example where the blue and red markers are on top of all the other lines:

\documentclass[12pt]{standalone}

 \usepackage{pgfplots}
\pgfplotsset{compat=newest}
\pgfplotsset{plot coordinates/math parser=false}
\usetikzlibrary{plotmarks}

\begin{document}

\tikzset{every picture/.style={mark repeat=2}}
\begin{tikzpicture}
    \begin{axis}[%
        width=13pc,
        height=16pc,
        scale only axis,
        xmin=-150,
        xmax=150,
        xlabel={Magnetic field strength $H$ (A/m)},
        xmajorgrids,
        ymin=-0.2,
        ymax=0.2,
        ylabel={Magnetic Flux density $B$ (T)},
        ymajorgrids
        ]

        \addplot [
        color=red,
        solid,
        line width=1.0pt,
        mark size=2.5pt,
        mark=x,
        mark options={solid},
        forget plot
        ]
        table[row sep=crcr]{
        -50.5816497802734 -0.1065673828125\\
        -49.0505828857422 -0.105224609375\\
        -47.0889282226563 -0.10009765625\\
        -45.5736541748047 -0.10009765625\\
        -44.0070343017578 -0.10009765625\\
        -42.3993225097656 -0.09765625\\
        -40.689697265625 -0.092529296875\\
        -39.3039855957031 -0.092529296875\\
        -37.411865234375 -0.0899658203125\\
        -36.3816680908203 -0.088623046875\\
        -34.6704559326172 -0.0848388671875\\
        -33.4948883056641 -0.0836181640625\\
        -31.8468933105469 -0.0810546875\\
        -31.0220947265625 -0.0771484375\\
        -28.3865661621094 -0.0784912109375\\
        -33.0524749755859 -0.075927734375\\
        -69.1204986572266 -0.0911865234375\\
        -94.3042755126953 -0.1141357421875\\
        -113.249969482422 -0.138427734375\\
        -126.323394775391 -0.16650390625\\
        -134.85334777832 -0.189453125\\
        -141.303146362305 -0.2110595703125\\
        };

        \addplot [
        color=blue,
        solid,
        line width=1.0pt,
        mark size=1.8pt,
        mark=square,
        mark options={solid},
        forget plot
        ]
        table[row sep=crcr]{
        -78.3354034423828 -0.0433349609375\\
        -97.7914581298828 -0.0433349609375\\
        -92.4113616943359 -0.0623779296875\\
        -83.7226104736328 -0.05859375\\
        -104.369247436523 -0.0751953125\\
        -84.6027069091797 -0.076416015625\\
        -103.263214111328 -0.0789794921875\\
        -99.5595550537109 -0.096923828125\\
        -89.9598999023438 -0.096923828125\\
        -110.728988647461 -0.1083984375\\
        -90.9166259765625 -0.1134033203125\\
        -109.801498413086 -0.11474609375\\
        -106.663497924805 -0.132568359375\\
        -96.3883666992188 -0.132568359375\\
        -117.817138671875 -0.1466064453125\\
        -97.7511749267578 -0.150390625\\
        -116.598907470703 -0.154296875\\
        -113.685287475586 -0.1695556640625\\
        -104.640228271484 -0.168212890625\\
        -126.403182983398 -0.1810302734375\\
        -106.021209716797 -0.18994140625\\
        -126.411865234375 -0.188720703125\\
        -122.742980957031 -0.2103271484375\\
        };
        \addplot [
        color=green,
        solid,
        line width=1.0pt,
        mark size=2.5pt,
        mark=o,
        mark options={solid},
        forget plot
        ]
        table[row sep=crcr]{
        -94.0001068115234 -0.11993408203125\\
        -102.187194824219 -0.12750244140625\\
        -107.515151977539 -0.13775634765625\\
        -110.865661621094 -0.14544677734375\\
        -113.445907592773 -0.15435791015625\\
        -115.765426635742 -0.16583251953125\\
        -117.892974853516 -0.17474365234375\\
        -119.951797485352 -0.18243408203125\\
        -122.001922607422 -0.19256591796875\\
        -124.731475830078 -0.19891357421875\\
        -126.528793334961 -0.20916748046875\\
        };
        \draw [<->,thick] (axis cs:-120,-0.155) -- node[below]{d$H$} (axis cs:-30,-0.155) ;
        \draw [<->,thick] (axis cs:-26,-0.155) -- node[right]{d$B$} (axis cs:-26,-0.076) ;


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

Is there a way how I can put the markers on the same layer as their respective lines?

10

You can set clip mode=individual: That way, the plot markers are placed directly on top of their respective lines, and later plots are drawn on top of earlier plots:

\documentclass[12pt]{standalone}

 \usepackage{pgfplots}
\pgfplotsset{compat=newest}
\pgfplotsset{plot coordinates/math parser=false}
\usetikzlibrary{plotmarks}

\begin{document}

\tikzset{every picture/.style={mark repeat=2}}
\begin{tikzpicture}
    \begin{axis}[%
        width=13pc,
        height=16pc,
        scale only axis,
        xmin=-150,
        xmax=150,
        xlabel={Magnetic field strength $H$ (A/m)},
        xmajorgrids,
        ymin=-0.2,
        ymax=0.2,
        ylabel={Magnetic Flux density $B$ (T)},
        ymajorgrids,
        clip mode=individual
        ]

        \addplot [
        color=red,
        solid,
        line width=1.0pt,
        mark size=2.5pt,
        mark=x,
        mark options={solid},
        forget plot
        ]
        table[row sep=crcr]{
        -50.5816497802734 -0.1065673828125\\
        -49.0505828857422 -0.105224609375\\
        -47.0889282226563 -0.10009765625\\
        -45.5736541748047 -0.10009765625\\
        -44.0070343017578 -0.10009765625\\
        -42.3993225097656 -0.09765625\\
        -40.689697265625 -0.092529296875\\
        -39.3039855957031 -0.092529296875\\
        -37.411865234375 -0.0899658203125\\
        -36.3816680908203 -0.088623046875\\
        -34.6704559326172 -0.0848388671875\\
        -33.4948883056641 -0.0836181640625\\
        -31.8468933105469 -0.0810546875\\
        -31.0220947265625 -0.0771484375\\
        -28.3865661621094 -0.0784912109375\\
        -33.0524749755859 -0.075927734375\\
        -69.1204986572266 -0.0911865234375\\
        -94.3042755126953 -0.1141357421875\\
        -113.249969482422 -0.138427734375\\
        -126.323394775391 -0.16650390625\\
        -134.85334777832 -0.189453125\\
        -141.303146362305 -0.2110595703125\\
        };

        \addplot [
        color=blue,
        solid,
        line width=1.0pt,
        mark size=1.8pt,
        mark=square,
        mark options={solid},
        forget plot
        ]
        table[row sep=crcr]{
        -78.3354034423828 -0.0433349609375\\
        -97.7914581298828 -0.0433349609375\\
        -92.4113616943359 -0.0623779296875\\
        -83.7226104736328 -0.05859375\\
        -104.369247436523 -0.0751953125\\
        -84.6027069091797 -0.076416015625\\
        -103.263214111328 -0.0789794921875\\
        -99.5595550537109 -0.096923828125\\
        -89.9598999023438 -0.096923828125\\
        -110.728988647461 -0.1083984375\\
        -90.9166259765625 -0.1134033203125\\
        -109.801498413086 -0.11474609375\\
        -106.663497924805 -0.132568359375\\
        -96.3883666992188 -0.132568359375\\
        -117.817138671875 -0.1466064453125\\
        -97.7511749267578 -0.150390625\\
        -116.598907470703 -0.154296875\\
        -113.685287475586 -0.1695556640625\\
        -104.640228271484 -0.168212890625\\
        -126.403182983398 -0.1810302734375\\
        -106.021209716797 -0.18994140625\\
        -126.411865234375 -0.188720703125\\
        -122.742980957031 -0.2103271484375\\
        };
        \addplot [
        color=green,
        solid,
        line width=1.0pt,
        mark size=2.5pt,
        mark=o,
        mark options={solid},
        forget plot
        ]
        table[row sep=crcr]{
        -94.0001068115234 -0.11993408203125\\
        -102.187194824219 -0.12750244140625\\
        -107.515151977539 -0.13775634765625\\
        -110.865661621094 -0.14544677734375\\
        -113.445907592773 -0.15435791015625\\
        -115.765426635742 -0.16583251953125\\
        -117.892974853516 -0.17474365234375\\
        -119.951797485352 -0.18243408203125\\
        -122.001922607422 -0.19256591796875\\
        -124.731475830078 -0.19891357421875\\
        -126.528793334961 -0.20916748046875\\
        };
        \draw [<->,thick] (axis cs:-120,-0.155) -- node[below]{d$H$} (axis cs:-30,-0.155) ;
        \draw [<->,thick] (axis cs:-26,-0.155) -- node[right]{d$B$} (axis cs:-26,-0.076) ;


    \end{axis}
\end{tikzpicture}%
\end{document}
  • Perfect,exactly as I need it! – Andy Sweden Jun 28 '13 at 16:18
8

Similarly, if you add \pgfplotsset{set layers} after \begin{tikzpicture} and before \begin{axis}, then give the mark layer=like plot option, the markers are grouped with the plot they belong to. The result looks like

enter image description here

  • Perfect, this is also working. I really would like to rate both answers as my accepted one. – Andy Sweden Jun 28 '13 at 16:21

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.