6

For the following MWE, I would like to annotate an interval (e.g. [2, 4]) as illustrated in the desired output.

Additionally, I need to know how to stylize the vertical lines and the horizontal arrow?

\documentclass[border=1cm]{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
    xmin=0,xmax=5
    ]
        \addplot[mark=none,blue] {x^2};

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

enter image description here

Diaa
  • 9,599
  • See e.g. https://tex.stackexchange.com/questions/332151/drawing-dotted-line-on-a-plot/332154#332154 As for stylizing, in general that's the same as for any line you draw with TikZ, but can you be more specific? – Torbjørn T. Apr 17 '17 at 05:52

1 Answers1

9

For the text of the annotation, you can use the decorations.text tikz library.

For stylizing, you can add any options you need in the \draw command, here is an example:

\documentclass[border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{decorations.text}    

\begin{document}
    \begin{tikzpicture}
    \begin{axis}[
    xmin=0,xmax=5
    ]
    \addplot[mark=none,blue] {x^2};
    \draw[red, dashed] (2, \pgfkeysvalueof{/pgfplots/ymin}) -- (2, \pgfkeysvalueof{/pgfplots/ymax});
    \draw[green, thick, dotted] (4, \pgfkeysvalueof{/pgfplots/ymin}) -- (4, \pgfkeysvalueof{/pgfplots/ymax});
    \draw[blue, very thick, {Stealth}-{Stealth}, postaction={decoration={raise=3pt, text along path, text={some text},text align=center}, decorate}] (2,5) -- (4,5);
    \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

Edit:

As required by the OP, I've created the style \myline for the vertical lines (and also a \myarrow one for the arrow of the interval).

Moreover, I exaggerated with the tikz mania. As Torbjørn T. correctly pointed out, using a text along path is overkill for a straight line. You can just put a node above the path, with no need of the decorations.text library.

\documentclass[border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\usetikzlibrary{arrows.meta}
\tikzset{%
    myline/.style = {green, very thick, dashed},
    myarrow/.style = {blue, very thick, {Stealth}-{Stealth}},
}

\begin{document}
    \begin{tikzpicture}
    \begin{axis}[
    xmin=0,xmax=5
    ]
    \addplot[mark=none,blue] {x^2};
    \draw[myline] (2, \pgfkeysvalueof{/pgfplots/ymin}) -- (2, \pgfkeysvalueof{/pgfplots/ymax});
    \draw[myline] (4, \pgfkeysvalueof{/pgfplots/ymin}) -- (4, \pgfkeysvalueof{/pgfplots/ymax});
    \draw[myarrow] (2,5) -- node[above] {some text} (4,5);
    \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

Second edit:

I've created a pic with three args: initial x, final x and height of the arrow y (this last one could be substituted by a fixed value, if it is always the same, modifying the pic to use only two args).

\documentclass[border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\usetikzlibrary{arrows.meta}
\tikzset{%
    myline/.style = {green, very thick, dashed},
    myarrow/.style = {blue, very thick, {Stealth}-{Stealth}},
    pics/myint/.style n args={3}{code={%  
            \draw[myline] (#1, \pgfkeysvalueof{/pgfplots/ymin}) -- (#1, \pgfkeysvalueof{/pgfplots/ymax});
            \draw[myline] (#2, \pgfkeysvalueof{/pgfplots/ymin}) -- (#2, \pgfkeysvalueof{/pgfplots/ymax});
            \draw[myarrow] (#1,#3) -- node[above] {some text} (#2,#3);
    }},
}

\begin{document}
    \begin{tikzpicture}
    \begin{axis}[
    xmin=0,xmax=5
    ]
    \addplot[mark=none,blue] {x^2};
    \pic {myint={2}{4}{5}};
    \end{axis}
    \end{tikzpicture}
\end{document} 

Of course, with \pic {myint={2}{4}{5}};, the output is the same as the previous one.

CarLaTeX
  • 62,716
  • Isn't text along path a bit overkill for a straight line like that? \draw (a) -- node[above] {some text} (b); (+1 though) – Torbjørn T. Apr 17 '17 at 06:50
  • @DiaaAbidou See my edit. Thank you for accepting my answer! (The vertical lines looks like they are blue and green but if you look at the pdf they are green, it's only a bad rendering of the converted image). – CarLaTeX Apr 17 '17 at 13:12
  • I deleted my comment before your edit because I figured it out :), but thanks for consideration. I have another question if you don't mind: how can I define the three draw commands of the vertical lines in addition to the arrow in the preamble as a macro or snippet, then all what i need is to type the name of this macro or snippet inside the axis environment?. I hope my question is not vague. – Diaa Apr 17 '17 at 13:21
  • In other words, I need one command that accepts three arguments x1, x2 and the text above the arrow, then this command draws the vertical lines and arrow, and prints the text above the arrow. – Diaa Apr 17 '17 at 13:26
  • I came up with my own command, but it fails, so do you mind asking it as a new question so you can help me there? – Diaa Apr 17 '17 at 13:42
  • Many thanks for your answer, it is really close to my need with two minor issues: 1- you can find here my output of your second edit, what is the reason of misalignment of the arrow text? I tried node[above,align=center], but it did nothing. 2- Since I have different plots with different y scales, for the arrow height, how can I make it relative to the x axis using absolute distance like 1cm or percentage of the total plot height? Sorry for my many question and thanks for patience. – Diaa Apr 17 '17 at 13:58
  • 1
  • Strange, it worked for me. 2. Surely it's possible but I can't answer at once. 1. and 2. I think it's better if you post a (new) follow-up question, with this one linked, and a complete minimal working example. Otherwise, the edits become too many...
  • – CarLaTeX Apr 17 '17 at 15:23
  • Thanks, I already posted my new question. – Diaa Apr 17 '17 at 15:40