有一个triangle
符号pgf图。因为有时需要倒三角形,我将其旋转 180 度。它在图中可以正常工作,但在图例中则不行。
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usepackage[showframe]{geometry}
%\usetikzlibrary{calc,shapes.arrows,datavisualization,arrows.meta,decorations.pathmorphing,backgrounds,positioning,fit,petri,decorations.pathreplacing}
\begin{document}
\begin{figure}
\begin{tikzpicture}
\begin{axis}[xticklabel style={text width=1em},yticklabel style={/pgf/number format/precision =2,/pgf/number format/fixed,/pgf/number format/fixed zerofill},scaled ticks=false,width=7.4cm,height=4cm,xmin=0,xmax=12,ylabel={the third},legend entries={legend1,legend2,legend3} ]
\addplot[
scatter,only marks, point meta=explicit symbolic,
scatter/classes={
a={mark=o,draw=black},
b={mark=square*},
c={mark=triangle*}
},
] table [meta=label]{
x y label
1 0.55476356 c
};
\addplot[mark=none, black,dashed,domain=0:12,samples=2] {0.778};
\end{axis}
\end{tikzpicture}\\
\begin{tikzpicture}
\begin{axis}[xticklabel style={text width=1em},yticklabel style={/pgf/number format/precision =2,/pgf/number format/fixed,/pgf/number format/fixed zerofill},scaled ticks=false,width=7.4cm,height=4cm,xmin=0,xmax=12,ylabel={the third},legend entries={legend1,legend2,legend3} ]
\addplot[
scatter,only marks, point meta=explicit symbolic,
scatter/classes={
a={mark=o,draw=black},
b={mark=triangle,rotate=180},
c={mark=triangle*}
},
] table [meta=label]{
x y label
1 0.55476356 c
};
\addplot[mark=none, black,dashed,domain=0:12,samples=2] {0.778};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
输出:
是什么原因?
答案1
要向绘图标记添加诸如旋转之类的选项,通常应使用mark options
(或every mark/.append style
),如手动的。但是,由于scatter/classes
实施原因,您不能在选项内部使用此功能,但您也不能在环境选项中使用此功能axis
,因为您只想旋转一些标记。
因此,最直接的方法可能就是定义一个自定义标记:
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\pgfdeclareplotmark{rotated triangle}{%
\pgfpathmoveto{\pgfqpoint{0pt}{-\pgfplotmarksize}}%
\pgfpathlineto{\pgfqpointpolar{30}{\pgfplotmarksize}}%
\pgfpathlineto{\pgfqpointpolar{150}{\pgfplotmarksize}}%
\pgfpathclose
\pgfusepathqstroke
}%
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xticklabel style={text width=1em},
yticklabel style={
/pgf/number format/precision =2,
/pgf/number format/fixed,
/pgf/number format/fixed zerofill
},
scaled ticks=false,
width=7.4cm,
height=4cm,
xmin=0, xmax=12,
ylabel={the third},
legend entries={legend1, legend2, legend3}
]
\addplot[
scatter, only marks, point meta=explicit symbolic,
scatter/classes={
a={mark=o, draw=black},
b={mark=rotated triangle},
c={mark=triangle*}
}
] table [meta=label]{
x y label
1 0.55476356 c
2 0.363722187 c
3 0.81286394 c
4 0.875783813 c
5 0.315270863 c
6 0.827911581 c
7 0.720070049 c
8 0.347479399 c
9 0.27043531 c
10 0.490422201 c
11 0.69325892 c
1 0.735883237 a
2 0.129003395 a
3 0.461826926 a
4 0.756650854 a
5 0.305755095 a
6 0.263120301 a
7 0.38954705 a
8 0.07894936 a
9 0.984081129 a
10 0.712322139 a
11 0.146583606 a
1 0.186438911 b
2 0.483752955 b
3 0.955453588 b
4 0.931741157 b
5 0.307806207 b
6 0.932585214 b
7 0.390174549 b
8 0.397465905 b
9 0.893646115 b
10 0.177956892 b
11 0.250648427 b
};
\addplot[mark=none, black,dashed, domain=0:12, samples=2] {0.778};
\end{axis}
\end{tikzpicture}
\end{document}
同样,您可以定义一个可填充的自定义三角形:
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\pgfdeclareplotmark{rotated triangle}{%
\pgfpathmoveto{\pgfqpoint{0pt}{-\pgfplotmarksize}}%
\pgfpathlineto{\pgfqpointpolar{30}{\pgfplotmarksize}}%
\pgfpathlineto{\pgfqpointpolar{150}{\pgfplotmarksize}}%
\pgfpathclose
\pgfusepathqstroke
}
\pgfdeclareplotmark{rotated triangle*}{%
\pgfpathmoveto{\pgfqpoint{0pt}{-\pgfplotmarksize}}%
\pgfpathlineto{\pgfqpointpolar{30}{\pgfplotmarksize}}%
\pgfpathlineto{\pgfqpointpolar{150}{\pgfplotmarksize}}%
\pgfpathclose
\pgfusepathqfillstroke
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xticklabel style={text width=1em},
yticklabel style={
/pgf/number format/precision =2,
/pgf/number format/fixed,
/pgf/number format/fixed zerofill
},
scaled ticks=false,
width=7.4cm,
height=4cm,
xmin=0, xmax=12,
ylabel={the third},
legend entries={legend1, legend2, legend3}
]
\addplot[
scatter, only marks, point meta=explicit symbolic,
scatter/classes={
a={mark=triangle},
b={mark=rotated triangle},
c={mark=rotated triangle*}
}
] table [meta=label]{
x y label
1 0.55476356 c
2 0.363722187 c
3 0.81286394 c
4 0.875783813 c
5 0.315270863 c
6 0.827911581 c
7 0.720070049 c
8 0.347479399 c
9 0.27043531 c
10 0.490422201 c
11 0.69325892 c
1 0.735883237 a
2 0.129003395 a
3 0.461826926 a
4 0.756650854 a
5 0.305755095 a
6 0.263120301 a
7 0.38954705 a
8 0.07894936 a
9 0.984081129 a
10 0.712322139 a
11 0.146583606 a
1 0.186438911 b
2 0.483752955 b
3 0.955453588 b
4 0.931741157 b
5 0.307806207 b
6 0.932585214 b
7 0.390174549 b
8 0.397465905 b
9 0.893646115 b
10 0.177956892 b
11 0.250648427 b
};
\addplot[mark=none, black,dashed, domain=0:12, samples=2] {0.778};
\end{axis}
\end{tikzpicture}
\end{document}