2

Please take a look at my code. I am plotting values from a file and I want to add a marker at the x coordinate of 100 GHz.

\documentclass[convert={density=400,outext=.png}]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}[scale=0.5]
\begin{axis}[ymin=-30,ymax=10,enlargelimits=false,
title=Large Signal S-parameters of Pre-Amplifier, 
ylabel=S-parameters(dB),
xlabel=Frequency(GHz), 
legend style={legend pos= south east}, 
ytick={-30,-25,-20,-15,-10,-5,0,5,10},
grid=major]
\addplot +[smooth][mark=none, line width=1.5pt]
file{S21_preamp};
\addlegendentry{$S_{21}$}
\addplot +[smooth] [mark=none, line width=1.5pt]
file{S11_preamp};
\addlegendentry{$S_{11}$}
\draw [<-,thick](100,360) -- (120,370) node[above]{5.8dB} ;
\end{axis}
\end{tikzpicture}
\end{document}

Right now I have an arrow pointing to the x position. Is there a better way to do this?

percusse
  • 157,807
user29898
  • 507
  • 1
    Welcome to TeX.sx! Please add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – Qrrbrbirlbel Apr 29 '13 at 20:19
  • A tip: If you indent lines by 4 spaces, they'll be marked as a code sample. You can also highlight the code and click the "code" button (with "{}" on it). – texenthusiast Apr 29 '13 at 20:30
  • I can't add images, need more reputation for that – user29898 Apr 29 '13 at 20:33
  • As new user without image posting privileges simply include the image as normal and remove the ! in front of it to turn it into a link. A moderator or another user with edit privileges can then reinsert the ! to turn it into an image again. — More importantly, a sample of your data files would be helpful or at least a dummy plot or the right min and max settings, so that we can have the right limits. By the way, inside the axis environment, you can use the axis cs and the axis direction cs to use the coordinate of the plot. There is also extra x tick/extra x label (or similar). – Qrrbrbirlbel Apr 29 '13 at 21:37

2 Answers2

4

You could use the approach from pgfplots: Placing node on a specific x-position to place a coordinate at a desired x-coordinate along the plot without having to determine the y-coordinate manually (example adapted from Tom's answer):

\documentclass{standalone}
\usepackage{pgfplots}
\usetikzlibrary{arrows, intersections}

\makeatletter
\def\parsenode[#1]#2\pgf@nil{%
    \tikzset{label node/.style={#1}}
    \def\nodetext{#2}
}

\tikzset{
    add node at x/.style 2 args={
        name path global=plot line,
        /pgfplots/execute at end plot visualization/.append={
                \begingroup
                \@ifnextchar[{\parsenode}{\parsenode[]}#2\pgf@nil
            \path [name path global = position line #1-1]
                ({axis cs:#1,0}|-{rel axis cs:0,0}) --
                ({axis cs:#1,0}|-{rel axis cs:0,1});
            \path [xshift=1pt, name path global = position line #1-2]
                ({axis cs:#1,0}|-{rel axis cs:0,0}) --
                ({axis cs:#1,0}|-{rel axis cs:0,1});
            \path [
                name intersections={
                    of={plot line and position line #1-1},
                    name=left intersection
                },
                name intersections={
                    of={plot line and position line #1-2},
                    name=right intersection
                },
                label node/.append style={pos=1}
            ] (left intersection-1) -- (right intersection-1)
            node [label node]{\nodetext};
            \endgroup
        }
    },
    add node at y/.style 2 args={
        name path global=plot line,
        /pgfplots/execute at end plot visualization/.append={
                \begingroup
                \@ifnextchar[{\parsenode}{\parsenode[]}#2\pgf@nil
            \path [name path global = position line #1-1]
                ({axis cs:0,#1}-|{rel axis cs:0,0}) --
                ({axis cs:0,#1}-|{rel axis cs:1,1});
            \path [yshift=1pt, name path global = position line #1-2]
                ({axis cs:0,#1}-|{rel axis cs:0,0}) --
                ({axis cs:0,#1}-|{rel axis cs:1,1});
            \path [
                name intersections={
                    of={plot line and position line #1-1},
                    name=left intersection
                },
                name intersections={
                    of={plot line and position line #1-2},
                    name=right intersection
                },
                label node/.append style={pos=1}
            ] (left intersection-1) -- (right intersection-1)
            node [label node] {\nodetext};
            \endgroup
        }
    }
}


\begin{document}
\begin{tikzpicture}[scale=0.5]
\begin{axis}
[   ymin=5,
    ymax=25,
    enlargelimits=false,
    title=Large Signal S-parameters of Pre-Amplifier, 
    ylabel=S-parameters(dB),
    xlabel=Frequency(GHz), 
    legend style={legend pos= south east}, 
    ytick={5,10,...,25},
    grid=major,
]
\addplot +[smooth, mark=none, line width=1.5pt,
    add node at x={3}{[coordinate, name=freq3]}] coordinates {(1,10)(2,8)(3,11)(4,13)(5,9)};
\addlegendentry{$S_{21}$}
\addplot +[smooth, mark=none, line width=1.5pt] coordinates {(1,17)(2,20)(3,19)(4,21)(5,18)};
\addlegendentry{$S_{11}$}
\draw [stealth-, thick] (freq3) -- ++(0,6ex) node [above] {Label};
\end{axis}
\end{tikzpicture}
\end{document}
Jake
  • 232,450
3

You can use the axis coordinate system via (axis cs: x,y). I made up some sample data for testing. I also used a nicer arrow tip, you'll need the arrows library for that. Lastly for the tick labels you can use the {a,a+b,...,c} notation which will give you labels spaced b starting from a to c:

Code

\documentclass[convert={density=400,outext=.png}]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{arrows}
\begin{document}
\begin{tikzpicture}[scale=0.5]
\begin{axis}
[   ymin=5,
    ymax=25,
    enlargelimits=false,
    title=Large Signal S-parameters of Pre-Amplifier, 
    ylabel=S-parameters(dB),
    xlabel=Frequency(GHz), 
    legend style={legend pos= south east}, 
    ytick={5,10,...,25},
    grid=major,
]
\addplot +[smooth][mark=none, line width=1.5pt] coordinates {(1,10)(2,8)(3,11)(4,13)(5,9)};
\addlegendentry{$S_{21}$}
\addplot +[smooth] [mark=none, line width=1.5pt] coordinates {(1,17)(2,20)(3,19)(4,21)(5,18)};
\addlegendentry{$S_{11}$}
\draw [stealth-,thick] (axis cs:3,11) -- (axis cs:3,15) node[above]{5.8dB} ;
\end{axis}
\end{tikzpicture}
\end{document}

Output

enter image description here

Tom Bombadil
  • 40,123