3

I am transitioning to using pgfplots to do most of my plots these days, however I am not always able to get the plots looking as I want them to.

I am plotting some data points with error bars, and it would be great if I could get the legend to reflect this, e.g. similar to the ones produced by gnuplot. Here is basically what I want vs what I have (gnuplot on left, pgfplots on right)

what I want -> Gnuplot style legend what I get -> pgfplots legend

And the mandatory MWE basically copied from the manual but with a legend

    \documentclass{article}
    \usepackage{pgfplots}
    \pgfplotsset{compat=1.12}

    \begin{document}
    \begin{tikzpicture}
      \begin{axis} [
        legend style={
          at={(0.05,0.95)}, anchor=north west
        },
      ]
        \addplot+ [
          only marks,
          error bars/.cd,
            y dir=both, y fixed=0.1,
        ]
        coordinates {
          (0,0)
          (0.1,0.1)
          (0.2,0.2)
          (0.5,0.5)
          (1,1)
        };
        \addlegendentry{Data}
      \end{axis}
    \end{tikzpicture}
    \end{document}

I would assume it is possible to do this with \addlegendimage but I am not fluent enough in pgfplots to find out how to automatise this process for every node style.

Edit: Here is the result from compiling the MWE for future reference

  • A warm welcome to TeX.SE! The MWE really does not compile. – A Feldman Jun 28 '16 at 12:16
  • Sorry, I skipped the documentclass, fixed – Jonas Glesaaen Jun 28 '16 at 12:22
  • You should probably use \documentclass{article} see tex.stackexchange.com/questions/42114/… – A Feldman Jun 28 '16 at 12:25
  • Thanks, I have actually never used the minimal class before, it just felt appropriate for the question as it doesn't really have anything to do with the problem at hand. – Jonas Glesaaen Jun 28 '16 at 12:27
  • The problem with minimal is that people copy code from TeX.SE, so questions and answers with it are actually somewhat of a land mine. It is a complete disaster when used, by anyone other than an real expert, in a LaTeX document that has any text. – A Feldman Jun 28 '16 at 12:41
5

To be able to distinguish the legend drawing you can use the legend image post style which appends additional options to only the legend but not to the plot itself. And if only marks is given to a plot you can override it back via sharp plot. For the error bar notation you can use the | arrow head.

Hence you can just add the following to your axis options

legend image post style={sharp plot,|-|}

and it gives

enter image description here

If these don't satisfy your specs then you can basically draw your own legend image via legend image code explained in the manual.

  • @JonasGlesaaen My pleasure. – percusse Jun 28 '16 at 12:54

Your Answer

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

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