2

I would like to add space around the image in a legend. MWE:

\documentclass{standalone}

\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[legend columns=-1]
    \addplot {x^2};
    \addlegendentry{Parabola}
    \addplot[only marks] {x};
    \addlegendentry{Line}
\end{axis}
\end{tikzpicture}

\end{document}

Current Output:

current

Desired Output:

enter image description here

The best approach I have come up with is to make give the second series a white line in addition to its black marks, but this looks odd if the background is not white or the line crosses over other series.

2

You can do whatever you want by defining your own legend style if you don't want to tweak all the time.

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest,
  my legend/.style={legend image code/.code={%
    \draw[##1]plot coordinates{(0cm,0cm)(0.3cm,0cm)(0.6cm,0cm)};}
  }
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[]
    \addplot {x^2};
    \addlegendentry{Parabola}
    \addplot[only marks,my legend] {x};
    \addlegendentry{Line}
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

More details are given on page 260 in the manual.

  • How can I get empty space using this command? If I make "my legend" style only plot one of the three points in your MWE, then the spacing is the same as before – user1476176 Jul 20 '18 at 0:17
  • @user1476176 For example you can add sharp plot,draw opacity=0 to the legend options just what you though before. – percusse Jul 20 '18 at 0:28
  • Could you edit your answer to show how to do this? It would make a better record for anyone curious in future. – user1476176 Jul 20 '18 at 20:11

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.