Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Problem: line stops at given coordinates, ymax is not obeyed.

\documentclass[a4paper,10pt]{report}
\usepackage[left=1.5cm,top=2cm,right=1.5cm,bottom=1.5cm]{geometry}
\usepackage[table,usenames,dvipsnames]{xcolor}
\usepackage{tikz}
\usepackage{pgfplots}

\pgfplotsset{
    width=\textwidth,
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar,ymax=100,enlarge y limits={upper,value=1},
height=8cm,
enlargelimits=0.25,
ylabel={Uptime \%},
symbolic x coords={Mar '12,Apr '12,May '12},
xtick=data,
nodes near coords=\rotatebox{90}{\scriptsize\pgfmathprintnumber\pgfplotspointmeta},
]
\addplot[draw=blue,fill=blue!40!white] coordinates      {(Mar '12,100)      (Apr '12,100)       (May '12,100)}; 
\addplot[draw=Salmon,fill=Salmon!40!white] coordinates      {(Mar '12,100)      (Apr '12,100)       (May '12,100)}; 
\addplot[draw=yellow,fill=yellow!40!white] coordinates      {(Mar '12,99.86)    (Apr '12,99.47)     (May '12,99.37)}; 
\addplot[draw=RawSienna,fill=RawSienna!40!white] coordinates    {(Mar '12,100)      (Apr '12,100)       (May '12,99.98)};
\addplot[draw=purple,fill=purple!40!white] coordinates      {(Mar '12,99.69)    (Apr '12,98.79)     (May '12,97.91)}; 
\addplot[draw=cyan,fill=cyan!40!white] coordinates      {(Mar '12,100)      (Apr '12,100)       (May '12,100)}; 
\addplot[draw=magenta,fill=magenta!40!white] coordinates    {(Mar '12,100)      (Apr '12,100)       (May '12,99.96)}; 
\addplot[draw=LimeGreen,fill=LimeGreen!40!white] coordinates    {(Mar '12,100)      (Apr '12,100)       (May '12,99.99)}; 
\addplot[draw=red,fill=red!40!white] coordinates        {(Mar '12,100)      (Apr '12,100)       (May '12,99.99)}; 
\addplot[red,sharp plot] coordinates {(Mar '12,98) (May '12,98)} node[above] at (axis cs:Apr '12,98) {KPI};

\end{axis}
\end{tikzpicture}

\end{document}

Now I like to have a continuous line at 98% without the labels but with the KPI label, and the y axis should stop at 100. Is this possible?

share|improve this question

1 Answer 1

up vote 5 down vote accepted

You can adapt the solution from How can I add a zero line to a plot? to add the red line:

\draw [red] ({rel axis cs:0,0}|-{axis cs:May '12,98}) -- ({rel axis cs:1,0}|-{axis cs:May '12,98}) node [pos=0.33, above] {KPI};

will draw a red line across the whole plot width with a label at one third of the length. Note that the x coordinate May '12 isn't actually used for anything here, but you still need to supply a valid value.

The reason your ymax isn't obeyed is that you're calling enlargelimits=0.25, which overrides the ymax. Also, you're setting enlarge y limits={upper,value=1}, which also increases the upper y limit. Replace those options with enlarge x limits=0.25, and your upper y limit will be obeyed. In that case, you'll probably want to set x axis lines*=left, so only the bottom x axis line will be drawn (the * switches off the arrow tip that would otherwise be used).

\documentclass[a4paper,10pt]{report}
\usepackage[left=1.5cm,top=2cm,right=1.5cm,bottom=1.5cm]{geometry}
\usepackage[table,usenames,dvipsnames]{xcolor}
\usepackage{tikz}
\usepackage{pgfplots}

\pgfplotsset{
    width=\textwidth,
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar,ymax=100,
height=8cm,
enlarge x limits=0.25,
axis x line*=left,
ylabel={Uptime \%},
symbolic x coords={Mar '12,Apr '12,May '12},
xtick=data,
nodes near coords=\rotatebox{90}{\scriptsize\pgfmathprintnumber\pgfplotspointmeta},
]
\addplot[draw=blue,fill=blue!40!white] coordinates      {(Mar '12,100)      (Apr '12,100)       (May '12,100)}; 
\addplot[draw=Salmon,fill=Salmon!40!white] coordinates      {(Mar '12,100)      (Apr '12,100)       (May '12,100)}; 
\addplot[draw=yellow,fill=yellow!40!white] coordinates      {(Mar '12,99.86)    (Apr '12,99.47)     (May '12,99.37)}; 
\addplot[draw=RawSienna,fill=RawSienna!40!white] coordinates    {(Mar '12,100)      (Apr '12,100)       (May '12,99.98)};
\addplot[draw=purple,fill=purple!40!white] coordinates      {(Mar '12,99.69)    (Apr '12,98.79)     (May '12,97.91)}; 
\addplot[draw=cyan,fill=cyan!40!white] coordinates      {(Mar '12,100)      (Apr '12,100)       (May '12,100)}; 
\addplot[draw=magenta,fill=magenta!40!white] coordinates    {(Mar '12,100)      (Apr '12,100)       (May '12,99.96)}; 
\addplot[draw=LimeGreen,fill=LimeGreen!40!white] coordinates    {(Mar '12,100)      (Apr '12,100)       (May '12,99.99)}; 
\addplot[draw=red,fill=red!40!white] coordinates        {(Mar '12,100)      (Apr '12,100)       (May '12,99.99)};
\draw [red] ({rel axis cs:0,0}|-{axis cs:May '12,98}) -- ({rel axis cs:1,0}|-{axis cs:May '12,98}) node [pos=0.33, above] {KPI};
\end{axis}
\end{tikzpicture}

\end{document}
share|improve this answer
    
Thanx!! this was very helpful!! Also many thanx for the clear explanation of the errors I made :-) –  Berry Jun 6 '12 at 16:19
    
@Berry: Pleasure! Welcome to the site! –  Jake Jun 6 '12 at 16:20
    
({rel axis cs:0,0}|-{axis cs:May '12,98}) -- ({rel axis cs:1,0}|-{axis cs:May '12,98}) pretty clever trick, the line spans the how axis regardless of the axis cs value. –  alfC May 6 '13 at 21:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.