2

I am fairly new to using pgfplots and have been combing the manual for a couple hours now and I can't find an elegant solution to my problem. I would like to have two columns of legend entries and then one "label" underneath them all and centered. I managed to get it by playing with the [text width= ] settings, but I was wondering if there was a better way. MWE:

\documentclass[11pt,titlepage]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10,
                height=0.5\textwidth,
                width=0.8\textwidth,
                every axis/.append style={thick}}
\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
        legend pos=north west,
        legend columns=2,
        scaled y ticks = false,
        xlabel={Months},
        ylabel={Profit},
        yticklabel={\$ \pgfmathprintnumber{\tick} },
        ymin={-5500},
        xmin={0},
        xmax={5.5},
        ytick={-5500, 0, 5000, 10000, 15000},
        xtick={1,2,...,5},
        grid=major,
        ]
    \addplot[mark=none, color=red, domain=0:5.5] {900*x -5200};
    \addlegendentry{120}
    \addplot[mark=none, color=green, domain=0:5.5] {1900*x -5200};
    \addlegendentry{130}
    \addplot[mark=none, color=blue, domain=0:5.5] {2900*x -5200};
    \addlegendentry{140}
    \addplot[mark=none, color=orange, domain=0:5.5] {3900*x -5200};
    \addlegendentry{150}
    \addlegendimage{empty legend}
    \addlegendentry[text width=30pt]{Drinks/Day}
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
JayCle
  • 83
  • btw, is there a less annoying way to insert code than to go add 4 spaces to each line? – JayCle Apr 26 '14 at 14:37
  • If you are using Emacs with AUCTeX, you can hit tab and it will indent the line to the appropriate level. I imagine other editors do the same (at least that would be nice). – dustin Apr 26 '14 at 14:40
  • @JayCle: A tip: If you indent lines by 4 spaces, they'll be marked as a code sample. You can also highlight the code and click the "code" button (with "{}" on it). Or just highlight and press "CTRL-K"... –  Apr 26 '14 at 14:40
  • @ChristianHupfer he meant on the forum. I thought in his editor to achieve the indentation. My mistake. – dustin Apr 26 '14 at 14:42
  • @dustin: No problem at all ;-) –  Apr 26 '14 at 14:44
  • @ChristianHupfer Thanks. I tried that and it removed all of my code and left "enter code here." Maybe I did something wrong, though? – JayCle Apr 26 '14 at 14:47
  • @JayCle: Perhaps some little errors of yours? –  Apr 26 '14 at 14:51
  • You might find http://tex.stackexchange.com/questions/54794/using-a-pgfplots-style-legend-in-a-plain-old-tikzpicture useful. – John Kormylo Apr 26 '14 at 15:11
  • Would you like to have the label as part of the legend box, or would it be okay outside of it? – Paul Paulsen Feb 01 '15 at 18:44

1 Answers1

2

For the last two lines inside the axis environment you can also name the legend entry and position it relative to that. Line legend images are 0.6 cm by default hence you can tweak quicker.

    \addlegendimage{empty legend}
    \addlegendentry[name=l1,minimum height=5mm,]{}
\end{axis}
\node at ([xshift=0.35cm]l1) {Drink/Day};

enter image description here

percusse
  • 157,807