5


I have the following plot:
enter image description here
But I can not figure out how to make the colors of the legends match the actual color of their respective series (for some reason the legend is shown in only two colors, rather than six).

Also I would like to add to the legends box captions explaining the marks shape (as they refer to another piece of data), something like this:
enter image description here

This is the code I have created so far:

\begin{tikzpicture}
\begin{axis}[
    title={Colors},
    xlabel={1C [\%]},
    ylabel={AC [\%]},
    legend style={
        at={(0.5,-0.2)},
        anchor=north,
        legend columns=3
    },
    ymajorgrids=true,
    xmajorgrids=true,
    ymin=4.0,
    minor y tick num= 2,
    yminorgrids = true, 
    minor grid style=loosely dotted,
    scatter/classes={
        Diamond={mark=diamond*},
        HDiamond={mark=halfdiamond*},
        Square={mark=square*},
        Triangle={mark=triangle*}
    },
    only marks,
    scatter,
    mark size=3.5pt,
    scatter src=explicit symbolic,
]
\addplot [
    color=pink,
]
table[
    x=Pink-1C, 
    y=Pink-AC,
    meta=Method
]
{figures/example.dat};
\addlegendentry{Pink}

\addplot [
    color=blue,
]
table[
    x=Blue-1C, 
    y=Blue-AC,
    meta=Method
]
{figures/example.dat};
\addlegendentry{Blue}

\addplot [
    color=green,
]
table[
    x=Green-1C, 
    y=Green-AC,
    meta=Method
]
{figures/example.dat};
\addlegendentry{Green}


\addplot [
    color=cyan,
]
table[
    x=Cyan-1C, 
    y=Cyan-AC,
    meta=Method
]
{figures/example.dat};
\addlegendentry{Cyan}


%
\addplot [
    color=red,
]
table[
    x=Red-1C, 
    y=Red-AC,
    meta=Method
]
{figures/example.dat};
\addlegendentry{Red}


%
\addplot [
    color=orange,
]
table[
    x=Orange-1C, 
    y=Orange-AC,
    meta=Method
]
{figures/example.dat};
\addlegendentry{Orange}

\end{axis}
\end{tikzpicture}

And this is the dat file from which I am importing the data:

Method Pink-1C Pink-AC Green-1C Green-AC Blue-1C Blue-AC Cyan-1C Cyan-AC Red-1C Red-AC Orange-1C Orange-AC
Square 24.75 4.8 32.28 7.39 32.65 7.53 31.96 7.18 33.57 8 33.19 7.08
Triangle 31.38 5.55 31.33 6.33 31.63 6.11 30.95 6.56 31.7 6.36 32.12 5.54
Diamond 28.55 5.42 29.31 6.69 29.23 6.5 29.03 6.9 33.51 7.07 28.57 6.06
HDiamond 29.19 5.66 29.5 7.13 29.41 6.93 29.2 7.28 32.77 7.86 29.41 6.69

1 Answer 1

7

When you are using scatter/classes these classes are plotted in the legend. You defined 4 classes why you get first the 4 classes in pink, because that is the first color you used, then the (4) classes are repeated in blue, which is the second color you used ...

So the trick is to use the (first) legend to create the entries of the classes, i.e. the symbols and create a second legend with the colors.

Here is one solution how this can be done. For more details on how it works please have a look at the comments in the code.

% used PGFPlots v1.15
\begin{filecontents*}{example.dat}
Method Pink-1C Pink-AC Green-1C Green-AC Blue-1C Blue-AC Cyan-1C Cyan-AC Red-1C Red-AC Orange-1C Orange-AC
Square 24.75 4.8 32.28 7.39 32.65 7.53 31.96 7.18 33.57 8 33.19 7.08
Triangle 31.38 5.55 31.33 6.33 31.63 6.11 30.95 6.56 31.7 6.36 32.12 5.54
Diamond 28.55 5.42 29.31 6.69 29.23 6.5 29.03 6.9 33.51 7.07 28.57 6.06
HDiamond 29.19 5.66 29.5 7.13 29.41 6.93 29.2 7.28 32.77 7.86 29.41 6.69
\end{filecontents*}
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \pgfplotscreateplotcyclelist{my colors}{
        % color for the legend
        black!50\\
        % color for the "real" plots
        pink\\
        blue\\
        green\\
        cyan\\
        red\\
        orange\\
    }
    \pgfplotsset{
        compat=1.3,
        cycle list name=my colors,
        legend cell align=left,
    }
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        title={Colors},
        xlabel={1C [\%]},
        ylabel={AC [\%]},
        ymin=4.0,
        minor y tick num=2,
        ymajorgrids=true,
        xmajorgrids=true,
        yminorgrids=true,
        minor grid style=loosely dotted,
        only marks,
        scatter,
        mark size=3.5pt,
        scatter src=explicit symbolic,
        table/meta=Method,
        scatter/classes={
            Diamond={mark=diamond*},
            HDiamond={mark=halfdiamond*},
            Square={mark=square*},
            Triangle={mark=triangle*}
        },
        % this legend only shows the defined scatter classes
        % (as it is stated in the manual)
        legend entries={
            Diamond,
            HDiamond,
            Square,
            Triangle%
        },
        legend pos=outer north east,
    ]

        % ---------------------------------------------------------------------
        % dummy plot for the legend
        % (make sure the expression values are outside the visible axis limits
        %  if this `\addplot' wouldn't be present. This requires at least
        %  setting one of the limits explicitly, i.e. in this case `xmin')
        \addplot table [
            x expr=0,
            y expr=0,
        ] {example.dat};
        % ---------------------------------------------------------------------

        % simplified the call for the data
        \foreach \i in {
            Pink,
            Blue,
            Green,
            Cyan,
            Red,
            Orange%
        }{
            \addplot table [
                x=\i-1C,
                y=\i-AC,
            ] {example.dat};
        }

    \end{axis}

    % this is a dummy `axis' environment only to create the second legend
    \begin{axis}[
        % set some axis limits and plot the coordinates outside that box
        % so they don't show up
        xmin=1,
        xmax=2,
        ymin=1,
        ymax=2,
        % of course we also don't want to show this axis
        hide axis,
        % we need only marks
        only marks,
        % state the legend entries for the second legend
        % (here we don't have scatter classes, so each `\addplot' gets its
        %  own entry in the legend)
        legend entries={
            ,       % the dummy plot should not show up in the legend
            pink,
            blue,
            green,
            cyan,
            red,
            orange%
        },
        % place it below the other legend
        % therefore we have to shift it down (manually)
        legend pos=outer north east,
        legend style={
            yshift=-60pt,
        },
    ]
        % just add some dummy plots to create the legend
        \foreach \i in {0,...,6} {
            \addplot+ [mark=*] coordinates { (0,0) };
        }
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .