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.



\pgfplotsset{width=16.5cm, height=5cm}
            date coordinates in=x,
            xlabel={Time horizon},
            stack plots=y,
            ylabel=Yield {[}\%{]},
            ymin=0, ymax=5,
            date ZERO = 2013-12-05,
            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) {};
                    at (axis cs:2014-10-24,2.55) {};
                    at (axis cs:2015-07-25,3.11) {};
                    at (axis cs:2015-10-24,3.30) {};


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.


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:


  • 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