我正在使用 pgfplots 绘制大量数据,如下所示。对于某些图,我指定了图例条目,但这些图的标记未显示在图例中。有人知道错误在哪里吗?
\documentclass[a4paper]{article}
\usepackage{pgfplots}
\usepgflibrary{shapes.geometric}
\usetikzlibrary{patterns,arrows}
\begin{document}
\begin{tikzpicture}
\centering
\begin{axis}[
height=10cm,
width=\columnwidth,
enlarge y limits=0.07,
enlarge x limits=0.03,
legend style={at={(0.5,-0.1)}, anchor=north,legend columns=-1},
%ylabel={Bezugsphoneme: regspr. Vnt. (Hn)},
symbolic x coords={a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w},
xtick=data
]
\addplot+[sharp plot, only marks, mark options={mark=-, scale=0.8, draw=black}] table[x=vpid, y=-] {data.dat};
\addplot+[sharp plot, only marks, mark options={mark=-, scale=0.8, draw=black}] table[x=vpid, y=a] {data.dat};
\addplot+[sharp plot, only marks, mark options={mark=-, scale=0.8, draw=black}] table[x=vpid, y=aa] {data.dat};
\addplot+[sharp plot, only marks, mark options={mark=-, scale=0.8, draw=black}] table[x=vpid, y=ai] {data.dat};
\addplot+[sharp plot, only marks, mark options={mark=-, scale=0.8, draw=black}] table[x=vpid, y=au] {data.dat};
\addplot+[sharp plot, only marks, mark options={mark=-, scale=0.8, draw=black}] table[x=vpid, y=b] {data.dat};
\addplot+[sharp plot, only marks, mark options={mark=o, scale=0.7, draw=black, solid}] table[x=vpid, y=c] {data.dat}; \addlegendentry{c}
\addplot+[sharp plot, only marks, mark options={mark=-, scale=0.8, draw=black}] table[x=vpid, y=d] {data.dat};
\addplot+[sharp plot, only marks, mark options={mark=triangle, scale=0.8, draw=black, solid}] table[x=vpid, y=ee] {data.dat}; \addlegendentry{ee}
\addplot+[sharp plot, only marks, mark options={mark=-, scale=0.8, draw=black}] table[x=vpid, y=schwa] {data.dat};
\addplot+[sharp plot, only marks, mark options={mark=-, scale=0.8, draw=black}] table[x=vpid, y=E] {data.dat};
\addplot+[sharp plot, only marks, mark options={mark=-, scale=0.8, draw=black}] table[x=vpid, y=EE] {data.dat};
\addplot+[sharp plot, only marks, mark options={mark=-, scale=0.8, draw=black}] table[x=vpid, y=f] {data.dat};
\addplot+[sharp plot, only marks, mark options={mark=-, scale=0.8, draw=black}] table[x=vpid, y=g] {data.dat};
\addplot+[sharp plot, only marks, mark options={mark=-, scale=0.8, draw=black}] table[x=vpid, y=ii] {data.dat};
\addplot+[sharp plot, only marks, mark options={mark=-, scale=0.8, draw=black}] table[x=vpid, y=i] {data.dat};
\addplot+[sharp plot, only marks, mark options={mark=-, scale=0.8, draw=black}] table[x=vpid, y=k] {data.dat};
\addplot+[sharp plot, only marks, mark options={mark=-, scale=0.8, draw=black}] table[x=vpid, y=m] {data.dat};
\addplot+[sharp plot, only marks, mark options={mark=-, scale=0.8, draw=black}] table[x=vpid, y=n] {data.dat};
\addplot+[sharp plot, only marks, mark options={mark=-, scale=0.8, draw=black}] table[x=vpid, y=ng] {data.dat};
\addplot+[sharp plot, only marks, mark options={mark=-, scale=0.8, draw=black}] table[x=vpid, y=oo] {data.dat};
\addplot+[sharp plot, only marks, mark options={mark=-, scale=0.8, draw=black}] table[x=vpid, y=oe] {data.dat};
\addplot+[sharp plot, only marks, mark options={mark=-, scale=0.8, draw=black}] table[x=vpid, y=oee] {data.dat};
\addplot+[sharp plot, only marks, mark options={mark=-, scale=0.8, draw=black}] table[x=vpid, y=o] {data.dat};
\addplot+[sharp plot, only marks, mark options={mark=-, scale=0.8, draw=black}] table[x=vpid, y=oi] {data.dat};
\addplot+[sharp plot, only marks, mark options={mark=-, scale=0.8, draw=black}] table[x=vpid, y=p] {data.dat};
\addplot+[sharp plot, only marks, mark options={mark=-, scale=0.8, draw=black}] table[x=vpid, y=pf] {data.dat};
\addplot+[sharp plot, only marks, mark options={mark=square, scale=0.7, draw=black, solid}] table[x=vpid, y=r] {data.dat}; \addlegendentry{r}
\addplot+[sharp plot, only marks, mark options={mark=diamond, scale=0.8, draw=black, solid}] table[x=vpid, y=s] {data.dat}; \addlegendentry{s}
\addplot+[sharp plot, only marks, mark options={mark=pentagon, scale=0.8, draw=black,solid}] table[x=vpid, y=sch] {data.dat}; \addlegendentry{sch}
\addplot+[sharp plot, only marks, mark options={mark=-, scale=0.8, draw=black}] table[x=vpid, y=t] {data.dat};
\addplot+[sharp plot, only marks, mark options={mark=-, scale=0.8, draw=black}] table[x=vpid, y=ts] {data.dat};
\addplot+[sharp plot, only marks, mark options={mark=-, scale=0.8, draw=black}] table[x=vpid, y=uu] {data.dat};
\addplot+[sharp plot, only marks, mark options={mark=-, scale=0.8, draw=black}] table[x=vpid, y=u] {data.dat};
\addplot+[sharp plot, only marks, mark options={mark=-, scale=0.8, draw=black}] table[x=vpid, y=v] {data.dat};
\addplot+[sharp plot, only marks, mark options={mark=-, scale=0.8, draw=black}] table[x=vpid, y=uee] {data.dat};
\addplot+[sharp plot, only marks, mark options={mark=-, scale=0.8, draw=black}] table[x=vpid, y=ue] {data.dat};
\addplot+[sharp plot, only marks, mark options={mark=-, scale=0.8, draw=black}] table[x=vpid, y=z] {data.dat};
\addplot+[sharp plot, only marks, mark options={mark=-, scale=0.8, draw=black}] table[x=vpid, y=sth_sch] {data.dat};
\addlegendimage{-}
\addlegendentry{others}
\end{axis}
\end{tikzpicture}
\end{document}
数据表:
vpid - a aa ai au b c d ee schwa E EE f g ii i k m n ng oo oe oee o oi p pf r s sch t ts uu u v uee ue z sth_sch
a 4 17 6 8 2 10 28 10 33 6 0 0 3 17 23 4 2 1 14 0 5 0 0 20 0 0 0 79 1 18 10 0 0 3 7 1 0 13 0
b 0 11 2 4 1 2 33 0 24 3 2 0 0 2 12 6 5 0 62 0 5 0 0 1 0 7 2 50 2 22 36 0 0 4 10 0 0 2 0
c 1 12 2 3 1 5 11 1 33 3 4 0 0 6 12 11 2 0 17 0 7 1 0 1 0 6 0 56 3 7 17 0 0 16 28 1 1 3 0
d 0 18 1 6 4 6 56 7 58 6 3 0 4 15 18 15 4 1 27 0 1 1 0 3 0 12 0 74 45 29 29 0 0 8 17 1 0 24 2
e 0 16 1 3 1 8 46 9 35 6 6 0 3 18 11 5 0 0 21 0 2 0 1 22 0 5 2 78 2 47 14 0 1 9 1 0 0 3 2
f 0 1 1 10 2 10 68 7 35 2 5 0 2 7 10 9 0 0 15 0 0 0 0 4 0 3 2 65 24 30 40 0 0 10 1 0 0 1 0
g 4 28 16 40 3 26 49 16 27 10 3 0 5 19 40 40 4 0 24 1 8 0 0 7 0 3 3 71 52 24 19 0 2 5 15 2 2 14 0
h 0 4 1 1 4 5 56 4 29 1 3 0 5 7 19 9 1 1 10 0 0 1 0 2 0 4 2 35 15 35 26 0 2 5 3 0 0 5 0
i 1 10 2 5 5 2 111 8 27 0 2 0 0 11 16 17 1 1 13 0 4 0 0 7 0 1 2 48 30 37 16 0 2 8 5 0 0 2 0
j 0 13 1 5 5 2 95 5 36 2 1 0 1 3 21 20 0 6 11 0 1 0 0 6 0 4 1 43 21 15 15 0 0 4 7 0 0 8 0
k 0 9 9 7 9 12 76 7 41 4 2 0 8 11 31 15 4 0 16 0 12 0 0 4 0 7 2 82 27 43 10 0 0 3 10 0 0 5 0
l 0 12 6 11 8 6 64 9 35 2 4 0 11 15 33 19 1 1 17 0 18 0 0 6 0 3 1 63 44 15 23 0 0 9 27 0 0 31 0
m 0 30 4 3 4 10 64 7 48 6 2 0 3 8 20 8 0 0 26 0 11 1 0 9 0 2 0 115 0 66 13 0 0 14 6 0 0 15 0
n 0 17 0 8 4 6 65 7 33 3 4 0 4 9 22 6 5 3 9 0 7 0 1 4 0 2 2 59 24 44 18 0 1 2 3 0 0 6 0
o 0 1 1 0 3 12 77 5 30 2 3 0 1 10 15 6 0 0 7 0 2 0 0 1 0 4 0 51 0 45 21 0 1 5 3 1 0 2 0
p 0 28 4 6 5 6 41 8 38 1 0 0 2 4 43 3 1 0 17 0 3 0 0 4 0 4 0 65 2 35 26 0 1 2 21 0 0 3 3
q 0 14 3 4 6 2 60 2 30 2 3 0 6 13 29 2 0 1 9 0 3 0 0 3 0 4 2 66 0 38 10 0 0 6 14 0 0 8 0
r 0 40 7 5 5 5 109 13 43 3 2 0 5 17 30 25 1 1 14 0 3 0 0 13 0 4 0 74 71 32 22 0 6 2 22 0 0 4 0
s 1 12 1 2 2 17 41 14 26 2 0 0 3 18 22 5 0 0 11 0 3 0 0 7 0 1 1 62 4 41 8 0 2 4 1 0 0 6 1
t 6 81 44 16 6 20 111 23 31 29 6 0 3 34 70 61 3 0 8 0 24 0 0 13 3 6 3 95 102 52 56 0 18 5 35 6 1 8 0
u 0 19 5 4 17 15 114 22 38 2 8 0 5 8 24 16 1 0 10 0 13 0 0 7 0 2 0 82 25 61 25 0 0 0 11 0 0 13 0
v 0 25 7 6 11 20 142 7 21 6 4 0 7 21 45 15 3 1 12 0 13 0 0 5 0 0 0 52 24 23 37 0 2 6 11 0 0 16 0
w 0 24 1 6 14 6 74 14 15 3 3 0 11 12 22 2 5 1 3 0 7 0 0 6 0 2 0 46 0 35 17 0 1 2 8 0 0 14 0
答案1
您无需重新排序图表,还可forget plot
以为那些不会在图例中出现的数字系列提供支持。
对于像你这样的复杂情节,最好使用样式。我对你的例子做了一些调整:
\documentclass[a4paper]{article}
\usepackage{pgfplots}
\usepgflibrary{shapes.geometric}
\begin{document}
\begin{tikzpicture}
\centering
\begin{axis}[
height=10cm,
width=\columnwidth,
enlarge y limits={upper, value=0.07},
enlarge x limits=0.03,
legend style={
at={(0.5,-0.1)},
anchor=north, %Proper alignment
nodes={anchor=mid},
legend columns=-1,
/tikz/every even column/.append style={column sep=4pt} %Increase spacing in legend
},
symbolic x coords={a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w},
xtick=data,
xticklabel style={text height=1.5ex}, % To get proper alignment
only marks,
every axis plot post/.style={draw=black}, %Common options
other/.style={
forget plot,
mark options={
mark=-, scale=0.8
}
},
c plot/.style={
mark options={mark=o, scale=0.7, solid}
},
ee plot/.style={
mark options={mark=triangle, scale=0.8, solid}
},
r plot/.style={
mark options={mark=square, scale=0.7, solid}
},
s plot/.style={
mark options={mark=diamond, scale=0.8, solid}
},
sch plot/.style={
mark options={mark=pentagon, scale=0.8,solid}
}
]
\addplot [other] table[x=vpid, y=-] {data.dat};
\addplot [other] table[x=vpid, y=a] {data.dat};
\addplot [other] table[x=vpid, y=aa] {data.dat};
\addplot [other] table[x=vpid, y=ai] {data.dat};
\addplot [other] table[x=vpid, y=au] {data.dat};
\addplot [other] table[x=vpid, y=b] {data.dat};
\addplot [c plot] table[x=vpid, y=c] {data.dat}; \addlegendentry{c}
\addplot [other] table[x=vpid, y=d] {data.dat};
\addplot [ee plot] table[x=vpid, y=ee] {data.dat}; \addlegendentry{ee}
\addplot [other] table[x=vpid, y=schwa] {data.dat};
\addplot [other] table[x=vpid, y=E] {data.dat};
\addplot [other] table[x=vpid, y=EE] {data.dat};
\addplot [other] table[x=vpid, y=f] {data.dat};
\addplot [other] table[x=vpid, y=g] {data.dat};
\addplot [other] table[x=vpid, y=ii] {data.dat};
\addplot [other] table[x=vpid, y=i] {data.dat};
\addplot [other] table[x=vpid, y=k] {data.dat};
\addplot [other] table[x=vpid, y=m] {data.dat};
\addplot [other] table[x=vpid, y=n] {data.dat};
\addplot [other] table[x=vpid, y=ng] {data.dat};
\addplot [other] table[x=vpid, y=oo] {data.dat};
\addplot [other] table[x=vpid, y=oe] {data.dat};
\addplot [other] table[x=vpid, y=oee] {data.dat};
\addplot [other] table[x=vpid, y=o] {data.dat};
\addplot [other] table[x=vpid, y=oi] {data.dat};
\addplot [other] table[x=vpid, y=p] {data.dat};
\addplot [other] table[x=vpid, y=pf] {data.dat};
\addplot [r plot] table[x=vpid, y=r] {data.dat}; \addlegendentry{r}
\addplot [s plot] table[x=vpid, y=s] {data.dat}; \addlegendentry{s}
\addplot [sch plot] table[x=vpid, y=sch] {data.dat}; \addlegendentry{sch}
\addplot [other] table[x=vpid, y=t] {data.dat};
\addplot [other] table[x=vpid, y=ts] {data.dat};
\addplot [other] table[x=vpid, y=uu] {data.dat};
\addplot [other] table[x=vpid, y=u] {data.dat};
\addplot [other] table[x=vpid, y=v] {data.dat};
\addplot [other] table[x=vpid, y=uee] {data.dat};
\addplot [other] table[x=vpid, y=ue] {data.dat};
\addplot [other] table[x=vpid, y=z] {data.dat};
\addplot [other] table[x=vpid, y=sth_sch] {data.dat};
\addlegendimage{other}
\addlegendentry{others}
\end{axis}
\end{tikzpicture}
\end{document}
答案2
这是因为\addlegendentry
会创建一个由对该命令的调用次数构成的列表。这意味着第一次调用\addlegendentry
适用于第一个\addplot
,第二次调用\addlegendentry
适用于第二个\addplot
。无论命令本身的位置如何。
您需要做的是将所有 组合\addplot
到\addlegendentry
调用中。
有效地:
\begin{axis}
\addplot+[sharp plot, only marks, mark options={mark=o, scale=0.7, draw=black, solid}] table[x=vpid, y=c] {data.dat}; \addlegendentry{c}
\addplot+[sharp plot, only marks, mark options={mark=triangle, scale=0.8, draw=black, solid}] table[x=vpid, y=ee] {data.dat}; \addlegendentry{ee}
\addplot+[sharp plot, only marks, mark options={mark=square, scale=0.7, draw=black, solid}] table[x=vpid, y=r] {data.dat}; \addlegendentry{r}
\addplot+[sharp plot, only marks, mark options={mark=diamond, scale=0.8, draw=black, solid}] table[x=vpid, y=s] {data.dat}; \addlegendentry{s}
\addplot+[sharp plot, only marks, mark options={mark=pentagon, scale=0.8, draw=black,solid}] table[x=vpid, y=sch] {data.dat}; \addlegendentry{sch}
....
<other>
\end{axis}