1

What have I to modify in this code to get simple annotations ?

\begin{tikzpicture}
\begin{axis}[
xtick={45,46,...,55},
ytick={0,0.1,...,1},
width=18cm,
height=10cm,
grid=major,
minor x tick num=9,
%minor y tick num={1},
ymin=0,
ymax=1,
xmin=45,
xmax=55,
]
\addplot[mark=none, blue, line width=1.5pt] coordinates {(45,0)(47,0.1)(49,0.35)(51,0.775)(53,0.875)(55,1)};

\addplot[mark=none, dashed, red] coordinates {(0,0.5)(49.7,0.5)(49.7,0.5)(49.7,0)};
\node at (axis cs:49.7,-0.15){Me};

\addplot[mark=none, dashed, red] coordinates {(0,0.25)(48.2,0.25)(48.2,0.25)(48.2,0)};
\node at (axis cs:48.2,-0.15){$Q_1$};

\addplot[mark=none, dashed, red] coordinates {(0,0.75)(50.9,0.75)(50.9,0.75)(50.9,0)};
\node at (axis cs:50.9,-0.15){$Q_3$};

\end{axis}
\end{tikzpicture}
  • You are drawing the annotations outside the axis environment, so they are clipped. Add clip=false to the axis options and they will appear. (But then also your dashed lines will not be clipped any more, so you should adjust them, too.) – Stefan Pinnow Nov 13 '16 at 18:35
  • @StefanPinnow clip mode=individual? – Torbjørn T. Nov 13 '16 at 18:49
  • @TorbjørnT. of course, I always miss this ... (But that is most probably, because I have never used it before -- I think ;)) – Stefan Pinnow Nov 13 '16 at 19:11
3

As Torbjørn already stated in the comment below the question a "quick fix" to your problem would be to simply use clip mode=individual, but "your" way is -- in my opinion -- not the ideal solution to add annotations.

Here I present another method using extra ticks. For more details have a look at the comments in the code.

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \pgfplotsset{
        % use `compat' level 1.11 or higher so you don't need to add
        % `axis cs:' to each TikZ coordinate
        compat=1.11,
    }
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
%            % not needed, because this is the default with this scales
%            xtick={45,46,...,55},
            % and instead of applying the `ytick`s just give the "distance"
            ytick distance=0.1,
            %
            width=18cm,
            height=10cm,
            grid=major,
            minor x tick num=9,
            ymin=0,
            ymax=1,
            xmin=45,
            xmax=55,
            %
            % if you don't want to draw markers (anywhere), disable it globally
            no markers,
            % where do you want to draw the extra labels ...
            extra x ticks={48.2,49.7,50.9},
            extra x tick style={
                % and what should the labels state
                xticklabels={$Q_1$,Me,$Q_3$},
                % adjust the style so it fits your needs ...
                % you don't want to draw (extra) grid lines there
                grid=none,
                % you also don't want to draw extra ticks there
                major tick length=0pt,
                % and the labels should be moved below the "normal" ticklabels
                xticklabel style={
                    yshift=-3ex,
                },
                % to align the labels on the same baseline
                % (for your current labels that doesn't change anything,
                %  but if you change e.g. "Me" to "me" and "$Q_3$" to
                %  "${Q_3^2}^2$", you will see the difference, if you comment
                %  the next line)
                typeset ticklabels with strut,
            },
        ]
        \addplot[mark=none, blue, line width=1.5pt] coordinates {
            (45,0)(47,0.1)(49,0.35)(51,0.775)(53,0.875)(55,1)
        };

        % you only need _3_ coordinates to plot the dashed lines
        % and so you don't need to adapt the "lower" values when changing the
        % axis limits, just use them directly in the coordinates
        \addplot [dashed, red] coordinates {
            (\pgfkeysvalueof{/pgfplots/xmin},0.25)
            (48.2,0.25)
            (48.2,\pgfkeysvalueof{/pgfplots/ymin})
        };
        % but you can also do it with _2_ coordinates using TikZ's `\draw' command
        \draw [dashed, red]
            (\pgfkeysvalueof{/pgfplots/xmin},0.5) -| (49.7,\pgfkeysvalueof{/pgfplots/ymin})
        ;
        \draw [dashed, red]
            (\pgfkeysvalueof{/pgfplots/xmin},0.75) -| (50.9,\pgfkeysvalueof{/pgfplots/ymin})
        ;
        \end{axis}
    \end{tikzpicture}
\end{document}

image showing the result of above code

  • It seems ytick distance and typeset ticklabels with strut don't work (I've tried it with TeXmaker and Texwriter which is written for Android). – imotep81 Nov 15 '16 at 17:35
  • By commenting it, your method is right – imotep81 Nov 15 '16 at 17:37
  • What do you mean with "don't work"? Do you get an error message or does your output look other than mine when using above code? At least ytick distance was introduced in PGFPlots v1.13, so you would get an error message when you have an older version. – Stefan Pinnow Nov 15 '16 at 17:43
  • Right. .I get an error message – imotep81 Nov 16 '16 at 10:24
  • As already stated, most probably then you have an older PGFPlots version than v1.13. So after updating the issue should be solved. – Stefan Pinnow Nov 16 '16 at 10:29

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.