编辑散点图图例(颜色和标记)

编辑散点图图例(颜色和标记)


我有以下情节:
在此处输入图片描述
但我不知道如何使图例的颜色与各自系列的实际颜色相匹配(由于某种原因,图例仅显示两种颜色,而不是六种)。

另外,我想在图例框标题中添加解释标记形状的说明(因为它们引用了另一部分数据),如下所示:
在此处输入图片描述

这是迄今为止我创建的代码:

\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}

这是我从中导入数据的 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

答案1

当您使用scatter/classes这些类时,它们会绘制在图例中。您定义了 4 个类,为什么您首先得到粉红色的 4 个类,因为这是您使用的第一个颜色,然后 (4) 个类以蓝色重复,这是您使用的第二种颜色...

因此,诀窍是使用(第一个)图例来创建类别的条目,即符号,并使用颜色创建第二个图例。

以下是实现此目的的一种解决方案。有关其工作原理的更多详细信息,请查看代码中的注释。

% 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}

该图显示了上述代码的结果

相关内容