我有一个包含多列的文件,我创建了散点图,其中位置由第 1 列和第 2 列给出,而标记颜色则基于第 3 列。我现在希望自动创建一个图例,每个图例都有一个条目。
以下解决方案适用于有限数量的颜色(6-7)。它使用颜色图来表示散点图中的点,并使用颜色循环列表(颜色与颜色图中的颜色相同)来表示图例图像。
问题: 我如何将其推广到使用点多于颜色的场景?这需要使用颜色图来绘制图例图像,而不是循环列表。如果可能的话,也可以使用预定义的颜色图,而不是重新定义所有内容。
我认为问题在于\addlegendentry
循环中无法保留当前分散标记的颜色,否则解决方案会容易得多。
\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{filecontents}
\pgfplotsset{compat=newest}
\usetikzlibrary{pgfplots.groupplots}
\begin{filecontents}{data.out}
0 1 2
1 3 4
2 5 6
3 7 8
4 11 12
5 13 14
\end{filecontents}
\pgfplotstableread{data.out}\datatable
\pgfplotsset{
select row/.style={
x filter/.code={\ifnum\coordindex=#1\else\def\pgfmathresult{}\fi}
}
}
\definecolor{RYB1}{RGB}{141, 211, 199}
\definecolor{RYB2}{RGB}{255, 255, 179}
\definecolor{RYB3}{RGB}{190, 186, 218}
\definecolor{RYB4}{RGB}{251, 128, 114}
\definecolor{RYB5}{RGB}{128, 177, 211}
\definecolor{RYB6}{RGB}{253, 180, 98}
\definecolor{RYB7}{RGB}{179, 222, 105}
\pgfplotscreateplotcyclelist{colorbrewer-RYB}{
{RYB1!50!black,fill=RYB1},
{RYB2!50!black,fill=RYB2},
{RYB3!50!black,fill=RYB3},
{RYB4!50!black,fill=RYB4},
{RYB5!50!black,fill=RYB5},
{RYB6!50!black,fill=RYB6},
%{RYB7!50!black,fill=RYB7},
}
\pgfplotsset{colormap={MyRYB}{
rgb255=(141, 211, 199)
rgb255=(255, 255, 179)
rgb255=(190, 186, 218)
rgb255=(251, 128, 114)
rgb255=(128, 177, 211)
rgb255=(253, 180, 98)
% rgb255=(179, 222, 105)
}
}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[
group style={
group name=myplot,
group size=2 by 1,
horizontal sep=60pt,
},
legend pos=outer north east,
% colormap/bluered,
colormap name={MyRYB},
scatter/use mapped color=
{draw=black,fill=mapped color},
cycle list name=colorbrewer-RYB,
]
\nextgroupplot[
legend to name=grouplegend,
legend columns=-1,
legend style={draw=none, /tikz/every even column/.append style={column sep=5pt}},
legend image code/.code={%
\draw[#1] (0cm,-0.1cm) rectangle (0.3cm,0.1cm);
},
]
\pgfplotsinvokeforeach {0,...,6}{
\addplot+[scatter,scatter src=\thisrowno{2},thick]
table [ x index={0}, select row=#1, y index={1}] {data.out};
\addlegendentry {\pgfplotstablegetelem{#1}{2}\of\datatable \pgfplotsretval}
}
\addplot[mark=none,color=red,thick,solid,forget plot]
table [x index={0},y index={1}] {data.out};
\addplot[scatter,scatter src=\thisrowno{2},mark=square*,color=blue,thick,solid,forget plot]
table [x expr=\thisrowno{0}+1,y index={1}] {data.out};
\nextgroupplot
\addlegendimage{mark=*,mark options={draw=black,fill=white},color=red,thick,solid}
\addlegendentry{$a$}
\addlegendimage{mark=square*,mark options={draw=black,fill=white},color=blue,thick,solid}
\addlegendentry{$b$}
\addplot[scatter,scatter src=\thisrowno{2},mark=*,color=red,thick,solid]
table [x index={0},y index={1}] {data.out};
\addplot[scatter,scatter src=\thisrowno{2},mark=square*,color=blue,thick,solid]
table [x expr=\thisrowno{0}+1,y index={1}] {data.out};
\end{groupplot}
\node at ($(myplot c1r1.south)!0.5!(myplot c2r1.south)$)
[inner sep=0pt,anchor=north, yshift=-5ex]
{\ref{grouplegend}};
\end{tikzpicture}
\end{document}
这个想法是从杰克的回答中抄袭来的这里。