我在使用 pgfkeys 的散点图中遇到了图例问题。图例中半正方形符号的颜色与图中定义的颜色不同,如下图 MWE 所示。
我该如何纠正这个问题?
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
\pgfplotsset{
height=6cm,
scatter/classes={%
a={mark=square,draw=blue},%
b={mark=triangle,draw=red},%
c={mark=o,draw=orange},
d={mark=diamond,draw=green},
e={mark=pentagon,draw=magenta},
f={mark=halfsquare*,cyan,draw=cyan}
},
}
\tikzset{every mark/.append style={scale=2},
}
\def\ec{2cm}
% this is the leftmost y axis
\begin{axis}[
xmin=0,xmax=1,%--- CF
xshift=0,%-- CF
width=1.8cm,
hide x axis,
axis y line*=left,
ymin=25, ymax=27.5,
ytick distance=0.5,
title={$V_{max}$ (V)},
legend style={at={(3,0.5)},anchor=west}
]
\addplot+ [
only marks,
%mark=ball,
scatter,% enable scatter
scatter src=explicit symbolic,% the "color data"
]
table[meta=label] {PlotV.dat};
\legend{a,b,c,d,e,f}
\end{axis}
\end{tikzpicture}
\end{document}
答案1
这是一个没有问题的版本。更干净的方法可能是重写该标记的代码,pgflibraryplotmarks.code.tex
但至少以下是一种解决方法。遗憾的是我不得不编造数据。
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{filecontents*}{PlotV.dat}
x y label
0.1 0.15 a
0.45 0.27 b
0.02 0.17 c
0.06 0.1 d
0.9 0.5 e
0.5 0.3 f
0.85 0.52 a
0.12 0.05 b
0.73 0.45 c
0.53 0.25 d
0.76 0.5 e
0.55 0.32 f
\end{filecontents*}
\begin{document}
\begin{tikzpicture}
\pgfplotsset{
height=6cm,
scatter/classes={%
a={mark=square,draw=blue},%
b={mark=triangle,draw=red},%
c={mark=o,draw=orange},%
d={mark=diamond,draw=green},%
e={mark=pentagon,draw=magenta},%
f={mark=halfsquare*,cyan,mark options={cyan,scale=2}}%
},
}
\tikzset{every mark/.append style={scale=2},
}
\def\ec{2cm}
% this is the leftmost y axis
\begin{axis}[
xmin=0,xmax=1,%--- CF
xshift=0,%-- CF
width=1.8cm,
hide x axis,
axis y line*=left,
%ymin=25, ymax=27.5,
ytick distance=0.5,
title={$V_\mathrm{max}$ (V)},
legend style={at={(3,0.5)},anchor=west}
]
\addplot+ [
only marks,
%mark=ball,
scatter,% enable scatter
scatter src=explicit symbolic,% the "color data"
]
table[meta=label] {PlotV.dat};
\legend{a,b,c,d,e,f}
\end{axis}
\end{tikzpicture}
\end{document}