7
\documentclass[border=5mm] {standalone}
\usepackage{pgfplots, pgfplotstable}


\begin{document}

\begin{tikzpicture}
\pgfplotstableread{ % Read the data into a table macro
Label   First   Second  Third
10      0.1     0.3     0.3
20      0.2     0.3     0.3
30      0.3     0.4     0.5
40      0.3     0.5     0.8
160     0.5     0.9     1.5
}\datatable

\begin{axis}[
    xbar stacked,   % Stacked horizontal bars
    xmin=0,         % Start x axis at 0
    ytick=data,     % Use as many tick labels as y coordinates
    yticklabels from table={\datatable}{Label}  % Get the labels from the Label column of the \datatable
]
\addplot [fill=yellow] table [x=First, y expr=\coordindex] {\datatable};    % Plot the "First" column against the data index
\addplot [fill=green!70!blue]table [x=Second, y expr=\coordindex] {\datatable};
\addplot [fill=red!80!yellow] table [x=Third, y expr=\coordindex] {\datatable};
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

Now how do I draw a vertical black line parallel to the Y axis going through 1 on x axis? And how do I put a label on this line?

2
  • 1
    You might use something like \draw[dotted](axis description cs:0.3,0)--(axis description cs:0.3,1); as per 4.8.1 Placement of Axis Descriptions in the manual. Fix the 0.3 to locate the line in the right position. – Claudio Fiandrino Mar 4 '13 at 14:23
  • You have a couple of options, see How can I add a zero line to a plot?. All of the approaches there allow you to specify the value exactly. – Jake Mar 4 '13 at 14:27
12

You can use Joseph's approach from How can I add a zero line to a plot?: By setting extra x ticks={1}, you can define an additional tick that can be formatted differently from the others. By setting extra x tick style={xmajorgrids=true}, you can switch on the grid line for this tick, resulting in a vertical line:

\documentclass[border=5mm] {standalone}
\usepackage{pgfplots, pgfplotstable}
\pgfplotsset{compat=newest}

\begin{document}

\begin{tikzpicture}
\pgfplotstableread{ % Read the data into a table macro
Label   First   Second  Third
10      0.1     0.3     0.3
20      0.2     0.3     0.3
30      0.3     0.4     0.5
40      0.3     0.5     0.8
160     0.5     0.9     1.5
}\datatable

\begin{axis}[
    xbar stacked,   % Stacked horizontal bars
    xmin=0,         % Start x axis at 0
    ytick=data,     % Use as many tick labels as y coordinates
    yticklabels from table={\datatable}{Label},  % Get the labels from the Label column of the \datatable
    extra x ticks={1},  % Add an extra tick at position x=1
    extra x tick style={    % Set styles that only apply to the extra tick
            xticklabel pos=right,   % Put the label on the right ( = top) side of the plot
            xticklabels={Special!}, % Set the label text
            xmajorgrids=true            % Draw grid line
        }
]
\addplot [fill=yellow] table [x=First, y expr=\coordindex] {\datatable};    % Plot the "First" column against the data index
\addplot [fill=green!70!blue]table [x=Second, y expr=\coordindex] {\datatable};
\addplot [fill=red!80!yellow] table [x=Third, y expr=\coordindex] {\datatable};
\end{axis}
\end{tikzpicture}

\end{document}
2
  • thanks...how can I name the x axis as 'time in ms' and y axis as 'number of iterations'? – epsilon8 Mar 5 '13 at 11:24
  • xlabel= ylabel= – epsilon8 Mar 5 '13 at 11:31

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.