3

I've just put a lot of effort in the following tikz image for Wikiepdia (see commons link):

enter image description here

However, there are two small things that I can't get fixed:

  1. When you look at the bottom left corner where the two axes meet you notice that those lines are not connected. How do I get them connected?
  2. The red plot lies on the axis. How can I get the axes over the plot?

Source

\documentclass[varwidth=true, border=5pt]{article}
\usepackage[active,tightpage]{preview}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning} 
\usepackage{helvet}
\usepackage[eulergreek]{sansmath}

\pgfmathdeclarefunction{gauss}{2}{%
  \pgfmathparse{1/(sqrt(2*pi*#2))*exp(-((x-#1)^2)/(2*#2))}%
}
% to be used: \gauss(mean)(variance)

\pgfplotsset{
tick label style = {font=\sansmath\sffamily},
every axis label/.append style={font=\sffamily\footnotesize},
}

\begin{document}
\begin{preview}
\tikzstyle{plotA}=[ultra thick,red!90!black]
\tikzstyle{plotB}=[ultra thick,cyan!50!black]
\begin{tikzpicture}
    \begin{axis}[
        width=13.5cm,
        height=8.625cm,
        % Grid
        grid = major,
        % size
        xmin= 0,     % start the diagram at this x-coordinate
        xmax= 125,   % end   the diagram at this x-coordinate
        ymin= 0,     % start the diagram at this y-coordinate
        ymax= 0.045, % end   the diagram at this y-coordinate
        % Legende
        legend style={
            font=\large\sansmath\sffamily,
            at={(0.5,-0.18)},
            anchor=north,
            legend cell align=left,
            legend columns=-1,
            column sep=0.5cm
        },
        % Ticks
        tick align=inside,
        minor tick style={thick},
        scaled y ticks = false,
        xtick={40,70,80},
        ytick=\empty,
        % Axis
        axis lines = middle,
        axis line style = very thick,
        xlabel=$x$,
        x label style={at={(axis description cs:0.5,0)},
                       anchor=north,
                       font=\boldmath\Large},
        ylabel=$f(x)$,
        y label style={at={(axis description cs:0,0.5)},
                       anchor=south,
                       rotate=90,
                       font=\boldmath\Large},
        ]

        \coordinate (plotAcoord) at (axis cs:70,0.024);
        \coordinate (plotAcoordY) at (axis cs:0,0.024);
        \draw[plotA,thick, dashed](plotAcoord -| plotAcoordY) -- (plotAcoord);

        \coordinate (plotBcoord) at (axis cs:70,0.0004);
        \coordinate (plotBcoordY) at (axis cs:0,0.0004);
        \draw[plotB,thick, dashed](plotBcoord -| plotBcoordY) -- (plotBcoord);

        \node[above right=0cm and 0cm of plotAcoordY,color=red!90!black] (f1y) {$f_{I}(70)$};
        \node[above right=0cm and 0cm of plotBcoordY,color=cyan!50!black] (f2y) {$f_{II}(70)$};

        % Plots of the distributions
        \addplot [domain=0:120,samples=200,plotA] {gauss(80,100)};
        \addplot [domain=0:120,samples=200,plotB] {gauss(40,100)};

        % Mark points
        \addplot+[plotA,
                  samples at={70},
                  mark=*,
                  mark options={scale=1, fill=red!90!black}] {gauss(80,100)};
        \addplot+[plotB,
                  samples at={70},
                  mark=*,
                  mark options={scale=1, fill=cyan!50!black}] {gauss(40,100)};
        \coordinate (a) at (axis cs:70,0.024);
        \draw[black,thick](a |- current plot begin) -- (a);

        % Add labels
       \node[plotA,above] at (axis cs:102,0.03){\Large $\mathcal{N}(80, 100)$};
       \node[plotB,above] at (axis cs:20,0.03){\Large $\mathcal{N}(40, 100)$};
      % \addlegendentry{$\mu=80,\enskip \sigma^2 = 100$}
      % \addlegendentry{$\mu=40,\enskip \sigma^2 = 100$}
    \end{axis} 
\end{tikzpicture}
\end{preview}
\end{document}
4

If you add the lines

    axis on top,% Question 2
    axis line style = {very thick,shorten <=-0.5\pgflinewidth}, %Question 1

in your %Axis option section, then you are done.

enter image description here

2
  • I've just noticed that axis on top makes also the grid to be over the plot. What I was searching is explained here: tikz/pgfplots advanced z-order axis/grid – Martin Thoma Aug 20 '14 at 22:28
  • @moose If it doesn't fit the bill, you can use explicit layers and organize everything pretty clearly. The manual has all the layer orders, – percusse Aug 20 '14 at 23:07

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.