PGFplot 图例中的点云符号

PGFplot 图例中的点云符号

我在绘图中有一个点云数据,一位审阅者评论说要将点云的图例条目从单个点更改为点云,或者至少是图例中的几个点。但是我不知道如何实现这一点。我知道我可以更改图例条目,但您如何将符号更改为包含多个点?

例如,MWE 在此处输入图片描述

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{amsmath}
\begin{document}


\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}


\begin{tikzpicture}

\begin{axis}[%
small,
height=3in,
axis equal image,
xmin=-0.3,
xmax=0.3,
y dir=reverse,
ymin=-1.15,
ymax=1.12,
name=plot1,
legend style={draw=black,fill=white,legend cell align=left,at={(axis cs:0.35,0)},anchor=west,font=\footnotesize}
]
\addplot+[
only marks,
mark=*,
mark size = 0.2pt,
mark options={solid}
]
table[row sep=crcr]{
0.0828301519283208 -0.973246408029514\\
0.0716818671371936 -0.962098123238387\\
0.0716818671371936 -0.95094983844726\\
0.0605335823460664 -0.939801553656133\\
0.0605335823460664 -0.928653268865005\\
0.0493852975549392 -0.917504984073878\\
0.0493852975549392 -0.906356699282751\\
0.038237012763812 -0.895208414491624\\
0.038237012763812 -0.884060129700497\\
0.0270887279726848 -0.872911844909369\\
0.0270887279726848 -0.861763560118242\\
0.0270887279726848 -0.850615275327115\\
};
\addlegendentry{contour points};


\end{axis}

\end{tikzpicture}%
\end{document}

答案1

这是一个初步的解决方案。我希望将其推广:将的单位设置为legend image code与的比例,以mark size防止散点与较大的标记重叠。

xbar legend我从(v1.11 手册第 212 页)的定义开始pgfplots,并对其进行了修改以绘制小的坐标散射:

\pgfplotsset{
  /pgfplots/scatter legend/.style={
    /pgfplots/legend image code/.code={\draw[##1,yshift=-0.1em] plot coordinates {
      (0.1em, 0.1em)
      (0.2em, 0.4em)
      (0.3em, 0.0em)
      (0.4em, 0.3em)
    };},
  },
}

这种新scatter legend风格现在可以应用于整个axis环境或单个\addplot命令。

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\pgfplotsset{
  /pgfplots/scatter legend/.style={
    /pgfplots/legend image code/.code={\draw[##1,yshift=-0.1em] plot coordinates {
      (0.1em, 0.1em)
      (0.2em, 0.4em)
      (0.3em, 0.0em)
      (0.4em, 0.3em)
    };},
  },
}

\begin{document}
\begin{tikzpicture}

\begin{axis}[scatter legend]
  \addplot+[only marks,mark=*,mark size = 0.5pt,mark options={solid}] 
    table[row sep=crcr] {
      0.0828301519283208 -0.973246408029514\\
      0.0716818671371936 -0.962098123238387\\
    };
  \addlegendentry{contour points1};
  \addplot+[only marks,mark=*,mark size = 0.5pt,mark options={solid}] 
    table[row sep=crcr] {
      0.0716818671371936 -0.95094983844726\\
      0.0605335823460664 -0.939801553656133\\
    };
  \addlegendentry{contour points2};
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

一种方法是pattern=crosshatch dots通过

\pgfplotsset{
    legend image code/.code={%
        \draw[pattern=crosshatch dots, pattern color=blue, draw=none] 
                (0cm,-0.1cm) rectangle (0.3cm,0.1cm);
    }
}

在此处输入图片描述

代码:

\documentclass[tikz, border=2pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{amsmath}
\begin{document}


\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}
\usetikzlibrary{patterns}

\pgfplotsset{
    legend image code/.code={%
        \draw[pattern=crosshatch dots, pattern color=blue, draw=none] (0cm,-0.1cm) rectangle (0.3cm,0.1cm);
    }
}

\begin{tikzpicture}

\begin{axis}[%
small,
height=3in,
axis equal image,
xmin=-0.3,
xmax=0.3,
y dir=reverse,
ymin=-1.15,
ymax=1.12,
name=plot1,
legend style={draw=black,fill=white,legend cell align=left,at={(axis cs:0.35,0)},anchor=west,font=\footnotesize}
]
\addplot+[
only marks,
mark=*,
mark size = 0.2pt,
mark options={solid}
]
table[row sep=crcr]{
0.0828301519283208 -0.973246408029514\\
0.0716818671371936 -0.962098123238387\\
0.0716818671371936 -0.95094983844726\\
0.0605335823460664 -0.939801553656133\\
0.0605335823460664 -0.928653268865005\\
0.0493852975549392 -0.917504984073878\\
0.0493852975549392 -0.906356699282751\\
0.038237012763812 -0.895208414491624\\
0.038237012763812 -0.884060129700497\\
0.0270887279726848 -0.872911844909369\\
0.0270887279726848 -0.861763560118242\\
0.0270887279726848 -0.850615275327115\\
};
\addlegendentry{contour points};


\end{axis}

\end{tikzpicture}%
\end{document}

相关内容