2

I'm very new to LaTeX, preparing my very first document in the environment and learning on the way. I've encountered a problem with one of my plots - I'm trying to add a trend line to a bar chart to showcase the linearity of changes. I've tried using the solution proposed Here - Drawing a trend line on a bar chart but to no success.

\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}


\begin{document}

\pgfplotsset{width=16.5cm, height=5cm}
\begin{center}
    \begin{tikzpicture}
        \begin{axis}[
            ymajorgrids,
            date coordinates in=x,
            xticklabel={\day.\month.\year},
            xlabel={Time horizon},
            stack plots=y,
            ylabel=Yield {[}\%{]},
            ytick={0,1,2,3,4},
            ymin=0, ymax=5,
            date ZERO = 2013-12-05,
            xtick={2013-12-05,2014-07-25,2014-10-24,2015-07-25,2015-10-24},
            every axis/.append style={font=\small,line width=0.5pt,tick style={line width=0.5pt}},
            ylabel near ticks,
            bar width = 4
            ]


            \addplot[ybar,fill=cyan] coordinates {
                (2013-12-05, 0)
                (2014-10-24, 2.55)
                (2015-07-25, 3.11)
                (2015-10-24, 3.30)};
            \addplot[ybar,fill=green] coordinates {
                (2014-07-25, 2.36)};

%           \addplot [thick, orange] coordinates {
%               (2014-07-25, 2.36)
%               (2014-10-24, 2.55)
%               (2015-07-25, 3.11)
%               (2015-10-24, 3.30)};


            \node[coordinate,label={\(r_{0.64} = 2.36\%\)}]
                    at (axis cs:2014-07-25,2.36) {};
            \node[coordinate,label={\(r_{0.88}\)}]
                    at (axis cs:2014-10-24,2.55) {};
            \node[coordinate,label={\(r_{1.64}\)}]
                    at (axis cs:2015-07-25,3.11) {};
            \node[coordinate,label={\(r_{1.88}\)}]
                    at (axis cs:2015-10-24,3.30) {};
        \end{axis}
    \end{tikzpicture}
\end{center}

\end{document}

When you uncomment the line plot, it returns an error: Sorry, pgfplots expects stacked plots to have exactly the same number of coordinates.

What I need is a straight line connecting the 4 bars I have. I tried putting the line plot code before the bar chart code, and it allows me to plot the line, but after that all bars are being plotted from the levels of the line, as shown in the picture.Line plotted before bars

Any help will be appreciated.

4

If the line is plotted, stack plots=y is still active. After disabling:

\addplot [thick, orange, stack plots=false] coordinates {
    (2014-07-25, 2.36)
    (2014-10-24, 2.55)
    (2015-07-25, 3.11)
    (2015-10-24, 3.30)};

I get:

Result

  • Thank you very much. This was simpler than I've thought. Have a good day! – user79910 Jun 10 '15 at 23:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy