[Tex/LaTex] Adding horizontal lines to pgfplots bar

bar chartpgfplots

I am using a pgfplots stacked bar to display the aggregated energy demand of a houshold and the associated price. When the energy demand exceeds a certain threshold, than a higher price has to be paid. This is visualized by the color red and blue of the bars. The threshold is displayed by the thick red horizontal line.

My problem is, that I want this red line to exceed the width of the bar, so that it's width is circa 120 percent of the width of the bar.
Is there any possibility to achieve this?
Thanks

enter image description here

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
  ymin=0,ymax=4,
  samples=3,
  enlarge x limits={abs=0.5},
  bar width=0.6,
  ybar stacked,
  legend pos=south east,
    every axis/.append style={font=\footnotesize},
]

\draw[red, very thick] (axis cs:0.7,2) -- (axis cs:1.3,2);
\draw[red, very thick] (axis cs:1.7,2.5) -- (axis cs:2.3,2.5);
\draw[red, very thick] (axis cs:2.7,2.5) -- (axis cs:3.3,2.5);

\addplot
coordinates 
    {(1,1) (2,2.5) (3,1.5)};

\addplot
coordinates 
    {(1,0) (2,1) (3,0)};


\legend{low price, high price}
\end{axis}
\end{tikzpicture}
\end{document}

Best Answer

You can compute the excess width and adjust the coordinates by 10% on each side of the drawn lines to get the lines to be 20% wider:

enter image description here

Code:

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}

\newcommand{\MyBarWidth}{0.6}%
\pgfmathsetmacro{\ExcessBarWitdh}{0.1*\MyBarWidth}%

\begin{document}
\begin{tikzpicture}
\begin{axis}[
  ymin=0,ymax=4,
  samples=3,
  enlarge x limits={abs=0.5},
  bar width=\MyBarWidth,
  ybar stacked,
  legend pos=south east,
    every axis/.append style={font=\footnotesize},
]

\draw[red, very thick] (axis cs:0.7-\ExcessBarWitdh,2) -- (axis cs:1.3+\ExcessBarWitdh,2);
\draw[red, very thick] (axis cs:1.7-\ExcessBarWitdh,2.5) -- (axis cs:2.3+\ExcessBarWitdh,2.5);
\draw[red, very thick] (axis cs:2.7-\ExcessBarWitdh,2.5) -- (axis cs:3.3+\ExcessBarWitdh,2.5);

\addplot
coordinates 
    {(1,1) (2,2.5) (3,1.5)};

\addplot
coordinates 
    {(1,0) (2,1) (3,0)};


\legend{low price, high price}
\end{axis}
\end{tikzpicture}
\end{document}
Related Question