18

I have a plot created using pgfplots in which I have included a legend. However, I find that the marker sizes in the legend are the same size as the markers used in the main plot. I think this is a bit distracting, and I usually prefer markers in the legend to be slightly smaller than markers in the main plot. (Similarly, legend text to be smaller than labels).

The manual says that the legend is implemented as a TikZ matrix; however, I couldn't figure out how to change the marker symbol sizes selectively. My current line in the code that determiens legend style is:

legend style={draw=black,rounded corners=3pt,thin,at={(0.03,0.97)},anchor=north west}

Any changes I make to these option only influences the outer box. Modifying the line to

legend style={draw=black,rounded corners=3pt,thin,at={(0.03,0.97)},anchor=north west, 
                 font=\small}

only changes the font of the text in the legend itself.

Is there a way to selectively change the marker size in the legend alone?

In a related question, If I mark a particular point with a small rectangle around it (using the draw command), and then want to include that rectangle in the legend to describe what the marking means, how is that done?

Update following @cmhughes recommendation

Here is the modified MWE:

\documentclass{standalone}
\usepackage{pgfplots, amsmath, amssymb}
\pgfplotsset{width=7cm, compat=newest}
\pgfplotsset{every axis/.append style={
                    legend style={font=\tiny,line width=5pt,mark size=10pt},
                    }}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            xmode =                             log,
            ymode =                             log,
            xlabel =                            {A},
            ylabel =                            {B},
            ]
            \addplot[
                color =                             red,
                mark =                              x,
                mark size =                         2,
                only marks,
            ] 
            coordinates 
            {
                (2,2.8559703)
                (30,30.5301677)
                (400,400.3050655)
                (5000,5000.1413136)
                (60000,60000.0322865)
            };
            \addlegendentry{some data}
        \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

I went through the code once more and have pinned down the problem to the addplot[mark size = 2] option. Commenting out this line changes the marker size in the legend exclusively (Marker size, line widths exaggerated for illustration):

enter image description here

But I would like to have the option of changing the marker size in the plot according to my needs, and independently scaling down all legend marker sizes.

1 Answers1

18

As demonstrated by @Jake in pgfplots: Legends in multiple y-axis plot overlapping you can use \addlegendimage{<plot options>} before your plot.

For example

\addlegendimage{red,dashed};
\addlegendentry{Different!};
\addplot[blue,thick]{x^2}; 

gives

screenshot

MWE

\documentclass{standalone}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
    \begin{axis}
        \addlegendimage{red,dashed};
        \addlegendentry{Different!};
        \addplot[blue,thick]{x^2}; 
    \end{axis}
\end{tikzpicture}
\end{document}

Update following the comments

If you want a global change, then perhaps something like

\pgfplotsset{every axis/.append style={
                    legend style={font=\tiny,line width=.5pt,mark size=.6pt},
                    }}

will be appropriate.

screenshot

Or, finally, thanks to the guru @Jake, you could try

\pgfplotsset{every axis/.append style={
        legend style={ font=\tiny, mark options={scale=0.5} }, }
cmhughes
  • 100,947
  • Thanks; this solution works. But it is still a bit tedious. I now have to manually enter the mark type, color, etc. based on what I use in the plot. I often tend to have many different \addplot[] commands on the same axes and end up cycling colors/marker styles. I'm not sure if this solution can automate the legend creation too? I'm guessing I have to plot the data, observe the output and then go back and add in a legend? – G. H. Hardly Jan 08 '13 at 02:23
  • @G.H.Hardly but you did say, selectively :) I took this to mean that you only wanted to do it once or twice... It sounds like you want a solution that applies globally to all legends? – cmhughes Jan 08 '13 at 02:30
  • Oh, I see I wasn't clear enough. By selectively, I meant change the marker size only in legend, and not in the actual plot. Essentially, I just want to size down markers in the legend box.

    For part II of my question (right at the end), your solution is exactly what I am looking for.

    – G. H. Hardly Jan 08 '13 at 03:32
  • @G.H.Hardly no problem :) I have updated my answer- let me know what you think – cmhughes Jan 08 '13 at 03:49
  • Adding that line to the preamble has no effect on the marker size. The legend fonts are now tiny, and changing the line width changes the 'edge thickness' of the markers, but the size itself is unchanged. For example, if I use the 'x' marker, the edges of the lines become thicker, but the size of the x is the same (I know this because the x becomes some sort of a rotated square---which makes sense if you thicken the edge lines alone). – G. H. Hardly Jan 08 '13 at 04:09
  • @G.H.Hardly please update your question with a complete MWE – cmhughes Jan 08 '13 at 04:16
  • 1
    To scale down all the markers by a certain factor (if that's what the OP wants), you can globally set every axis/.append style={ legend style={ font=\tiny, mark options={scale=0.5} }, } – Jake Jan 08 '13 at 07:19
  • Perfect! This scales down the legend marker size irrespective of size options of the markers in the actual plot. – G. H. Hardly Jan 08 '13 at 18:25
  • Thanks, Jake and @cmshughes. Is there a similar 'scale' command to scale down the whole legend? Outer box, markers, text, lines, outer box---the whole lot? every axis/.append style={ legend style={ font=\tiny, scale=0.5 }, } doesn't seem to do anything. – G. H. Hardly Jan 08 '13 at 22:24
  • 1
    How to change the length of the line? – orezvani Oct 20 '14 at 23:10
  • 1
    Change legend line length in pgfplots: http://tex.stackexchange.com/a/210259 –  Mar 10 '15 at 13:45